Restrict the condition on the query data - help.

I want to restrict the query with the following condition data. I can't get the syntax right. Any help is appreciated

Select *...
Of...
where ps.name in ('black' or ('color' and not 'film') or ('film'))

OAF - dev wrote:
I want to restrict the query with the following condition data. I can't get the syntax right. Any help is appreciated

Select *...
Of...
where ps.name in ('black' or ('color' and not 'film') or ('film'))

select *
from ...
where ps.name in ('black', 'color', 'film')

list of all the options that you want to match before 'film', to 'film' last.

Published by: user142857 on November 16, 2009 13:04

Tags: Database

Similar Questions

  • procedure that will dynamically build the query data and table Medallion

    Hi people,

    I write a procedure that dynamically build the query data and insert in the table "dq_summary".
    enforcement procedure with success and data not inserted into the table 'dq_summary '.

    I have thin problem in code attached below
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    FOR rep IN cur_di_attr
          LOOP
            dbms_output.put_line ('d: ');   
            
            BEGIN
              EXECUTE IMMEDIATE 'SELECT table_name FROM ' || sum_tab || ' WHERE id = ' || rep.attribute_id INTO rep_tab;
              dbms_output.put_line ('rep_tab: '||rep_tab);
              run_query := run_query || ' ' || rep_tab || ' WHERE ' || nvl(wh_cond, '1 = 1');
              EXECUTE IMMEDIATE run_query INTO end_rslt;
            
              EXECUTE IMMEDIATE 'UPDATE dq_summary SET ' || prop || '_' || p_code || ' = ' || end_rslt || ' WHERE attribute_id = ' || rep.attribute_id;
              dbms_output.put_line ('e: ');      
              dbms_output.put_line ('rep_tab: '||rep_tab);
              dbms_output.put_line ('end_rslt: '||end_rslt);
              dbms_output.put_line ('f: '); 
            EXCEPTION
              WHEN no_data_found THEN
                rep_tab := '';
                sum_tab := '';
            END;  
          
          END LOOP;    
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    but in the procedure below must be run several times
    create or replace
    PROCEDURE DQ_REPORT_PROC
    AS
      prop                              di_proposition.pro_name%type;
      col_var                           VARCHAR2(100);
      p_code                            dq_parameter.para_code%type;
      sum_tab                           di_proposition.summary_table%type;
      run_query                         dq_parameter.run_query%type;
      wh_cond                           dq_parameter.where_cond%type;
      end_rslt                          VARCHAR2(20);
      rep_tab                           VARCHAR2(50);
      v_error_msg                       VARCHAR2(200);   
      v_error_code                      VARCHAR2(200);  
      v_object_name                     VARCHAR2(50)                          DEFAULT 'DQ_REPORT_PROC';
      v_iss_no                          VARCHAR2(20)                          DEFAULT NULL;
      CURSOR cur_di_prop IS 
        SELECT upper(replace(replace(pro_name, ' '),'-')) pro_name
          FROM di_proposition;
      
      CURSOR cur_di_para IS
        SELECT upper(para_code) para_code, run_query, where_cond
          FROM dq_parameter;
      
      CURSOR cur_di_attr IS 
        SELECT attribute_id
          FROM dq_summary;
    BEGIN
      
      DELETE FROM dq_summary;
    
      INSERT INTO dq_summary (attribute_id, entity_name, attribute_name, data_champ) 
        SELECT a.attribute_id, b.entity_name, a.attribute_name, a.data_champ
          FROM di_attribute_master a, di_entity_master b
         WHERE a.entity_id = b.entity_id;
    
      FOR c_prop IN cur_di_prop
      LOOP
        prop := c_prop.pro_name;
        
        BEGIN
          SELECT distinct SUBSTR(column_name, 1, INSTR(column_name, '_')-1), summary_table
            INTO col_var, sum_tab
            FROM user_tab_cols a, di_proposition b
           WHERE a.table_name = 'DQ_SUMMARY'
             AND upper(replace(replace(b.pro_name, ' '),'-')) = prop
             AND SUBSTR(a.column_name, 1, INSTR(a.column_name, '_')-1) = upper(replace(replace(b.pro_name, ' '),'-'))
             AND upper(b.status) = 'Y';
             
             dbms_output.put_line ('col_var: '||col_var);
             dbms_output.put_line ('sum_tab: '||sum_tab);
             
        EXCEPTION
          WHEN no_data_found THEN
            col_var := '';
            sum_tab := '';
        END;
    
        dbms_output.put_line ('a: ');
    
        FOR para IN cur_di_para
        LOOP
         dbms_output.put_line ('b: ');
          p_code := para.para_code;
          run_query := para.run_query;
          wh_cond := para.where_cond;
          dbms_output.put_line ('c: ');
          FOR rep IN cur_di_attr
          LOOP
            dbms_output.put_line ('d: ');   
            
            BEGIN
              EXECUTE IMMEDIATE 'SELECT table_name FROM ' || sum_tab || ' WHERE id = ' || rep.attribute_id INTO rep_tab;
              dbms_output.put_line ('rep_tab: '||rep_tab);
              run_query := run_query || ' ' || rep_tab || ' WHERE ' || nvl(wh_cond, '1 = 1');
              EXECUTE IMMEDIATE run_query INTO end_rslt;
            
              EXECUTE IMMEDIATE 'UPDATE dq_summary SET ' || prop || '_' || p_code || ' = ' || end_rslt || ' WHERE attribute_id = ' || rep.attribute_id;
              dbms_output.put_line ('e: ');      
              dbms_output.put_line ('rep_tab: '||rep_tab);
              dbms_output.put_line ('end_rslt: '||end_rslt);
              dbms_output.put_line ('f: '); 
            EXCEPTION
              WHEN no_data_found THEN
                rep_tab := '';
                sum_tab := '';
            END;  
          
          END LOOP;    
        END LOOP;
      END LOOP; 
      COMMIT;   
    EXCEPTION
          WHEN OTHERS THEN
             v_error_msg   := SQLERRM;
             v_error_code  := SQLCODE;  
             TRACKER_LOG_EXECEPTION(v_iss_no, v_object_name, CURRENT_TIMESTAMP, v_error_msg, v_error_code);
          COMMIT;        
      
    END DQ_REPORT_PROC;
    Published by: BluShadow on February 7, 2012 12:04
    addition of {noformat}
    {noformat} tags.  Please read {message:id=9360002} and learn to do this yourself in future.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

    903830 wrote:

    I write a procedure that dynamically build the query data and insert in the table "dq_summary".
    enforcement procedure with success and data not inserted into the table 'dq_summary '.

    I'm sorry. But there is no kind of say that way. The code is undesirable. The approach is wrong. This will not happen. This will cause the fragmentation of memory in the shared Pool. This will lead to another session being impossible to analyze the sliders because of the fragmented memory.

    Not only that. The underlying data model is questionable.

    All this seems a candidate perfect as an example of how NOT to design and code and use Oracle.

  • Need help with the query. Help, please

    Hey everyone, need your help.  Thank you in advance.  In my view, there is function Pivot.  Just do not know how to use this function.  I have the query that works.  The result is:

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 dental plan pre-tax amount 29,65

    11-111-1111 Vlad 16505 01/04/2013 dental pre-tax 5 August 13 Plan level EE + SP

    11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Option TOP dental plan

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax dental care plan pay the value

    11-111-1111 16505 Vlad 01/04/2013 dental pre-tax 5 August 13 Plan period Type

    11-111-1111 Vlad 16505 01/04/2013 amount pre-tax medical Plan of 5 August 13 149

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Medical Plan level EE + SP

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan medical Option MED

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 plan pre-tax pay value

    11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Medical Plan period Type

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan PPO medical Plan

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 Vision Plan amount 5.94 pre-tax

    But I need the result to be

    Amount of SSN ID name item level Option PayValue period Type

    11-111-1111 Vlad 16505 01/04/2013 null null high of 5 August 13 pre-tax Dental Plan 29,65 EE + SP

    11-111-1111 Vlad 16505 01/04/2013 null null MED 5 August 13 149 medical plan pre-tax EE + SP

    11-111-1111 Vlad 16505 01/04/2013 Vision Plan before taxes of 5 August 13

    Select distinct
    ' 11-111-1111 "as ssn,
    WOMEN'S WEAR. Employee_number,
    "Vlad" as EMPLOYEE_FULL_NAME,
    TO_CHAR (papf.start_date, "MM/DD/YYYY") as Date_Of_Hire
    a.effective_start_date,
    PETF.element_name,
    pivf. Name,
    peevf.screen_entry_value

    Of
    PER_all_PEOPLE_F women's wear
    per_assignments_f A
    pay_element_types_f petf
    pay_element_links_f pelf
    PAY_ELEMENT_ENTRIES_F penf
    PAY_ELEMENT_ENTRY_VALUES_F peevf
    pay_input_values_f pivf
    WHERE
    PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
    and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
    and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
    and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
    and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
    and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
    and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
    AND papf.employee_number IS NOT NULL
    AND A.assignment_type = 'E '.
    AND A.person_id = papf.person_id
    and papf.effective_end_date > sysdate
    and a.effective_end_date > sysdate
    and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
    and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
    and a.assignment_id = 42643
    and a.assignment_status_type_id = '1'
    order of petf.element_name;

    Change with your query

    SELECT * FROM (select distinct)

    ' 11-111-1111 "as ssn,

    WOMEN'S WEAR. Employee_number,

    "Vlad" as employee_full_name,

    TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire

    a.effective_start_date,

    PETF.element_name,

    pivf. Name,

    peevf.screen_entry_value

    Of

    PER_all_PEOPLE_F women's wear

    per_assignments_f A

    pay_element_types_f petf

    pay_element_links_f pelf

    PAY_ELEMENT_ENTRIES_F penf

    PAY_ELEMENT_ENTRY_VALUES_F peevf

    pay_input_values_f pivf

    WHERE

    PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")

    and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID

    and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)

    and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)

    and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)

    and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID

    and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID

    AND papf.employee_number IS NOT NULL

    AND A.assignment_type = 'E '.

    AND A.person_id = papf.person_id

    and papf.effective_end_date > sysdate

    and a.effective_end_date > sysdate

    and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)

    and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)

    and a.assignment_id = 42643

    and a.assignment_status_type_id = '1')

    PIVOT (MAX (screen_entry_value) FOR (name) TO ("Amount" AS 'Amount', 'level' AS 'level', 'Option High' AS 'High Option', 'Pay the value' AS 'Value to pay', 'Period of Type' AS 'Type period'))

    order by element_name;

    (GOLD)

    SELECT ssn,

    Employee_number,

    employee_full_name,

    date_of_hire,

    effective_start_date,

    element_name,

    Max (decode (Name, 'Amount', screen_entry_value)) 'amount. "

    Max (decode (Name, 'Level', screen_entry_value)) 'level ',.

    MAX (DECODE (name, "High Option", screen_entry_value)) "High Option",

    MAX (DECODE (name, 'Value of pay', screen_entry_value)) 'value of pay. "

    MAX (DECODE (name, 'Period Type', screen_entry_value)) 'period of Type '.

    FROM (select distinct)

    ' 11-111-1111 "as ssn,

    WOMEN'S WEAR. Employee_number,

    "Vlad" as employee_full_name,

    TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire

    a.effective_start_date,

    PETF.element_name,

    pivf. Name,

    peevf.screen_entry_value

    Of

    PER_all_PEOPLE_F women's wear

    per_assignments_f A

    pay_element_types_f petf

    pay_element_links_f pelf

    PAY_ELEMENT_ENTRIES_F penf

    PAY_ELEMENT_ENTRY_VALUES_F peevf

    pay_input_values_f pivf

    WHERE

    PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")

    and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID

    and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)

    and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)

    and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)

    and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID

    and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID

    AND papf.employee_number IS NOT NULL

    AND A.assignment_type = 'E '.

    AND A.person_id = papf.person_id

    and papf.effective_end_date > sysdate

    and a.effective_end_date > sysdate

    and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)

    and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)

    and a.assignment_id = 42643

    and a.assignment_status_type_id = '1')

    GROUP BY ssn, employee_number, employee_full_name, date_of_hire, effective_start_date, NOM_ELEMENT

    order by element_name;

  • restrict the query\where in forms 6i

    Hello

    Is it possible to restrict the users using Query\Where in forms 6i
    Thanks in advance.

    You can set FORMS60_RESTRICT_ENTER_QUERY in the registry to TRUE to achieve this goal.

  • Several files of OAM aren't overview and cropping of the other data, help?

    If something beginning first day with muse is since then inserting HTML preview is sometimes off, and even the container box is sometimes forced to be similar as a previously installed HTML bit. I thought that this new site that I do at work could use some features of Adobe Edge animate to bearings and drop downs, and as I set it up I get errors OAM files more I have insert on the page. Extracts that I expect few already works not properly, but no, I'm getting a situation where my OAM file display correctly because of the box containing the first element that I inserted. Here are some pictures explaining the questions...Adobe.jpg

    Resolved on the cat.

  • How to indicate the query term not found using PHP server behaviors

    I try to use Dreamweaver server behaviors to a search for MySQL in a PHP environment and want to be able to tell the user that their search warrant was not found. I have a form that they fill and can use the server foundcount behavior to determine if the query data, they enter matches all records in the database (which works fine, as does to recover the data, if there is a match). But in my form, I want the fields of feedback to be uninhabited until they enter an order number and I do research. If there is a match, I have to return the data, but if there is no match, I want to send a message back saying that.
    Problem is that code PHP that includes the form being the action, if I try to set a flag to enable or disable a default condition (no error messages) to be to so display a message 'number command not found', the flag will reset when the form action is executed. I have to believe that this type of error reporting is common, so expect Dreamweaver to have this ability. Am I missing something? Thank you.

    You say that you have a record set if there are records. Just do a
    Recordset conditional that registers, the user sees records, remains a
    message no records have been found.

  • Update or merge it with the return data

    Hello, I want to do an update in a single statement, and select (I want to get the data and update a field in one of the tables involved with a timestamp), I tried this:

    merge into table t

    using (SELECT ... a long and complex select...) e

    on (e.ID = t.ID)

    When matched then

    update the value t.ts = systimestamp

    e return

    It does not work because his return does not seem possible with the merger, he did the update but I get no data...

    With an update, I tried:

    Update

    (

    SELECT...) e

    Aliens set = systimestamp

    And I get ORA-01779: cannont change a column that maps to a table of key-preserver nonkey.

    The select is complex with many paintings and selects involved and more than 20 KB of text only select... and this isn't mine so I want a solution that does not need to change the internal selection if possible.

    Any ideas? Sorry for my limited knowledge of sql :-(

    No, you did not read what I said.  The RETURNING clause allows you to capture the details of the update table.  You may not have the details of the complex query that you use to determine the return lines.  There is no way in SQL to a single statement that updates a table and queries data from other tables at the same time.

    The best you're going to manage is to update the data, turning the ID (or regardless of the primary key is appropriate) and then query the query data complex where once again the ID in your collection.  However, it's really messy.  What US the real purpose of all this?

  • I want to loop through the data from two different tables using for loop where the query should be replaced at runtime, please help me

    I have the data into two table with the structure of similar column, I want to loop through the data in these two tables

    based on some condition and runtime that I want to put the query in loop for example, the example is given, please help me

    create table ab (a number, b varchar2 (20));

    Insert into ab

    Select rownum, rownum. "" sample "

    of the double

    connect by level < = 10

    create table bc (a number, b varchar2 (20));

    Insert into BC.

    Select rownum + 1, rownum + 1 | "" sample "

    of the double

    connect by level < = 10

    declare

    l_statement varchar2 (2000);

    Boolean bool;

    Start

    bool: = true;

    If it is true, then

    l_statement: =' select * ab ';

    on the other

    l_statement: =' select * from bc';

    end if

    I'm in execute immediate l_statement - something like that, but I don't know

    loop

    dbms_output.put_line (i.a);

    end loop;

    end;

    Something like that, but this isn't a peace of the code work.

    Try this and adapt according to your needs:

    declare

    l_statement varchar2 (2000);

    c SYS_REFCURSOR;

    l_a number;

    l_b varchar2 (20);

    Boolean bool;

    Start

    bool: = true;

    If it is true, then

    l_statement: = "select a, b, AB;

    on the other

    l_statement: = "select a, b from bc;

    end if;

    --

    Open c for l_statement;

    --

    loop

    extract the c in l_a, l_b;

    When the output c % notfound;

    dbms_output.put_line (l_a |') -' || l_b);

    end loop;

    close c;

    end;

    /

  • Get help conditional select stmt of the query

    Hi I have the following data set
    with t as 
    (
     select 100 s_id ,'abc' txt,'SH' r_cd,'Z+' symb,1 rt_id,11 m_id ,111 i_id ,'GO' sub_cd,'PC' cd from dual union all
     select 200 s_id,'bcd','SH','Z+',2,22,222 ,'A','PC' from dual union all
     select 200 ,'bcd','SH','Z+',2,22,222 ,'B','AN' from dual
    )     
    select * from t
    Now IF cd = PC for unique s_id then post it, so s_id = 100 should be displayed
    If cd = PC as well as the PC <>for unique s_id then show only the record that is <>PC (YEAR), means s_id = cd = 200 YEAR should be displayed
    Expected results
    s_id =100 with cd = CP 
    s_id=200 with cd =AN
    Could you please help me to achieve this

    If I understand correctly, if there is only one row with PC = cd you want to return this line. If there is a line with cd = 'PC' and another line for the s_id even with a value of different CDs, you want to return this another line. If there is no line with PC = cd then ignore this identifier.

    If this is correct, then something in the sense of:

    SQL> with t as (
      2     select 100 s_id ,'abc' txt,'SH' r_cd,'Z+' symb,
      3            1 rt_id,11 m_id ,111 i_id ,'GO' sub_cd,'PC' cd
      4     from dual union all
      5     select 200 s_id,'bcd','SH','Z+',2,22,222 ,'A','PC' from dual union all
      6     select 200,'bcd','SH','Z+',2,22,222 ,'B','AN' from dual union all
      7     select 300, 'bcd','SH','Z+',2,22,222 ,'B','AN' from dual )
      8  select s_id, txt, r_cd, symb, rt_id, m_id, i_id, sub_cd, cd
      9  from (select t.*,  -- or whatever columns you want
     10               COUNT(DECODE(cd, 'PC', 1, null)) OVER(PARTITION BY  s_id)     AS pc_cnt,
     11               ROW_NUMBER() OVER(PARTITION BY s_id order by DECODE(cd, 'PC', 2, 1)) rn
     12        FROM t)
     13  WHERE rn = 1 and
     14        pc_cnt =1;
    
          S_ID TXT R_ SY      RT_ID       M_ID       I_ID SU CD
    ---------- --- -- -- ---------- ---------- ---------- -- --
           100 abc SH Z+          1         11        111 GO PC
           200 bcd SH Z+          2         22        222 B  AN
    

    If ther could be more than two lines for a s_id, then you can adjust the order by in the row_number() to choose a. If you want several lines for the case where there is a PC line and several lines not PC, then something like:

    SQL> with t as (
      2     select 100 s_id ,'abc' txt,'SH' r_cd,'Z+' symb,
      3            1 rt_id,11 m_id ,111 i_id ,'GO' sub_cd,'PC' cd
      4     from dual union all
      5     select 200 s_id,'bcd','SH','Z+',2,22,222 ,'A','PC' from dual union all
      6     select 200,'bcd','SH','Z+',2,22,222 ,'B','AN' from dual union all
      7     select 200,'bcd','SH','Z+',2,22,222 ,'B','AC' from dual union all
      8     select 300, 'bcd','SH','Z+',2,22,222 ,'B','AN' from dual )
      9  select s_id, txt, r_cd, symb, rt_id, m_id, i_id, sub_cd, cd
     10  from (select t.*,  -- or whatever columns you want
     11               COUNT(DECODE(cd, 'PC', 1, null)) OVER(PARTITION BY  s_id)     AS pc_cnt,
     12               ROW_NUMBER() OVER(PARTITION BY s_id order by DECODE(cd, 'PC', 2, 1)) rn
     13        FROM t)
     14  WHERE      pc_cnt = 1 and
     15        CASE WHEN cd = 'PC' and rn = 1 THEN 'Y'
     16             WHEN cd <> 'PC' and rn > 1 THEN 'N'
     17             ELSE 'Y' END = 'Y';
    
          S_ID TXT R_ SY      RT_ID       M_ID       I_ID SU CD
    ---------- --- -- -- ---------- ---------- ---------- -- --
           100 abc SH Z+          1         11        111 GO PC
           200 bcd SH Z+          2         22        222 B  AN
           200 bcd SH Z+          2         22        222 A  PC
    

    John

  • Issue using the AND condition in the query.

    Hi all

    I need to fetch the data of a table single app_reqm based on the conditions that send me all these APP_REQ_ID where class symbol is MBA, BTECH, BBA, SENIOR SECONDARY AND SECONDARY with AND condition (i.e. When class_symbol matches all).

    I wrote a query, but it does not give the correct data, as when I run for the MBA, BBA, BTECH symbol only for condition AND class then returns the data for the other symbol of the class also-

    SELECT APP_REQ_ID FROM app_req where APP_REQ_ID in
    (SELECT APP_REQ_ID FROM app_req WHERE class_symbol = 'MBA')
    and APP_REQ_ID in
    (SELECT APP_REQ_ID FROM app_req WHERE class_symbol = "BTECH")
    and APP_REQ_ID in
    (SELECT APP_REQ_ID FROM app_req WHERE class_symbol = "BBA")
    and APP_REQ_ID in
    (SELECT APP_REQ_ID FROM app_req WHERE class_symbol = 'MAIN SCHOOL')
    and APP_REQ_ID in
    (SELECT APP_REQ_ID FROM app_req WHERE class_symbol = "SECONDARY")

    Kindly help me.
    Thanks in advance for your time and look forward to it.

    This should do what you want:

    SELECT b.app_req_id,
           b.student_name
    FROM  (SELECT a.app_req_id,
                  a.student_name,
                  COUNT(DISTINCT a.class_symbol) OVER (PARTITION BY student_name) cnt
           FROM   app_req a
           WHERE  a.class_symbol IN ('MBA', 'BTECH', 'BBA', 'SENIOR SECONDARY', 'SECONDARY')) b
    WHERE  b.cnt = 5
    
  • need help with the query: (Frank)

    Hi Frank,.

    I have the oracle 8 database.
    I have the following table with the data structure:

    TABLE P (PID, PNAME, COLOR)
    PRIMARY KEY: PID
    PID     Pname    Color
    p1...........          Red
    P2.................    green
    p3...............       blue
    p4 ..................   red
    CAT TABLE (SID, PID, COST)
    KEY: SID MAIN + PID
    KEY REFERENCE: SID REFERENCE S.SID
    PID P.PID REFERENCES
    Sid        Pid       COst....
    S1        P1
    S2        P2
    S3        P3
    S1      P4
    S2       p1
    S3        p1
    now my question is:
    I want to find the SIDS of suppliers that provide a red and a green part

    for this I used the following query, but it gives the result worng...

    can help me please in this way to get this...
    select sid from cat
    where pid in (select pid from p where color= 'red'
              union
              select pid from p where color = 'gren') 
    group by sid having count(pid) >= 2

    Hello

    Jojo wrote:
    Hi Frank,.

    It would be nice if someone with a different name helped you? Do not restrict yourself by addressing someone in particular in your subject line or the first message.

    I have the oracle 8 database.
    I have the following table with the data structure:

    TABLE P (PID, PNAME, COLOR)
    PRIMARY KEY: PID

    PID     Pname    Color
    p1...........          Red
    P2.................    green
    p3...............       blue
    p4 ..................   red
    

    CAT TABLE (SID, PID, COST)
    KEY: SID MAIN + PID
    KEY REFERENCE: SID REFERENCE S.SID
    PID P.PID REFERENCES

    Sid        Pid       COst....
    S1        P1
    S2        P2
    S3        P3
    S1      P4
    S2       p1
    S3        p1
    

    now my question is:
    I want to find the SIDS of suppliers that provide a red and a green part

    for this I used the following query, but it gives the result worng...

    can help me please in this way to get this...

    select sid from cat
    where pid in (select pid from p where color= 'red'
              union
              select pid from p where color = 'gren')
    group by sid having count(pid) >= 2
    

    You don't like what is the number of lines with a pid; If you had 20 rows all said 'p1', that would be good enough. The number of distinct colors of the whole ('red', 'green') is what is important.
    Avoid the UNION when there is a simple alternative. The UNION tends to be slow.

    You can use a join instead, like this:

    SELECT       c.sid
    FROM       cat     c
    ,            p
    WHERE       p.pid          = c.pid
    AND       p.color       IN ('red', 'green')
    GROUP BY  c.sid
    HAVING       COUNT (DISTINCT p.color)     = 2
    ;
    

    If you would care to post CREATE TABLE and INSERT statements for the sample data and the results that you want from this data, then I could test this.

    Published by: Frank Kulash, January 10, 2011 15:25

  • Dates of the query

    Hello

    Need to write this query in an optimized way:

    For example:
    create table ff(a number, ff varchar2(10), rang_d date, ncc_i char(1));
    
    select * from ff
    
    A                      FF         RANG_D                    NCC_I 
    ---------------------- ---------- ------------------------- ----- 
    1                      f1         10-DEC-08                 N     
    2                      f1         11-DEC-08                 N     
    2                      f1         12-DEC-08                 N     
    3                      f2         09-DEC-08                 N     
    3                      f2         10-DEC-08                 N     
    5                      f1         09-DEC-08                 Y     
    6                      f2         10-DEC-08                 Y     
    
    6 rows selected
    
    
    create table fff(a number, ff varchar2(10), fbd date, trd_c varchar2(10));
    
    select * from fff
    
    A                      FF         FBD                       TRD_C      
    ---------------------- ---------- ------------------------- ---------- 
    1                      f1         10-DEC-08                 TRC        
    in these conditions:

    -Need to extract a row of the table "fff" current date is 9 December 08 ', if the folder does not exist need to check a table 'ff'... to get the next valid date or the minimum date for the funds 'f1' i.e. ' 10-DEC-08

    -Si TRD_C = 'TRC' then select the records in table 'ff' where NCC_I = 'Y '.

    Published by: user8650395 on November 21, 2009 07:03

    persistent on mode
    Let's see if I got it this time ;)

    What:

    HOEK%xe> select * from fff;
    
             A FF         FBD        TRD_C
    ---------- ---------- ---------- ----------
             1 f1         10-12-2008 TRC
             2 f1         11-12-2008 TRC
             3 f1         09-12-2008 TRE
    
    HOEK%xe> select * from ff;
    
             A FF         RANG_D     N
    ---------- ---------- ---------- -
             1 f1         10-12-2008 N
             2 f1         11-12-2008 N
             3 f1         12-12-2008 N
             4 f1         09-12-2008 Y
    
    HOEK%xe> select f.a
      2  ,      f.ff
      3  ,      f.fbd
      4  ,      f.trd_c
      5  ,      f2.ncc_i
      6  from   fff f
      7  ,    ( select ff
      8         ,      ncc_i
      9         ,      min(rang_d) rang_d
     10         from   ff
     11         where  rang_d >= to_date('09-12-2008','dd-mm-yyyy')-- your curr. business cycle date
     12         group  by ff
     13         ,         ncc_i
     14       ) f2
     15  where  f2.ff = f.ff
     16  and    f2.rang_d = f.fbd
     17  and case
     18        when f.trd_c  = 'TRC' and f2.ncc_i = 'N' then 1
     19        when f.trd_c != 'TRC' and f2.ncc_i = 'Y' then 1
     20      end = 1
     21  order by a;
    
             A FF         FBD        TRD_C      N
    ---------- ---------- ---------- ---------- -
             1 f1         10-12-2008 TRC        N
             3 f1         09-12-2008 TRE        Y
    

    So too this isn't meet your requirement, so let us know what you yourself tried sofar (post some examples of query got stuck you on and explain why you're stuck) and that you use the CASE has been a help to you.

  • Can't understand why this query returns multiple lines with the same data

    Hi all
    I am a relative novice and self-taught when it comes to SQL. I wrote a query to our reporting tool that returns multiple rows, and I can't understand why. I know that I can use the SELECT DISTINCT option, but it really slows the execution when I do. I'd really rather understand if I can change the code to avoid the multiples. This is the query. I've included a few statements in italics to help explain the break. Any ideas?

    SELECT MATSITE, MATPONUM, FIRSTRECPTDATE
    Of
    Subquery that concludes the first date on which purchase orders have been implemented with ACK State
    (SELECT ACKSTAT. PONUM AS 'ACKPONUM', (MIN (ACKSTAT. CHANGEDATE)) AS 'FIRSTACKDATE '.
    OF PZMAX. POSTATUS ACKSTAT
    WHERE (ACKSTAT. STATE = 'ACK') AND (ACKSTAT.ORGID ='CGSALTUS)
    GROUP OF ACKSTAT. PONUM),
    Subquery that concludes the first reception against a purchase order transaction for purposes of comparison
    (SELECT TRANS. PONUM AS "MATPONUM", TRANS. SITEID AS 'MATSITE', (MIN (TRANS. TRANSDATE)) AS 'FIRSTRECPTDATE '.
    OF PZMAX. MATRECTRANS TRANS
    WHERE (TRANS.ORGID ='CGSALTUS) AND (TRANS. HOUR > =: startDate and TRANS. TRANSDATE < =: endDate)
    TRANS GROUP. SITEID, TRANS. PONUM)
    WHERE
    (ACKPONUM = MATPONUM AND FIRSTRECPTDATE < FIRSTACKDATE) OR (NOT EXISTS (SELECT 1 FROM PZMAX. POSTATUS ACKSTAT2 WHERE (ACKSTAT2. PONUM = MATPONUM) AND (ACKSTAT2. STATE = 'ACK') AND (ACKSTAT2.ORGID ='CGSALTUS)))

    The where the instruction is intended to find when one of two conditions exists. ((1) received happened before the command either in ACK or 2) a reception that's happened, but the purchase order is never in ACK State. It seems that this second condition that creates multiple lines.

    Any thoughts will be appreciated geratly.

    Dave Teece
  • Absent from the report data query

    is the SQL code for the query: SELECT AD_Pref_none_pref_Arch_Domain. [Organizational units (uses)], AD_Pref_none_pref_Arch_Domain.Name, Count (AD_Pref_none_pref_Arch_Domain. [Standard and Regulation_Category]) AS CountOfPref, AS Count (AD_Pref_none_pref_Arch_Domain.Name) CountOfPNP, [CountOfPref] / [CountofPNP] AS PercentPref, ([CountofPNP]-[CountOfPref]) / [CountofPNP] AS RatioPNP
    OF AD_Pref_none_pref_Arch_Domain
    GROUP OF AD_Pref_none_pref_Arch_Domain. [Organizational units (uses)] AD_Pref_none_pref_Arch_Domain.name, [CountOfPref] / [CountofPNP], ([CountofPNP]-[CountOfPref]) / [CountofPNP]
    HAVE (((AD_Pref_none_pref_Arch_Domain. [Organizational units (uses)]) like "E S SF" & "

    Q3Z get AD sum of Total P N P1
    E S SF 1.1 data management 0 11 0.00% 100.00%
    E S SF 1.1.4 staff Master Data Management 0 1 0.00% 100.00%
    E S SF 1.1.5 product Data Management 6 9 66.67% 33.33%

    and the show report

    R3Z get AD total P N p
    E S SF 1.1 data management 0.00% 100.00%
    E S SF 1.1.4 staff Master Data Management 0.00% 100.00%
    E S SF 1.1.5 product Data Management 66.67% 33.33%

    Now I don't know what else to do. I have already created several queries to if I can find the problem, that the data displayed on the query, but once I have create the query report data is missing only the count data. Thanks for your help anyone.

    Hello

    SQL is not supported in these forums. I suggest you ask again in the dedicated Microsoft here SQL instance:

    http://social.technet.microsoft.com/Forums/en-us/category/sqlserver .

    Thank you.    :)

  • Need help to build the query/pl-sql block to get the query result and the name of column from DB table in the form of key-value pairs.

    Hi Experts,

    I have a DB table has columns of more than 50.

    I question this table, it should only return one line at any time. as sqldeveloper below image.

    here, I need to build block pl/sql-query, Discover the column in the table as a key and query result as values.

    Eg:     Key                         -  Value

    TASK_EVENT_ID - 1765

    EVENT_TYPE - ASR_UPDATE

    ... etc until all of the columns in my table.

    Experts please comment on that point, appreciate your help on this.

    Thank you

    -Vincent.

    Here is an approach using DBMS_SQL to iterate over the columns of key / value to assign... (Little code snipped for brevity)

    create or replace procedure (task_expired)

    v_store_id in full,

    v_task_action_id in full,

    v_job_id in full

    )

    as

    -[SNIP code...]

    v_sql VARCHAR2 (4000): = ' select * from my_table where PK = 123';  -Your SQL here!

    v_v_val VARCHAR2 (4000);

    v_n_val NUMBER;

    v_d_val DATE;

    v_ret NUMBER;

    c NUMBER;

    d NUMBER;

    col_cnt INTEGER.

    f BOOLEAN;

    rec_tab DBMS_SQL. DESC_TAB;

    col_num NUMBER;

    vAsString VARCHAR2 (4000);

    BEGIN

    -[SNIP code...]

    Message_properties. CORRELATION: = "EDF_EVENT";

    MSG: = SYS. AQ$ _JMS_BYTES_MESSAGE. Construct();

    Msg.set_string_property ('queueName', ' shipping/csi_cth');

    Msg.set_string_property ('MODE', 'CR8');

    c: = DBMS_SQL. OPEN_CURSOR;

    DBMS_SQL. PARSE (c, v_sql, DBMS_SQL. NATIVE);

    d: = DBMS_SQL. Execute (c);

    DBMS_SQL. DESCRIBE_COLUMNS (c, col_cnt, rec_tab);

    1.col_cnt J

    LOOP

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_n_val);      -Number

    WHEN 12 CAN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_d_val);      -Date

    ON THE OTHER

    DBMS_SQL. DEFINE_COLUMN (c, j, v_v_val, 2000);   -Else treat as varchar2

    END CASE;

    END LOOP;

    LOOP

    v_ret: = DBMS_SQL. FETCH_ROWS (c);

    WHEN OUTPUT v_ret = 0;

    1.col_cnt J

    LOOP

    -Fetch each column to the correct data type based on coltype

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. COLUMN_VALUE (c, j, v_n_val);

    vAsString: = to_char (v_n_val);

    WHEN 12 CAN

    DBMS_SQL. COLUMN_VALUE (c, j, v_d_val);

    vAsString: = to_char (v_d_val, ' DD/MM/YYYY HH24:MI:SS');

    ON THE OTHER

    DBMS_SQL. COLUMN_VALUE (c, j, v_v_val);

    vAsString: = v_v_val;

    END CASE;

    Msg.set_string_property (rec_tab (j) .col_name, vAsString);

    END LOOP;

    END LOOP;

    DBMS_SQL. CLOSE_CURSOR (c);

    DBMS_AQ. ENQUEUE (queue_name-online 'cbus.aqjms_common',

    Enqueue_options => Enqueue_options,

    Message_properties => Message_properties,

    Payload-online msg,

    Msgid => Message_handle);

    dbms_output.put_line ('00 Msgid =' |) Message_handle);

    dbms_output.put_line('===Done=');

    -[SNIP code...]

    END;

    /

Maybe you are looking for