Need help to build the SQL query

Hi all

I struggle to create the select statement.

This is my expected outcome:

Insert into tmp (aa, bb, cc, dd)

values('CUST1','10100001',2,'10-Jan-2013');

Insert into tmp (aa, bb, cc, dd)

values('CUST1','10100002',4,'11-Jan-2013');

Insert into tmp (aa, bb, cc, dd)

values('CUST2','10200001',2,'19-Jan-2013');

Insert into tmp (aa, bb, cc, dd)

values('CUST2','10200002',6,'28-Jan-2013');

Insert into tmp (aa, bb, cc, dd)

values('CUST2','10200003',1,'28-Mar-2013');

My result is:

AA COUNT (1) MAX (DD) by cc dd AA AA

CUST1 2 10100002 4 11 January 2013

CUST2 3 10200003 1 28 MARCH 2013

Can someone help with the select statement?

Thank you and best regards,

Iwan

Just change it to the column:

SELECT

AA

COUNT (aa)

, MAX (bb) keep (last dense_rank command by JJ)

, MAX (cc) keep (last dense_rank command by JJ)

MAX (dd)

OF THE TMP

GROUP BY aa

ORDER BY aa

Note that by default sort order is ascending with the last NULL values.

If in case your line-date-max will contain null value in the bb or cc columns those will be returned.

Post edited by: Correction of chris227

Post edited by: chris227 added note

Tags: Database

