SQL help

Hi guys,.

Please can you help with this query

CREATE TABLE test (id NUMBER, code NUMBER, prefer VARCHAR2(1), status VARCHAR2(1), email VARCHAR2(50));
          
          INSERT INTO test VALUES (123, 2,'N','A','test');
          INSERT INTO test VALUES (123, 2,'Y','A','test2');
          
         

I have this request

        
          SELECT *
            FROM test
           WHERE id = 123
             AND (code = 2 AND prefer = 'Y' AND status = 'A') OR (code = 2 AND status = 'A');           

This returns two records but I want only 1 sheet to be returned because it meets the first condition: code = 2 AND you prefer = 'Y' AND status = 'A '. If it did not meet this condition, I want to check the second codition: code = 2 AND status = 'A '. How should I write this query.

Thank you.


Thankc.

= 2 AND you prefer = 'Y' AND status = 'A' ode c = 2 AND you prefer = 'Y' AND status = 'A' How

Try this

Select * from

(

SELECT test.*

-case when code = 2 AND you prefer = 'Y' AND status = 'A' then 1

When the code = 2 AND status = 'A' then 2

otherwise 0 end chk

OF the test

ID WHERE = 123

order by chk

) where rownum = 1

Tags: Database

Similar Questions

  • Problem SQL HELP during the installation of aviation the interactive dvd course!

    try to download interactive dvd Aviation course and be sktc kingschool SQL error does not exist... Check the State of SQL express instance and I install the classes I get a SsRestart occurred must close and send error report... Any help would be great!    Help!

    I suggest that you contact the manufacturer of the interactive DVD software to help get the program installed.

    They would be better able to help with your problem.

    Thank you

    Marilyn

  • SQL Help: Add flag for several tables

    Hello

    I have an existing SQL as follows:

    Select the storeId field,

    (select case when count (1) > 0 0 otherwise then 1 end)

    of tb_bbb

    where tb_bbb.id = tb_base.baseId) AS conInd

    of tb_base;

    I would now add the table tb_ccc for the value of conInd. I try the following SQL:

    Select the storeId field,

    (select case when count (1) > 0 0 otherwise then 1 end)

    Of

    (select tb_bbb.id where tb_bbb.id = tb_base.baseId)

    Union

    Select tb_ccc.id where tb_ccc.id = tb_base.basdId) AS conInd

    of tb_base;

    However, it does not work. Any idea on this SQL?  Thanks for your help!

    Hello

    Here is another way, which may be more effective because it uses EXISTS (which can quit smoking as soon as it finds a match) instead of COUNT (who continues to go, so he can tell you if there are 999 or 1000 matches, even if you don't care to).

    SELECT storeid

    CASE

    WHEN THERE IS)

    SELECT 1

    OF tb_bbb

    WHERE bbb_id = m.baseid

    ) 1 THEN

    WHEN THERE IS)

    SELECT 1

    OF tb_ccc

    WHERE ccc_id = m.baseid

    ) 1 THEN

    0 OTHERWISE

    END AS conlnd

    OF tb_base m

    ;

  • Updated SQL help

    Hi all

    I need correct column value serNo to the right as a result. It suppose to be the sequence number for each storeID. Any suggestion on the SQL?

    create table test1 (storeID number (5), number serNo (5));

    insert into table test1 values (100, 1);

    insert into table test1 values (200, 1);

    insert into table test1 values (100, 1);

    insert into table test1 values (200, 1);

    insert into table test1 values (100, 1);

    insert into table test1 values (100, 1);

    I want to have the following results in test1 after conversion:

    1 100

    2 100

    3 100

    4 100

    1 200

    2 200

    Thank you

    Hello

    942572 wrote:

    Hi all

    I need correct column value serNo to the right as a result. It suppose to be the sequence number for each storeID. Any suggestion on the SQL?

    create table test1 (storeID number (5), number serNo (5));

    insert into table test1 values (100, 1);

    insert into table test1 values (200, 1);

    insert into table test1 values (100, 1);

    insert into table test1 values (200, 1);

    insert into table test1 values (100, 1);

    insert into table test1 values (100, 1);

    I want to have the following results in test1 after conversion:

    1 100

    2 100

    3 100

    4 100

    1 200

    2 200

    Thank you

    Use the ROW_NUMBER analytic function to generate the new sernos, like this:

    MERGE INTO dst test1

    WITH THE HELP OF)

    SELECT ROWID AS r_id

    ROW_NUMBER () OVER (PARTITION BY storeid

    ORDER BY NULL

    ) AS serno

    OF test1

    )               src

    ON (summer time. ROWID = src.r_id)

    WHEN MATCHED THEN UPDATE

    SET dst.serno = src.serno

    ;

    Really, your table must have a primary key.  Because it's not working, I had to ROWID allows you to uniquely identify the lines.

  • SQL Help: How to collect summary number in group by?


    Hello

    I have the following table:

    create table tb_class_info (classNbr number (5), number (2) of ClassType, classTeacherNbr number (4));

    insert into tb_class_info values (101, 1, 12);

    insert into tb_class_info values (001, 2, 12);

    insert into tb_class_info_values (001, 2, 13);

    insert into tb_class_info_values (002, 2, 12);

    insert into tb_class_info_values (002, 2, 12);

    I would like to get statistics on the cnt, cnt for classType = 2 classTeacherNbr classNbr. I expect to have the following results:

    classNbr classType2Cnt classTeacherCnt

    101                0                         0

    001                2                          2

    002                2                          1

    The following SQL code I use:

    SELECT classNbr,

    SUM (CASE when classType = 2 THEN 1 ELSE END 0) as classTypeCnt,

    SUM (CASE when classType = 2 SO unique classTeacherNbr ELSE 0 END) as classTeacherCnt

    OF tb_class_info

    GROUP BY classNbr;

    However, I have the ' ORA-00905: lack of keyword ' error. Any suggestion on this subject?

    Thanks for your help!

    Select classnbr

    sum (case when classtype = 2 then 1 end)

    , count (distinct case when classtype = 2 then teachernbr end)

    of tb_class_into

    Classnbr group;

  • PL/SQL help

    I'm pretty new in pl/sql, I want to update a column with the next number.

    Here's an example-

    Table: political

    Policy_noSequenceCompany_name
    1010ABC PTY LTD.
    1010XYZ PTY LTD
    1030Test PTY LTD
    1040Test101 PTY LTD
    1010Pearl PTY LTD
    1040MyZore pty ltd

    I would get the date in the following format

    After Update - table

    Policy_noSequenceCompany_name
    1011ABC PTY LTD.
    1012XYZ PTY LTD
    1013Pearl PTY LTD
    1031Test PTY LTD
    1041Test101 PTY LTD
    1042MyZore pty ltd

    Basically, I want to update the column sequence based on the Policy_No group.

    Can someone please help.

    >

    Basically, I want to update the column sequence based on the Policy_No group.

    >

    This query will show you how to generate the values you need.

    >

    Select empno, deptno, row_number() over (partition by deptno) myRow arrested by deptno from EMP

    EMPNO, DEPTNO, MYROW

    7782,10,1

    7839,10,2

    7934,10,3

    7566,20,1

    7902,20,2

    7876,20,3

    7369,20,4

    7788,20,5

    7521,30,1

    7844,30,2

    7499,30,3

    7900,30,4

    7698,30,5

    7654,30,6

    >

    According to the number of rows in the table, you might be better to create a new table using DEC or a query like this.

  • Multiple Table join without loss of data - SQL help

    Hello
    I have the following tables 3 employee as indicated in the scripts below, please help in writing SQL that connects all these 3 tables and displays the output as shown below

    Required output
    EMP ID Loc Value 1 Value 2 Value 3                    
    1     A     3     1     6
    1 B 4 9 null
    Value null null 1 Wh 8
    2 Z 4 9 null
    3 R 0 null 1
    4 Y 1 null null
    5 O 7 7 null
    Value null null 5 Wh 5
    6 M 7 null 7
    6 B null null 8

    / * Create Tables * /.
    Create table Emp1 (Empid number, Loc Varchar2 (10), Value_1 number);
    Create the Emp2 Table (Empid number, Loc Varchar2 (10), Value_2 number);
    Create table emp3 (empid number, loc varchar2 (10), Value_3);

    / * Insert Scripts * /.
    Insert Into Emp1 Values (1, 'A', 3);
    Insert Into Emp1 Values (2, 'Z', 4);
    Insert Into Emp1 Values (3, 'R', 0);
    Insert Into Emp1 Values (4, 'Y', 1);
    Insert Into Emp1 Values (5, 'o', 7);
    Insert Into Emp1 Values (6, am', 7);

    Insert Into Emp2 Values (1, 'A', 1);
    Insert Into Emp2 Values (1, 'B', 4);
    Insert Into Emp2 Values (1, 'Wh', 8);
    Insert Into Emp2 Values (2, 'Z', 9);
    Insert Into Emp2 Values (5, 'o', 7);
    Insert Into Emp2 Values (5, 'Wh', 5);

    Insert Into Emp3 Values (1, 'A', 6);
    Insert Into Emp3 Values (1, 'B', 9);
    Insert Into Emp3 Values (3, 'R', 1);
    Insert Into Emp3 Values (6, am', 7);
    Insert Into Emp3 Values (6, 'B', 8);
    Commit;


    Thank you
    Varun

    Like this?

    SQL> select nvl(e.empid, e3.empid) empid
      2       , nvl(e.loc, e3.loc) loc
      3       , e.value_1
      4       , e.value_2
      5       , e3.value_3
      6    from (
      7            select nvl(e1.empid, e2.empid) empid
      8                 , nvl(e1.loc, e2.loc) loc
      9                 , e1.value_1
     10                 , e2.value_2
     11              from emp1 e1
     12              full join emp2 e2
     13                on e1.empid = e2.empid
     14               and e1.loc = e2.loc
     15         ) e
     16    full join emp3 e3
     17      on e.empid = e3.empid
     18     and e.loc = e3.loc
     19   order
     20      by empid
     21       , loc
     22  /
    
         EMPID LOC           VALUE_1    VALUE_2    VALUE_3
    ---------- ---------- ---------- ---------- ----------
             1 A                   3          1          6
             1 B                              4          9
             1 Wh                             8
             2 Z                   4          9
             3 R                   0                     1
             4 Y                   1
             5 O                   7          7
             5 Wh                             5
             6 B                                         8
             6 M                   7                     7
    
    10 rows selected.
    
    SQL> 
    
  • SQL help. Identify changes to a field.

    Greetings!
    PS/SQL is not an option for me. I need help to use SQL, if possible for the following scenario.
    Oracle 10G.

    Table: JOB_DATA

    EMPLID, DATE_EFF, DEPTID, JOBCODE
    100, 01/11/2012, 34567, MNG
    100, 01/10/2012, 34567, SUP
    100, 01/09/2012, 28967, MNG
    100, 15/08/2012, 28967, SUP
    100,6/30/2012,15879, MNG

    I need to get the following records only, in other words, every time that changes in the Department ID.

    100, 01/10/2012, 34567, SUP
    100, 15/08/2012, 28967, SUP
    100,6/30/2012,15879, MNG

    Thanks in advance.

    It looks like you want something like

    SELECT *
      FROM (SELECT j.*, lag(deptid) over (partition by emplid order by date_eff) prior_deptid
              FROM job_data j)
     WHERE prior_deptid IS NULL
        OR prior_deptid != deptid
    

    Justin

  • SQL help on taking in SHORT

    Hello

    I have the following table
    EmployeeID | ProjectID | Tasks
    EMP 01     | PID 01    | 5
    EMP 01     | PID 01    | 1
    EMP 01     | PID 02    | 8
    EMP 01     | PID 03    | 9
    EMP 02     | PID 01    | 7
    EMP 02     | PID 02    | 4
    EMP 02     | PID 02    | 3
    EMP 03     | PID 04    | 1
    EMP 04     | PID 04    | 8
    EMP 04     | PID 04    | 7
    I need to write the sql query to get the following results
    EmployeeID | ProjectID | EmpTasks | ProjectTasks
    EMP 01     | PID 01    | 23       | 6
    EMP 01     | PID 01    | 23       | 6
    EMP 01     | PID 02    | 23       | 8
    EMP 01     | PID 03    | 23       | 9
    EMP 02     | PID 01    | 14       | 7
    EMP 02     | PID 02    | 14       | 7
    EMP 02     | PID 02    | 14       | 7
    EMP 03     | PID 04    | 1        | 1
    EMP 04     | PID 04    | 15       | 15
    EMP 04     | PID 04    | 15       | 15
    Any help?

    Thank you

    This should do it:

    WITH t AS
    (SELECT 'EMP 01' EmployeeID, 'PID 01' ProjectID, 5 Tasks FROM dual UNION ALL
     SELECT 'EMP 01' EmployeeID, 'PID 01' ProjectID, 1 Tasks FROM dual UNION ALL
     SELECT 'EMP 01' EmployeeID, 'PID 02' ProjectID, 8 Tasks FROM dual UNION ALL
     SELECT 'EMP 01' EmployeeID, 'PID 03' ProjectID, 9 Tasks FROM dual UNION ALL
     SELECT 'EMP 02' EmployeeID, 'PID 01' ProjectID, 7 Tasks FROM dual UNION ALL
     SELECT 'EMP 02' EmployeeID, 'PID 02' ProjectID, 4 Tasks FROM dual UNION ALL
     SELECT 'EMP 02' EmployeeID, 'PID 02' ProjectID, 3 Tasks FROM dual UNION ALL
     SELECT 'EMP 03' EmployeeID, 'PID 04' ProjectID, 1 Tasks FROM dual UNION ALL
     SELECT 'EMP 04' EmployeeID, 'PID 04' ProjectID, 8 Tasks FROM dual UNION ALL
     SELECT 'EMP 04' EmployeeID, 'PID 04' ProjectID, 7 Tasks FROM dual)
    SELECT EmployeeID,
           ProjectID,
           SUM(Tasks) OVER (PARTITION BY EmployeeID) EmpTasks,
           SUM(Tasks) OVER (PARTITION BY EmployeeID, ProjectID) ProjectTasks
    FROM   t
    
  • REGEXP_LIKE sql help...

    I have a first_name column in my table of students with the following characteristics:

    REDA
    Kris
    Tanvi
    Shweta
    Rajendra
    Kapil
    Vipin
    Sandeep

    Now I'm trying to use the regexp_like function to find the names that begin the letter s and end with the letter p, but I am not able to achieve that I provided here my request please help:

    Select first_name
    the student
    where regexp_like (name, ' ^ sp$ ');

    Concerning
    Rahul

    Mac_Freak_Rahul wrote:
    where regexp_like (name, ' ^ sp$ ');

    Describe your regular expression is to find the string "sp". You do not include buildings for characters that could go p and between s. Here is an example that may help:

    SQL> WITH student AS
      2  (
      3          SELECT 'raj' AS first_name FROM dual UNION ALL
      4          SELECT 'kris' AS first_name FROM dual UNION ALL
      5          SELECT 'tanvi' AS first_name FROM dual UNION ALL
      6          SELECT 'shweta' AS first_name FROM dual UNION ALL
      7          SELECT 'rajendra' AS first_name FROM dual UNION ALL
      8          SELECT 'kapil' AS first_name FROM dual UNION ALL
      9          SELECT 'vipin' AS first_name FROM dual UNION ALL
     10          SELECT 'sandeep' AS first_name FROM dual
     11  )
     12  SELECT first_name
     13  FROM   student
     14  WHERE  REGEXP_LIKE(first_name,'^s.*p$')
     15  ;
    
    FIRST_NA
    --------
    sandeep
    
  • Coding SQL help

    Hi all
    I need help on getting the sql code.
    Here is how the example data are arranged in my data table called for example.

    favorite colors of name age
    John doe 15 Red: Blue: Green

    Notice how the data in the column "favorite colors", defined the values selected from a list of several.
    I need the sql code so that it can display like this.

    favorite colors of name age
    John doe 15 Red
    John doe 15 blue
    John doe 15 green

    Thank you

    Hi bigmac704,

    There may be other ways to do it, but one way is to use apex_util.string_to_table with a function in the pipeline:

    (1) create a type to define the line of your favorite color of the person:

    CREATE OR REPLACE TYPE  PERSON_COLOR as object (name varchar2(30), age number(3, 0), fav_color varchar2(10));
    

    (2) create a type to define a "picture" of your person-favorite color lines:

    CREATE OR REPLACE TYPE  PERSON_COLOR_TBL AS TABLE OF person_color
    

    (3) create the following function in the pipeline:

    CREATE OR REPLACE FUNCTION get_person_colors RETURN person_color_tbl
    PIPELINED IS
      out_rec    person_color := person_color(null, null, null);
      l_vc_arr2  APEX_APPLICATION_GLOBAL.VC_ARR2;
    BEGIN
      FOR r IN (SELECT * FROM person_colors) LOOP
        out_rec.name := r.name;
        out_rec.age := r.age;
        l_vc_arr2 := APEX_UTIL.STRING_TO_TABLE(r.fav_colors);
        // for each color, pipe a row
        FOR i IN 1..l_vc_arr2.count LOOP
          out_rec.fav_color := l_vc_arr2(i);
          pipe row(out_rec);
        END LOOP;
      END LOOP;
      RETURN;
    END;
    

    Now you can use this function in the pipeline, just as you would use a table:

    SELECT * FROM TABLE(get_person_colors);
    NAME AGE FAV_COLOR
    john doe 15 red
    john doe 15 blue
    john doe 15 green 
    
    SELECT * FROM TABLE(get_person_colors) WHERE name = 'john doe';
    NAME AGE FAV_COLOR
    john doe 15 red
    john doe 15 blue
    john doe 15 green 
    
    SELECT * FROM TABLE(get_person_colors) WHERE name = 'jim smith';
    no data found
    

    Hope this helps,
    John

    If you find this information useful, please do not forget to mark the 'useful' or 'correct' post so that others benefit as well.

  • SQL Help: Derive from 'News' columns based on condition

    Hi all

    Can someone help me with this please? I am trying to achieve in a SQL (no), if this can be done in two different stages, even that is fine too...

    Thanks in advance,
    Chavigny
    SELECT * FROM
    (
    SELECT 2765 BTCH_ID,'payroll' BTCH_NM,'payroll~EUR~EUR~DE'NEW_BTCH_NM,'1234'TMPLT_ID FROM DUAL
    UNION
    SELECT 2765,'payroll','payroll~USD~USD~US','1235' FROM DUAL
    UNION
    SELECT 2765,'payroll','payroll~USD~USD~US','1236' FROM DUAL
    UNION
    SELECT 2765,'payroll','payroll~GBP~GBP~GB','1237' FROM DUAL
    UNION
    SELECT 2766,'SALARY','SALARY~USD~USD~US','1238' FROM DUAL
    UNION
    SELECT 2766,'SALARY','SALARY~USD~USD~US','1239' FROM DUAL
    ) T_BATCH;
    For a "batch - id", there are several Transactions that are denoted by (cr_crncy_cd, dr_crncy_cd, recvr_cntry_cd), and the "NEW_BTCH_NM" column is basically a concatenation of 'BTCH_NM' + above 3 columns (with a ~)

    I'm trying to derive two new columns "SEQ" & "Output_Batch_name".

    The SEQ column:
    For every batch_id, the first row will be assigned by sequence.nextval.
        For all transactions within BTCH_ID, 
                       all the rows with same NEW_BTCH_NM, share the same sequence number
                        if different, then SEQ.nextval for all distinct rows
    Output_BTCH_NM:
       for a given batch_id, 
             if the NEW_BTCH_NM is the same for all transactions ( batch_id 2766 in the below example) ,  
                             then Output_BTCH_NM = BTCH_NM should be marked for All transactions which belong to this Batch_id,
            Else 
                Output_BTCH_NM = New_BTCH_NM
         END if
    EXPECTED RESULTS
    *BTCH_ID     BTCH_NM             NEW_BTCH_NM          TMPLT_ID     SEQ         Output_BTCH_NM*
    2765       payroll                payroll~EUR~EUR~DE     1234         1          payroll~EUR~EUR~DE
    2765       payroll                payroll~GBP~GBP~GB     1237         2          payroll~GBP~GBP~GB
    2765       payroll                payroll~USD~USD~US     1235         3          payroll~USD~USD~US
    2765       payroll                payroll~USD~USD~US     1236         3          payroll~USD~USD~US
    
    2766       SALARY                SALARY~USD~USD~US     1238         4          SALARY
    2766       SALARY                SALARY~USD~USD~US     1239         4          SALARY
    Published by: user10711957 on 25 Sep, 2010 16:37

    An extension of the solution of the (slightly modified) jiq

    function my_sequence(whichval in varchar2) return number is
    begin
      if whichval = 'currval' then
        return your_sequence.currval;
      else
        return your_sequence.nextval;
      end if;
    end my_sequence;
    
    WITH t AS
    (
    SELECT 2765 BTCH_ID,'payroll' BTCH_NM,'payroll~EUR~EUR~DE'NEW_BTCH_NM,'1234'TMPLT_ID FROM DUAL
    UNION
    SELECT 2765,'payroll','payroll~USD~USD~US','1235' FROM DUAL
    UNION
    SELECT 2765,'payroll','payroll~USD~USD~US','1236' FROM DUAL
    UNION
    SELECT 2765,'payroll','payroll~GBP~GBP~GB','1237' FROM DUAL
    UNION
    SELECT 2766,'SALARY','SALARY~USD~USD~US','1238' FROM DUAL
    UNION
    SELECT 2766,'SALARY','SALARY~USD~USD~US','1239' FROM DUAL
    )
    select btch_id,btch_nm,new_btch_nm,tmplt_id,
           case when lag(seq,1) over (order by tmplt_id) = seq
                then my_sequence('currval')
                else my_sequence('nextval')
           end seq,
           out_btch_nm
    select btch_id,btch_nm,new_btch_nm,tmplt_id,
           dense_rank() over(partition by btch_id order by new_btch_nm) seq
           case when count(distinct new_btch_nm) over (partition by btch_id) = 1
                then btch_nm
                else new_btch_nm
           end out_btch_nm
      from t
    

    Concerning

    Etbin

    Posted before you see the following

    the sequence # must be continuous, must avoid any shortfall or gaps with the numbering.

    the foregoing must be the only user of its own sequence somehow and maybe...
    Search for "sequence free gap"... http://asktom.oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:4343369880986
    Edited by: Etbin on 26.9.2010 08:21

  • RDB. Oracle with SQL help

    Hi all the gurus.

    I m NOT a guru :-(

    It's my problem.
    I have 2 tables with same columns
    sales_calculation_text and tmp_calculation_text.
    Columns of the SALES_CALCULATION_TEXT table: (and tmp...)
    Column name Data Type Domain
    ----------- --------- ------
    STOCK_ID TANK (2)
    DEAL_NO CHAR (10)
    CALC_VERSION_NO SMALLINT
    TEXT_ROW_TYPE TANK (25)
    TEXT_ROW_NO SMALLINT
    TEXT_ROW TANK (80)


    The SALES table contains about 800000 lines and is used by the application.

    Every night the TMP table is fallen, recreated and populated by some infiles. After the table TMP contains the population every night
    some exactly identical rows in the SALES table, a few new lines and a few lines with update TEXT_ROW.
    The infiles contains:
    new DEAL_NO of information
    old DEAL_NO with TEXT_ROW update on the existing test_rows (won´t be moved to SALES-table with todays SQL...)

    After that TMP table is re-created/filled the following sql code moves the data:

    INSERT INTO sales_calculation_text
    SELECT * FROM t tmp_calculation_text WHERE
    NOT EXIST
    (SELECT * FROM sales_calculation_text where)
    a.stock_id = t.stock_id and
    a.deal_no = t.deal_no and
    a.calc_version_no = t.calc_version_no and
    a.text_row_type = t.text_row_type and
    a.text_row_no = t.text_row_no);
    COMMIT;

    This has been very good since the beginning of the application a few years ago, but recentlly that the infiles contains updated TEXT_ROW that will not be updated
    by the sql...

    I don't have the skills to write SQL - someone please HELP me...

    BR

    Bjornr

    After INSERTING run this

    UPDATE sales_calculation_text a
    SET text_row = (select text_row FROM tmp_calculation_text t WHERE
    a.stock_id=t.stock_id and
    a.deal_no=t.deal_no and
    a.calc_version_no=t.calc_version_no and
    a.text_row_type=t.text_row_type and
    a.text_row_no=t.text_row_no)
    WHERE 1=(SELECT count(0) FROM tmp_calculation_text t  WHERE
    a.stock_id=t.stock_id and
    a.deal_no=t.deal_no and
    a.calc_version_no=t.calc_version_no and
    a.text_row_type=t.text_row_type and
    a.text_row_no=t.text_row_no);
    COMMIT;
    

    Max

  • SQL Help: selection of tickets without open tasks

    Hi gurus,

    I'm new to SQL. I need your help for a script where I pick up tickets without open tasks.
    Say, here is the table of tasks

    Table: task

    ------------------------------
    | TicketID | TaskID. TaskStatus.
    ------------------------------
    | 1. 1. O |
    | 1. 2. O |
    | 1. 3. O |
    | 2. 4. C |
    | 2. 5. C |
    | 3. 6. C |
    | 3. 7. O |
    ------------------------------
    The query should return the ticketid (s) with all its taskstatus = 'C '.

    Any help would be much appreciated.

    Thank you

    Hello

    surya_vus wrote:
    Frank,

    Your query will exclude other State of the task?

    Try it and see!

    If, in your real problem, the relevant information are spread over three tables, then join them.
    If there is a condition of additional, based on reason, then add that. The WHERE clause is the most logical place.
    If you are interested in taskstatus in 'C' or 'F', then test for one or the other.

    If the GROUP OF... HAVING solution is:

    SELECT       ti.ticketid
    FROM       ticket     ti
    JOIN       task          ta     ON ti.ticketid          = ta.ticketid
    JOIN       taskstatus     st     ON ta.taskstatusid     = st.taskstatusid
    WHERE       ti.reason     = 'C'
    GROUP BY  ticketid
    HAVING       COUNT (*)     = COUNT     ( CASE
                             WHEN  st.taskstatus IN ('C', 'F') THEN 1
                          END
                        );
    
  • Logical year SQL help...

    Hi, can anyone suggest a better way to please...
    I'm looking to calculate the number of policies here and I want to group counties datewise/yearwise as below:

    (1) County policies added during the month of the date of entry into
    (2) policies County added since (1 month to 1 year)
    (3) policies County added in the second year (i.e. between 1 year and 2nd year)
    (4) County of policies are complemented by year 2 (i.e. > 2 years)

    Below is the example sql with that I came:

    (1) select count (plcy_id)
    from table_1 b
    where
    b.PLCY_EFDT < = b.ORIGINAL_PLCY_EFDT + 30


    (2) select count (plcy_id)
    from table_1 b
    where
    b.PLCY_EFDT (b.ORIGINAL_PLCY_EFDT + 30) AND (b.ORIGINAL_PLCY_EFDT + 360)

    (3) select count (plcy_id)
    from table_1 b
    where
    b.PLCY_EFDT (b.ORIGINAL_PLCY_EFDT + 360) AND (b.ORIGINAL_PLCY_EFDT + 720)


    Count (plcy_id) select 4)
    from table_1 b
    where
    b.PLCY_EFDT > = b.ORIGINAL_PLCY_EFDT + 720


    Please let me know if there is any way to rewrite the query... Thanks for the help.

    better to use the add_month function
    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/functions004.htm#SQLRF00603

Maybe you are looking for