Similar Questions

  • Need help to write the SQL query

    Hello
    Please help me to write a query. My requirement is as below.

    Hello
    I have a table say XYZ in the following format.

    product_id local min_order_quntity
    ========================================
    1 en 10
    1 ch 10
    2 en 20
    2 ch 20
    3 en 30
    3 ch 30
    4 en 40
    4 NC 10

    Now I want to find the product_id where min_order_quantity is different for cn and locale

    now I want the result of the following

    product_id local min_order_quantity
    =============================================
    4          en          40
    4 ch 10

    This is different for local in and cn for product_id 4 min_order_quantity

    min_order_quantity should be the same for both the locale(en,ch) for any product_id.

    I want to find the product_id where min_order_quantity is different for ch and fr local

    Thank you..

    Hello

    This query should do the job

    select * from xyz t1
    where exists ( select 1 from xyz t2 where t2.product_id = t1.product_id and
                   t2.locale != t1.locale and t2.min_order_quantity != t1.min_order_quantity );
    

    See you soon

  • 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;

    /

  • Need help to build the query to group the lines

    Hello all, hope someone can help me with this. I am working in Oracle 11 g PL/SQL 11.2.0.2.0.

    I have the following query, which does exactly what I need, but I have been invited to consolidate the results in a specific way and I can't do that. First of all the existing request:
    SELECT facility_id
    ,facility_type
    ,COUNT (CASE WHEN component_type = 'ATTACH2NDPARTY' THEN 1 END) AS ATTACH2NDPARTY
    ,COUNT (CASE WHEN component_type = 'ATTACH3RDPARTY' THEN 1 END) AS ATTACH3RDPARTY
    ,COUNT (CASE WHEN component_type = 'BREAKERGT' THEN 1 END) AS BREAKERGT
    ,COUNT (CASE WHEN component_type = 'BREAKERUSS' THEN 1 END) AS BREAKERUSS
    ,COUNT (CASE WHEN component_type = 'CAPACIFORMER' THEN 1 END) AS CAPACIFORMER
    ,COUNT (CASE WHEN component_type = 'CAPACITOR' THEN 1 END) AS CAPACITOR
    ,COUNT (CASE WHEN component_type = 'CEFIBEROPTIC' THEN 1 END) AS CEFIBEROPTIC
    ,COUNT (CASE WHEN component_type = 'ELASTIDISCONN' THEN 1 END) AS ELASTIDISCONN
    ,COUNT (CASE WHEN component_type = 'NETWORKPROTECT' THEN 1 END) AS NETWORKPROTECT
    ,COUNT (CASE WHEN component_type = 'POLE' THEN 1 END) AS POLE
    ,COUNT (CASE WHEN component_type = 'REACTOR' THEN 1 END) AS REACTOR
    ,COUNT (CASE WHEN component_type = 'RECLOSER3PUPOIL' THEN 1 END) AS RECLOSER3PUPOIL
    ,COUNT (CASE WHEN component_type = 'RECLOSERTSUP' THEN 1 END) AS RECLOSERTSUP
    ,COUNT (CASE WHEN component_type = 'RECLSR3PCNTRL3A' THEN 1 END) AS RECLSR3PCNTRL3A
    ,COUNT (CASE WHEN component_type = 'RECLSR3PCNTRLNT' THEN 1 END) AS RECLSR3PCNTRLNT
    ,COUNT (CASE WHEN component_type = 'RECLSRTSCNTRL' THEN 1 END) AS RECLSRTSCNTRL
    ,COUNT (CASE WHEN component_type = 'RECTIFIERUSS' THEN 1 END) AS RECTIFIERUSS
    ,COUNT (CASE WHEN component_type = 'REGULATORJOINT' THEN 1 END) AS REGULATORJOINT
    ,COUNT (CASE WHEN component_type = 'REGULATOROH' THEN 1 END) AS REGULATOROH
    ,COUNT (CASE WHEN component_type = 'REGULATORUSS' THEN 1 END) AS REGULATORUSS
    ,COUNT (CASE WHEN component_type = 'RMS' THEN 1 END) AS RMS
    ,COUNT (CASE WHEN component_type = 'SPLICEPRIMARY' THEN 1 END) AS SPLICEPRIMARY
    ,COUNT (CASE WHEN component_type = 'STREETLIGHT' THEN 1 END) AS STREETLIGHT
    ,COUNT (CASE WHEN component_type = 'SWITCHSCADA' THEN 1 END) AS SWITCHSCADA
    ,COUNT (CASE WHEN component_type = 'TAPCHANGER' THEN 1 END) AS TAPCHANGER
    ,COUNT (CASE WHEN component_type = 'TRAFFICSIGNAL' THEN 1 END) AS TRAFFICSIGNAL
    ,COUNT (CASE WHEN component_type = 'TRANSF480' THEN 1 END) AS TRANSF480
    ,COUNT (CASE WHEN component_type = 'TRANSFGROUND' THEN 1 END) AS TRANSFGROUND
    ,COUNT (CASE WHEN component_type = 'TRANSFISOLATION' THEN 1 END) AS TRANSFISOLATION
    ,COUNT (CASE WHEN component_type = 'TRANSFOH' THEN 1 END) AS TRANSFOH
    ,COUNT (CASE WHEN component_type = 'TRANSFUG' THEN 1 END) AS TRANSFUG
    ,COUNT (CASE WHEN component_type = 'TRANSFURD' THEN 1 END) AS TRANSFURD
    ,COUNT (CASE WHEN component_type = 'TRANSFUSS' THEN 1 END) AS TRANSFUSS
    FROM (SELECT fy.id AS facility_id
          ,fy.external_facility_id
          ,fy.flty_facility_type as facility_type
          ,cp.cpty_component_type as component_type
          FROM facilities fy
          LEFT OUTER JOIN components cp
          ON cp.facility_id= fy.id
          ORDER BY fy.id DESC)
    GROUP BY facility_id, facility_type
    I was asked to do, is to display a number of facility_type who have each of the combinations of the 'DEAL' 33 areas. Installation types are strings. So an expected result would look something like:

    FACILITY_TYPE | ATTACH2NDPARTY | ATTACH3RDPARTY | BREAKERGT | ... TRANSFUSS | COUNTY
    MANHOLE X X 10
    MANHOLE X X 22
    POLE X X 5

    and so on. Because there are 33 BUSINESS areas and therefore 33! possible combinations, I was thinking about a sort of loop would achieve the desired result, but I'm stuck and could really use a shot in the arm. Any help will be appreciated and obtained points! Thanks in advance.
    select
     facility_type
    ,ATTACH2NDPARTY
    ,ATTACH3NDPARTY
    -- and so on
    , count(*) cnt
    from (
    --your query
    SELECT facility_id
    ,facility_type
    ,COUNT (CASE WHEN component_type = 'ATTACH2NDPARTY' THEN 1 END) AS ATTACH2NDPARTY
    ,COUNT (CASE WHEN component_type = 'ATTACH3RDPARTY' THEN 1 END) AS ATTACH3RDPARTY
    ,COUNT (CASE WHEN component_type = 'BREAKERGT' THEN 1 END) AS BREAKERGT
    ...
    )
    group by
     facility_type
    ,ATTACH2NDPARTY
    ,ATTACH3NDPARTY
    -- and so on like in projection
    
  • Need help to build the BasicDialog plugin sample

    Hello world

    I'm new to the Adobe ID SDK plugin development. I tried to build the sample BasicDialog but ended with the following error, could you please help me to generate the plug-in.

    Error108("' error MSB3073: the command 'merge_res.cmd'... \objd\BasicDialog\ ' BscDlg & echo removing old resources... & del/f/s ' C:\id7sdk\build\win\objd\BasicDialog\(BasicDialog Resources) \"*.idrc >... \objd\BasicDialog\prevDeletedResources.txt & xcopy/q/e '... \objd\BasicDialog\"*.idrc ' C:\id7sdk\build\win\objd\BasicDialog\(BasicDialog Resources)------" / y & del/f/s ' C:\id7sdk\build\win\objd\BasicDialog\(BasicDialog Resources) \"*.idrc & xcopy/e '... \objd\BasicDialog\\"*.idrc ' C:\id7sdk\build\win\objd\BasicDialog\(BasicDialog Resources)------"/ y»»»
    ': VCEnd ' came out with code 4.C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets1086BasicDialog

    I use vs2010 and let me know if you need any additional details to me.

    Thank you

    Momentum

    Hi momentum,

    If you use the SDK to InDesign CS5, CS5.5, you use vs2008 and if you use the SDK for InDesign CS6 you must use vs2010.

    Markus

  • Need help to write a SQL query complex

    I have the source tabe as below

    -> SOURCE_TABLE
    NAME     CUST_ID     SVC_ST_DT     SVC_END_DT 
    TOM        1               31/08/2009      23/03/2011 
    DOCK       2               01/01/2004      31/05/2010 
    HARRY      3               28/02/2007      31/12/2009 
    I want to load as target table below
    -> TARGET_TABLE
    NAME     CUST_ID                     SVC_ST_DT      SVC_END_DT 
    TOM      1           31/08/2009      31/12/2009 
    TOM      1           01/01/2010      31/12/2010 
    TOM      1           01/01/2011      23/03/2011 
    DOCK      2           01/01/2004      31/12/2004 
    DOCK      2           01/01/2005      31/12/2005 
    DOCK      2           01/01/2006      31/12/2006 
    DOCK      2           01/01/2007      31/12/2007 
    DOCK      2           01/01/2008      31/12/2008 
    DOCK      2           01/01/2009      31/12/2009 
    DOCK      2           01/01/2010      31/05/2010 
    HARRY      3           28/02/2007      31/12/2007 
    HARRY      3           01/01/2008      31/12/2008 
    HARRY      3           01/01/2009      31/12/2009 
    Is it possible to write a SQL query that returns the data in the same way above the target table.

    Published by: AChatterjee on April 30, 2012 07:14

    Published by: AChatterjee on April 30, 2012 07:14

    Or like this...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select 'TOM' as NAME, 1 as CUST_ID, date '2009-08-31' as SVC_ST_DT, date '2011-03-23' as SVC_END_DT from dual union all
      2             select 'DOCK', 2, date '2004-01-01', date '2010-05-31' from dual union all
      3             select 'HARRY', 3, date '2007-02-28', date '2009-12-31' from dual)
      4  --
      5  -- end of test data
      6  --
      7  select name, cust_id, svc_st_dt, svc_end_dt
      8  from (
      9        select name
     10              ,cust_id
     11              ,greatest(svc_st_dt, add_months(trunc(svc_st_dt,'YYYY'),yr*12)) as svc_st_dt
     12              ,least(svc_end_dt, add_months(trunc(svc_st_dt,'YYYY'),(yr+1)*12)-1) as svc_end_dt
     13        from t
     14             cross join (select rownum-1 as yr
     15                         from   dual
     16                         connect by rownum <= (select extract(year from max(svc_end_dt)) - extract(year from min(svc_st_dt)) + 1 from t)
     17                        )
     18       )
     19  where svc_st_dt <= svc_end_dt
     20* order by 2, 3
    SQL> /
    
    NAME     CUST_ID SVC_ST_DT            SVC_END_DT
    ----- ---------- -------------------- --------------------
    TOM            1 31-AUG-2009 00:00:00 31-DEC-2009 00:00:00
    TOM            1 01-JAN-2010 00:00:00 31-DEC-2010 00:00:00
    TOM            1 01-JAN-2011 00:00:00 23-MAR-2011 00:00:00
    DOCK           2 01-JAN-2004 00:00:00 31-DEC-2004 00:00:00
    DOCK           2 01-JAN-2005 00:00:00 31-DEC-2005 00:00:00
    DOCK           2 01-JAN-2006 00:00:00 31-DEC-2006 00:00:00
    DOCK           2 01-JAN-2007 00:00:00 31-DEC-2007 00:00:00
    DOCK           2 01-JAN-2008 00:00:00 31-DEC-2008 00:00:00
    DOCK           2 01-JAN-2009 00:00:00 31-DEC-2009 00:00:00
    DOCK           2 01-JAN-2010 00:00:00 31-MAY-2010 00:00:00
    HARRY          3 28-FEB-2007 00:00:00 31-DEC-2007 00:00:00
    HARRY          3 01-JAN-2008 00:00:00 31-DEC-2008 00:00:00
    HARRY          3 01-JAN-2009 00:00:00 31-DEC-2009 00:00:00
    
    13 rows selected.
    
  • Need help to convert a sql query to a stored procedure

    I have a task to convert a script sql in a stored precedure. The sql with different pr_id test_type are below. And the stored procedure must accept a pr_id and a test_type as an argument. Test type will be 3 for ATI and 2 for TI.  It must return a single value, the greatest distance calculated to fill the value of tap.  It is possible that they will pass a prid, test_type combination that will have all the records in the dsltdr table.

    {
    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464529581
    AND test_type = 3
    ORDER BY pw CSA;
    }



    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464528353
    AND test_type = 3
    ORDER BY pw CSA;


    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464527142
    AND test_type = 2
    ORDER BY pw CSA;


    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464528984
    AND test_type = 2
    ORDER BY pw CSA;



    >

    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464529918
    AND test_type = 2
    ORDER BY pw CSA;

    How to start with?

    Thanks in advance.

    I removed the dbms_output statement because it does not seem to turn it on. If you do this from sqlplus
    Make sure that you type "serverout on" and press return.

    declare
    vPW number := 0 ;
    vDistance number := 0 ;
    begin
    sp_dsl_test(3,'12345',vDistance,vPW) ;
    DBMS_OUTPUT.PUT_LINE('Distance = '|| vDistance || ' PW = '|| vPW);
    end;
    

    Published by: FlyingGuy on March 3, 2011 14:24

  • need help to build the hierarchy of sql

    Hi all

    I have 2 tables.
    create table apc_menus ( menu_id number
                            ,menu_code varchar2(30) not null
                            ,menu_name varchar2(100) not null
                            ,description varchar2(250) );
    
    create table apc_menu_details ( menu_id number
                                   ,sequence_no number not null
                                   ,sub_menu_id number );
    
    insert into apc_menus ( 3, 'SUBMENU1, 'Sub-Menu1 of Menu 1');
    insert into apc_menu_details ( 3, 10, NULL);
    
    insert into apc_menus ( 2, 'MENU1, 'Menu 1');
    insert into apc_menu_details ( 2, 10, 3);
    
    insert into apc_menus ( 5, 'SUBMENU1', 'Sub-Menu1 of Menu 2');
    insert into apc_menu_details ( 5, 10, NULL);
    
    insert into apc_menus ( 4, 'MENU2, 'Menu 2');
    insert into apc_menu_details ( 4, 10, 5);
    
    insert into apc_menus ( 1, 'MAIN', 'Main Menu');
    insert into apc_menu_details ( 1, 10, 2);
    insert into apc_menu_details ( 1, 20, 4);
    I am building an application where in it shows as...
    Menu 1
      Sub-Menu1 of Menu1
    
    Menu 2
      Sub-Menu1 of Menu2
    I would be grateful the help...

    Thank you.
    Allen

    You did not show the expected results:

    select  lpad(' ',(level - 1) * 2) || menu_code || nvl2(prior menu_code,' of ' || prior menu_code,null) menu_code
      from  apc_menus m,
            apc_menu_details d
      where d.menu_id = m.menu_id
      start with d.menu_id not in (select sub_menu_id from apc_menu_details where sub_menu_id is not null)
      connect by d.menu_id = prior d.sub_menu_id
    /
    MENU_CODE
    ----------------------------------
    MAIN
      MENU2 of MAIN
        MENU3 of MENU2
          MENU4 of MENU3
            MENU5 of MENU4
    
    SQL> 
    

    SY.
    P.S. Your sample missing citations and clause VALUES. Next time post work set out.

  • Need help to build the query

    Hello

    I got the info from a table named X.

    Col1 Col2 Col3 Col4
    IT01 20 01 X
    IT01 20 50 X
    IT01 30 01 X
    IT01 30 50 X

    If the value of "X" all 4 combinations of Col2 and Col3, Col4 the result should be displayed as

    Col1, Col4
    IT01 X

    If the actual values are as
    Col1 Col2 Col3 Col4
    IT01 20 01 X
    IT01 20 50 X
    IT01 30 01 X
    30 50 IT01 Y

    The result should be displayed as

    Col1, Col4
    IT01 Y

    How will generate the query for this?

    Thank you
    Thomas Titus

    Published by: Titus Thomas on April 20, 2010 14:48

    Try this:

    with test_tab as (select 'ITO1' Col1, 20 Col2, 01 Col3, 'X' Col4 from dual union all
                      select 'ITO1' Col1, 20 Col2, 50 Col3, 'X' Col4 from dual union all
                      select 'ITO1' Col1, 30 Col2, 01 Col3, 'X' Col4 from dual union all
                      select 'ITO1' Col1, 30 Col2, 50 Col3, 'X' Col4 from dual union all
                      select 'ITO2' Col1, 20 Col2, 01 Col3, 'X' Col4 from dual union all
                      select 'ITO2' Col1, 20 Col2, 50 Col3, 'X' Col4 from dual union all
                      select 'ITO2' Col1, 30 Col2, 01 Col3, 'X' Col4 from dual union all
                      select 'ITO2' Col1, 30 Col2, 50 Col3, 'Y' Col4 from dual)
    --- end of mimicking your data; use SQL below:
    select col1, max(col4) col4
    from   test_tab
    group by col1;
    
    COL1 COL4
    ---- ----
    ITO2 Y
    ITO1 X   
    
  • Need help in writing the SQL CASE or DECODE statement

    Hi experts,

    I need to write a SQL select order_num, cntry_cde, prod_id and Qty by joining order_num on PROD_ORDER and PROD_ORDER_TXT.

    Here is my sample data

    PROD ORDER_
    order_num Prod_id Qty cntry_cde
    100 US A1 5
    101 US 10 A2
    102 IN A3 4
    103 TO THE A4 9
    104. IN A5 3

    PROD ORDER_TXT_
    cntry_cde Prod_id order_num
    100 US A1
    101 US A2
    102 NZ A3
    103 TO THE A4
    A5 104

    Here's the requirement,

    (1) if the cntry_cde in PROD_ORDER is the same as cntry_cde in PROD_ORDER_TXT then select PROD_ORDER.cntry_cde (orders 100, 101, 103)
    (2) if they are different, choose PROD_ORDER_TXT (order 102, to THE NZ <>) country code
    (3) if they are different and PROD_ORDER_TXT.cntry_cde is null, I can't use it like cntry_cde in my report (order 104). It happenend just because of bad data to the source.
    I can't avoid it. Then just use the cntry_cde of PROD_ORDER

    Expected results
    100 US A1 5
    101 US 10 A2
    102 NZ A3 4 - changed in NZ
    103 TO THE A4 9
    104. IN A5 3 - IN retained as PROD_ORDER_TXT.cntry_cde is null


    instructions for creation and insertion of sample table are lower than

    create the table prod_order
    (number of order_num,
    cntry_cde CHAR (2),
    prod_id VARCHAR2 (6).
    number of quantity)

    create the table prod_order_txt
    (number of order_num,
    cntry_cde CHAR (2),
    prod_id VARCHAR2 (6))

    insert into prod_order values (100, 'WE', 'A1', 5);
    insert into prod_order values (101, 'WE', 'A2', 1);
    insert into prod_order values (102, ' to THE ', 'A3', 4);
    insert into prod_order values (103, ' to THE ', 'A4', 9);
    insert into prod_order values (104, 'IN', 'A5', 3);



    insert into prod_order_txt values (100, 'WE', 'A1');
    insert into prod_order_txt values (101, 'WE', 'A2');
    insert into prod_order_txt values (102, 'NZ', 'A3');
    insert into prod_order_txt values (103, ' to THE ', 'A4');
    insert into prod_order_txt values (104, 'A5', NULL);

    commit;

    Thank you for your help in advance

    Published by: manon March 28, 2012 13:39
  • Need a Suggestion tune the SQL query

    Hello


    There is a view that is created that takes 17 seconds to run the query, when I split the query and run each query prend.15 seconds to run. Below the query I need to improve the performance of the code if you need your suggestion please order to tweek so this time take for execution to become less

    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_1 "product name."
    TRIM (UPPER (serial_number_1)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_1! = 'NULL '.
    AND Serial_Number_1! = 'NULL '.
    AND C.Amid = A.Amid (+)

    UNION
    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_2 "product name."
    TRIM (UPPER (serial_number_2)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_2! = 'NULL '.
    AND serial_number_2! = 'NULL '.
    AND C.Amid = A.Amid (+)
    UNION

    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_3 "product name."
    TRIM (UPPER (serial_number_3)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_3! = 'NULL '.
    AND serial_number_3! = 'NULL '.
    AND C.Amid = A.Amid (+)

    UNION
    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_4 "product name."
    TRIM (UPPER (serial_number_4)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_4! = 'NULL '.
    AND serial_number_4! = 'NULL '.
    AND C.Amid = A.Amid (+)

    UNION
    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_5 "product name."
    TRIM (UPPER (serial_number_5)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_5! = 'NULL '.
    AND serial_number_5! = 'NULL '.
    AND C.Amid = A.Amid (+)
    UNION
    SELECT "Source", "ERP"
    Imv.Bill_To 'retailer ',.
    Imv.Ship_To "dealer."
    Ship_To_City "Dealer City."
    Ship_To_State "State dealer."
    Edb2_Global.Country_Name_Theater (Imv.Ship_To_Country, 'COUNTRY') 'dealer country. "
    Edb2_Global.Country_Name_Theater (Imv.Ship_To_Country, 'THÉÂTRE') 'dealer Theater. "
    Cl.Company_Name "name of the customer."
    Cl.City "customer's city."
    Cl.State "State of the customer."
    Edb2_Global.Country_Name_Theater (cl.) Country, 'COUNTRY') 'country of the customer. "
    Edb2_Global.Country_Name_Theater (cl.) Country, 'THEATER') "customer Theater."
    Imv.So_Booked_Date "Date from the sale of book."
    Imv.Ship_Date "Delivery Date"
    Imv.Order_Number "client order number."
    Imv.Cust_Po_Number "Number"
    Cp.Item_Name "product name."
    Cp.Product_Serial_Number "serial number of product."
    To_char (Imv.contract_number) "Contract ID"
    Imv.Service_Description "name of support."
    Imv.Service_Start_Date "Start Date"
    Imv.Service_End_Date "expiration Date."
    Imv.Header_Status "header Status."
    Imv.Line_Status "status of line."
    Imv.Line_Type 'line Type. "
    Imv.Termination_Reason "reason for termination of employment."
    Cl.Location_Id 'location_id. "
    CP.ID "Product_id".
    Of ib_support_contracts_mv imv.
    customer_products cp,
    customer_locations cl
    WHERE superior (Trim (Imv.Serial_Number)) = Upper (Trim (Cp.Product_Serial_Number (+)))
    AND Cp.Location_Id = Cl.Location_Id (+)
    /


    There is a Union for each request, please suggest how tweek

    Thank you
    Sudhir

    A quick look indicates that first 5 select queries are that even accepting the 2 conditions

    WHERE controller_model_number_1! = 'NULL '.
    AND Serial_Number_1! = 'NULL '.

    Use SEPARATE and Clause OR reduce these 5 queries in one like that

    SELECT DISTINCT
               .....
               ......  
    
      FROM edb_assets       c
          ,edb_accounts     a
    WHERE  C.Amid = A.Amid(+)
      AND  (
              (     controller_model_number_1 != 'NULL'
                AND Serial_Number_1 != 'NULL'
              )
             OR
              (     controller_model_number_2 != 'NULL'
                AND Serial_Number_2 != 'NULL'
              )
             OR
              (     controller_model_number_3 != 'NULL'
                AND Serial_Number_3 != 'NULL'
              )
             OR
              (     controller_model_number_4 != 'NULL'
                AND Serial_Number_4 != 'NULL'
              )
             OR
              (     controller_model_number_5 != 'NULL'
                AND Serial_Number_5 != 'NULL'
              )
    
           )
    

    Concerning
    Arun

    Published by: Arun Kumar Gupta on May 9, 2012 12:10

  • need help to build a tree query

    Hi all

    I created a table my_menu with the ff: columns:

    menu_id
    parent_menu_id
    name
    link

    then I added a few records.

    menu_id = 1
    parent_menu_id = null
    name = system administrator
    link = some_link

    menu_id = 2
    parent_menu_id = 1
    name = objects
    link = some_link

    menu_id = 3
    parent_menu_id = 2
    name = Users
    link = some_link

    menu_id = 4
    parent_menu_id = 2
    name = roles
    link = some_link

    menu_id = 5
    parent_menu_id = null
    name = human resources
    link = some_link

    menu_id = 6
    parent_menu_id = 5
    name = employed
    link = some_link

    menu_id = 7
    parent_menu_id = 6
    name = Manage
    link = some_link

    now I am creating a query in which I want to display only the menu system administrator and its submenus but a hard time
    select 
     menu_id id
    ,web_menu_id pid
    ,name
    ,level
    ,lead(level, 1, 0) over(order by rownum) lead
      from my_menus
    where menu_id=1
    the query above won't work of course because it will only show the parent menu 1... How to reference its submenus in a query?

    Thank you.
    with t as (
               select 1 menu_id,null parent_menu_id,'System Administrator' name,'some_link' link from dual union all
               select 2,1,'Objects','some_link' from dual union all
               select 3,2,'Users','some_link' from dual union all
               select 4,2,'Roles','some_link' from dual union all
               select 5,null,'Human Resources','some_link' from dual union all
               select 6,5,'Employees','some_link' from dual union all
               select 7,6,'Manage','some_link' from dual
              )
    select  menu_id,
            parent_menu_id,
            name,
            link
      from  t
      start with name = 'System Administrator'
      connect by parent_menu_id = prior menu_id
    /
    
       MENU_ID PARENT_MENU_ID NAME                 LINK
    ---------- -------------- -------------------- ---------
             1                System Administrator some_link
             2              1 Objects              some_link
             3              2 Users                some_link
             4              2 Roles                some_link
    
    SQL> 
    

    SY.

  • Need help in the sql query

    Hi all

    Here is the sql query,

    Select papf.employee_number,

    -papf.full_name, ppa.effective_date, pp.payroll_name,

    PET.element_name,

    PIV. Name input_value,

    prrv.result_value

    -, ppa.payroll_action_id, ppa.time_period_id

    Of

    Apps.pay_payroll_actions App,

    pay_assignment_actions AAP,

    pay_payrolls_f pp,

    pay_run_results prr,

    prrv pay_run_result_values,

    pay_input_values_f piv,

    animal pay_element_types_f,

    Apps.per_all_assignments_f ADP,

    Apps.per_all_people_f women's wear

    -where ppa.payroll_action_id =: payroll_action_id - give your payroll_action_id

    where ppa.payroll_id =: payroll_id

    and ppa.payroll_action_id =: payroll_action_id

    - and paa.assignment_action_id =: assignment_action_id

    and ppa.payroll_action_id = paa.payroll_action_id

    and ppa.payroll_id = pp.payroll_id

    and paa.assignment_action_id = prr.assignment_action_id

    and prr.run_result_id = prrv.run_result_id

    and prrv.input_value_id = piv.input_value_id

    and piv.element_type_id = pet.element_type_id

    and paaf.assignment_id = paa.assignment_id

    and paaf.person_id = papf.person_id

    and trunc (sysdate) between pp.effective_start_date and pp.effective_end_date

    and trunc (sysdate) between pet.effective_start_date and pet.effective_end_date

    and trunc (sysdate) between piv.effective_start_date and piv.effective_end_date

    and trunc (sysdate) between paaf.effective_start_date and paaf.effective_end_date

    and trunc (sysdate) between papf.effective_start_date and papf.effective_end_date

    - and papf.employee_number = '1'

    - and ppa.effective_date = July 22, 2014"

    and pet.element_name in ('Local Mission allowance', "Compensation of Mission International")

    order by 1.3

    The result is:

    Employee_number Element_Name Input_Value Result_value

    1 compensation of Mission international day amount 1000

    1 compensation of international Mission Distance days 4

    1 value to pay compensation of 1200 International Mission

    1 International Mission allowance Start Date 01/01/2014

    1 compensation of Mission international day amount 800

    1 compensation of international Mission Distance days 10

    1 International Mission allowance pay value 2000

    1 International Mission allowance Start Date 01/02/2014

    1 compensation of Mission local day amount 500

    1 compensation of local Mission Distance days 10

    1 Mission allowance paid local value 1000

    1 compensation of local Mission Start Date 01/11/2014

    Desired output:

    Employee_number Element_Name Day_Amount Distance_Days Pay_Value Start_Date

    1 compensation of international Mission 1000, 1200 4 2014/01/01

    1

    International Mission allowance80010200001/02/2014
    1Mission local compensation50010100001/11/2014

    Please suggest.

    INSERT statement:

    TOGETHER TO DEFINE

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'Day amount', '1000');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'Day amount', '1000');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'number of orders', '196');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'number of orders', '195');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', ' Distance days, 4 ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', ' Distance days, 1 ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ("1', 'International Mission allowance', 'employee Category", "scale of employment medical cities");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ("1', 'International Mission allowance', 'employee Category", "scale of employment medical cities");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'Class level', 'G3');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'Class level', 'G3');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'class of employment, ' ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'class of employment, ' ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'Country of Mission', ' 3003 - Kuwait ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'Country of Mission', ' 2004 - Canada ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', ' Mission days, 4' ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', ' Mission days, 3' ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'compensation of Mission international","End of Mission Date"' 2014/07/10 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'compensation of Mission international","End of Mission Date"' 2014/07/19 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'compensation of Mission international", 'Mission Start Date', ' 2014/07/07 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'compensation of Mission international", 'Mission Start Date', ' 2014/07/17 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'value of pay', '3000');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('1', 'International Mission allowance', 'value of pay', '4000');

    00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'Local Mission allowance', 'number of orders', '45');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'Local Mission allowance', 'number of orders', "456789");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', 'local Mission allowance', ' away days '0' ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', 'local Mission allowance', ' Distance days, 1 ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', 'local Mission allowance', 'Provided food', 'Y');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'compensation of local Mission","Accommodation provided", 'Y');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'compensation of local Mission","Accommodation provided", 'Y');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', "local Mission allowance", "Mission City", "AL MEDINA");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', "local Mission allowance", "Mission City", "RIYADH");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', 'local Mission allowance', ' Mission days, 4' ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70 ', 'local Mission allowance', ' Mission days, 5' ");

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'Local Mission allowance', 'End of Mission Date' ' 2014/06/16 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'Local Mission allowance', 'End of Mission Date' ' 2014-06-14 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'Local Mission allowance', 'Mission Start Date', ' 2014/06/13 00:00:00 ');

    Insert into APPS. PER_ALL_PEOPLE_F

    (EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)

    Values

    ('70', 'Local Mission allowance', 'Mission Start Date', ' 2014/06/10 00:00:00 ');

    Thank you very much in advance.

    Kind regards

    Afzal.

    So then... something like this:

    SELECT employee_number

    element_name

    MAX (decode (input_value, 'Day amount', result_value)) AS day_amount

    MAX (decode (input_value, 'Days of Distance', result_value)) AS Distance_Days

    , MAX (decode (input_value, 'Value of pay', result_value)) AS Pay_Value

    MAX (decode (input_value, 'Start Date', result_value)) AS Start_Date

    from (SELECT papf.employee_number

    pet.element_name

    piv.NAME input_value

    prrv.result_value

    prrv.run_result_id

    OF apps.pay_payroll_actions App

    pay_assignment_actions PAA

    pay_payrolls_f pp

    pay_run_results prr

    pay_run_result_values prrv

    pay_input_values_f piv

    pay_element_types_f pet

    apps.per_all_assignments_f ADP

    apps.per_all_people_f women's wear

    -where ppa.payroll_action_id =: payroll_action_id - give your payroll_action_id

    WHERE ppa.payroll_id =: payroll_id

    AND ppa.payroll_action_id =: payroll_action_id

    - and paa.assignment_action_id =: assignment_action_id

    AND ppa.payroll_action_id = paa.payroll_action_id

    AND ppa.payroll_id = pp.payroll_id

    AND paa.assignment_action_id = prr.assignment_action_id

    AND prr.run_result_id = prrv.run_result_id

    AND prrv.input_value_id = piv.input_value_id

    AND piv.element_type_id = pet.element_type_id

    AND paaf.assignment_id = paa.assignment_id

    AND paaf.person_id = papf.person_id

    AND trunc (sysdate) BETWEEN pp.effective_start_date AND pp.effective_end_date

    AND trunc (sysdate) BETWEEN pet.effective_start_date AND pet.effective_end_date

    AND trunc (sysdate) BETWEEN piv.effective_start_date AND piv.effective_end_date

    AND trunc (sysdate) BETWEEN paaf.effective_start_date AND paaf.effective_end_date

    AND trunc (sysdate) BETWEEN papf.effective_start_date AND papf.effective_end_date

    AND pet.element_name IN ('local Mission allowance', 'International Mission'))

    GROUP BY employee_number

    element_name

    run_result_id

    ;

    You should get your desired result.

    Roger

  • Need help to reduce the cost of my sql Query below 100 to optimize.

    Can you please suggest how to reduce the COST of my SQL query? Is it possible to get the < 100 COST?

    My goal:
    ------------------------

    To recover the data of length (a column is in normalised_event: table) corresponding to each type of call (which is to extract the column rc.abbreviation in reference_code: table) in a given range of dates. So I wrote the following as SQL below:

    ------------------------------------------------------------------------------------------------------------------
    Select nvl (sum (round(ne.duration/60,3)), 0), rc.abbreviation
    Since the rc reference_code, normalised_event not
    where to_char (rc.reference_code) = ne.full_path
    and rc.reference_type_id = 505002 AND
    trunc (don't. CHARGE_START_DATE) between trunc (to_date (January 1, 2008 "," dd-mm-yyyy hh24:mi:ss'))))
    and trunc (to_date (January 1, 2009 "," dd-mm-yyyy hh24:mi:ss'))))
    Rc.abbreviation group

    -----------------------------------------------------------------------------------------------------------------

    IAM using Toad Software which is taken in charge by oracle 9i.


    Explain the plan above query is:

    explain plan
    Set statement_id = "mp01" for
    Select nvl (sum (round(ne.duration/60,3)), 0), rc.abbreviation
    Since the rc reference_code, normalised_event not
    where to_char (rc.reference_code) = ne.full_path
    and rc.reference_type_id = 505002 AND
    trunc (don't. CHARGE_START_DATE) between trunc (to_date (January 1, 2008 "," dd-mm-yyyy hh24:mi:ss'))))
    and trunc (to_date (January 1, 2009 "," dd-mm-yyyy hh24:mi:ss'))))
    Rc.abbreviation group



    Select LPad (' ', 2 *(Level-1)). Level | '.' || NVL (position 0). ' ' ||
    Operation | ' ' || Options of | ' ' || Object_name | ' ' ||
    Object_type | ' ' || Decode (id, 0, Statement_Id |) "Cost = ' | Position)
    "Query plan.
    From Plan_Table
    Start with id = 0 and Statement_Id = 'mp01 ".
    Connect Id Parent_Id = prior
    And Statement_Id = 'mp01 ";


    ---------------------------Query Plan explained is----------------------------------------------------------

    1.555 INSTRUCTION SELECT mp01 cost = 555
    2.1 TYPE GROUP
    3.1 FILTER
    4.1 HASH JOIN
    5.1 TABLE ACCESS BY INDEX ROWID REFERENCE_CODE
    6.1 INDEX RANGE SCAN NON-UNIQUE I_REFERENCE_CODE_REF_TYPE
    5.2 PARTITION RANGE ALL THE
    6.1 TABLE ACCESS FULL NORMALISED_EVENT

    -Automatic tracking of the SQl query is:.

    recursive calls - 0
    DB block gets - 0
    compatible gets - 174
    physical reads - 0
    size - 104
    Parse count (hard) - 0
    Parse count (chess) - 0
    run the County - 2
    bytes sent via SQL * Net to dblink - 0
    bytes received via SQL * Net from dblink - 0

    I was wrong. Once again, this shows how it is dangerous to assume (I assumed that FBI was new in 10g because it is when I used them), and the power of the community to ensure that accurate information is provided.

    John

  • Please help build a sql query

    Hello

    Please help build a sql query


    My Table Test2015 has given below

    Header_id Line_id Ordered_item       

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887292 SH-POS-NO-BR POS-INS

    199926 723887293 MAIN2

    199927 723887294 IC-ENV-NON-BR-ENV-PXY

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199927 723887298 PRCSS SH-FAIRY-ELEC DISTR.

    199927 723887299 SH-FAIRY-SUM PRO-DE-CONS-HOUSE

    I am trying to query my Test2015 table to obtain the records with ordered_item containing 'MAIN1' and 'MAIN2' only. I tried to write a query as below

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1', 'MAIN2');

    But it gives me all the data with the MAIN2 records found but MAIN1 is absent, I want to retrieve only records to both 'MAIN1' and 'MAIN2' present for Header_id.

    While the result below shows me header_id - 199926 and 199929 that he should assume back. I want to fetch documents only with 'MAIN1' and 'MAIN2' both present.

    Header_id Line_id Ordered_item            

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887293 MAIN2

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199929 723887299 MAIN1

    Please suggest.

    Thank you and best regards,

    Prasad.

    Hello

    Try like this...

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1") and in header_id (select test2015 WHERE ORDERED_ITEM in ('MAIN2') header_id)

Maybe you are looking for