To write a single query to get the desired output.

Hi,
I have table by name "employees" it contains 20 records meaning 20 employee details.
some of the column it contains are employee_id,Hiredate as below.
EMPLOYEE_ID|HIRE_DATE
100|6/17/1987
101|9/21/1989
102|1/13/1993
103|1/3/1993
104|5/21/1991
107|2/7/1999
124|11/16/1999
141|10/17/1995
142|1/29/1997
143|3/15/1998
144|7/9/1998
149|1/29/2000
174|5/11/1996
176|3/24/1998
178|5/24/1999
200|9/17/1987
201|2/17/1996
202|8/17/1997
205|6/7/1994
206|6/7/1994
The query should display the total number of employees, employees hired in 1995,1996,1997 and 1998 some thing like below.
output
Total     1995     1996     1997     1998
20     1     2     2     3
please let me know the query.
Thanks
Dinesh

SQL> select * from employees;
         EMPLOYEE_ID HIRE_DATE
                 100 17-JUN-1987 00:00:00
                 101 21-SEP-1989 00:00:00
                 102 13-JAN-1993 00:00:00
                 103 03-JAN-1993 00:00:00
                 104 21-MAY-1991 00:00:00
                 107 07-FEB-1999 00:00:00
                 124 16-NOV-1999 00:00:00
                 141 17-OCT-1995 00:00:00
                 142 29-JAN-1997 00:00:00
                 143 15-MAR-1998 00:00:00
                 144 09-JUL-1998 00:00:00
                 149 29-JAN-2000 00:00:00
                 174 11-MAY-1996 00:00:00
                 176 24-MAR-1998 00:00:00
                 178 24-MAY-1999 00:00:00
                 200 17-SEP-1987 00:00:00
                 201 17-FEB-1996 00:00:00
                 202 17-AUG-1997 00:00:00
                 205 07-JUN-1994 00:00:00
                 206 07-JUN-1994 00:00:00
20 rows selected.
SQL> select count(*) total_employees
  2        ,sum(case when to_char(hire_date,'YYYY') = '1995' then 1 else 0 end) hired_1995
  3        ,sum(case when to_char(hire_date,'YYYY') = '1996' then 1 else 0 end) hired_1996
  4        ,sum(case when to_char(hire_date,'YYYY') = '1997' then 1 else 0 end) hired_1997
  5        ,sum(case when to_char(hire_date,'YYYY') = '1998' then 1 else 0 end) hired_1998
  6  from   employees;
     TOTAL_EMPLOYEES           HIRED_1995           HIRED_1996           HIRED_1997           HIRED_1998
                  20                    1                    2                    2                    3Edited by: SomeoneElse on Jan 6, 2009 8:09 AM

Similar Messages

  • How to modify this query to get the desired output format

    I hv written a Query to display all the parent table names and their primary key columns(relevant to this foreign key of the child table).The query is given below...
    SELECT DISTINCT(TABLE_NAME) AS PARENT_TABLE,COLUMN_NAME AS PARENT_COLUMN
    FROM ALL_CONS_COLUMNS
    WHERE CONSTRAINT_NAME IN (SELECT AC.R_CONSTRAINT_NAME
    FROM ALL_CONSTRAINTS AC
    WHERE AC.TABLE_NAME=TABLE_NAME
    AND AC.TABLE_NAME='&TABLE'
    AND AC.R_CONSTRAINT_NAME IS NOT NULL);
    This query will display all the parent tables and their primary key columns.Now my problem is that how to modify this query to also display the foreign key column name of the child table.
    I want the query result in the following format.The query should display the following columns.1)child table's name,2)child table's foreign key column name,3)the corresponding parent table's name,4)the parent table's primary key column name(which is the foreign key in the child table).
    For Example I want the output as follows...
    TAKE THE CASE OF SCOTT.EMP(AS INPUT TO YOUR QUERY)
    CHILD_TABLE CHILD_COLUMN PARENT_TABLE PARENT_COLUMN
    EMP DEPTNO DEPT DEPTNO
    In this result I hv used alias name for the columns.The query should display this only for the foreign keys in the child table.In the query which I sent to you earlier will give the parent table and the parent column names,But I also want to append the child table and child column names there.
    any help on how to tackle would be appreciated.

    Try this query
    SELECT c.table_name child_table,
         c.column_name child_column,
         p.table_name parent_table,
         p.column_name parent_column
    FROM user_constraints a,user_constraints b,user_cons_columns c,
         user_cons_columns p
    WHERE a.r_constraint_name=b.constraint_name and
          a.constraint_name=c.constraint_name and
          b.constraint_name=p.constraint_name and
          c.position=p.position
    ORDER BY c.constraint_name,c.position
    Anwar

  • How to correctly perform a join to get the desired output

    Hi,
    I have the following query which gives me the desired output.
    SELECT
    Cast(TEDW_F_STORE_OPS_GOAL.LOCATION_NUM As Varchar) As LOCATION_NUM,
    Cast(TEDW_D_DATE_TYPE_1.DATE_SHORT As Varchar) As DATE_SHORT,
    'Store' As DRIVER_TYPE,
    'OPS GOAL' As DRIVER,
    SUM(OPS_GOAL) As ROW_TOTAL
    FROM Planning.TEDW_F_STORE_OPS_GOAL
    left outer join
    edw.Calendar.TEDW_D_DATE_TYPE_1
    ON TEDW_F_STORE_OPS_GOAL.DATE_KEY = TEDW_D_DATE_TYPE_1.DATE_KEY
    left outer join
    [EDW].[Location].[TEDW_D_LOCATION_TYPE_1_NEW]
    ON TEDW_F_STORE_OPS_GOAL.LOCATION_NUM = TEDW_D_LOCATION_TYPE_1_NEW.LOCATION_NUM
    where TEDW_D_LOCATION_TYPE_1_NEW.LOCATION_TYPE_DESC = 'FULL PRICE STORE'
    group by
    TEDW_F_STORE_OPS_GOAL.LOCATION_NUM,
    TEDW_D_DATE_TYPE_1.DATE_SHORT
    order by LOCATION_NUM desc
    , DATE_SHORT desc
    The output of the above query will be like this
    3646 2014-04-17 Store OPS GOAL 2598.00000
    3646 2014-04-16 Store OPS GOAL 2475.00000
    3646 2014-04-15 Store OPS GOAL 2497.00000
    3646 2014-04-14 Store OPS GOAL 2691.00000
    But the problem with this query is it brings back the OPS Goal for all the dates,but i have to limit the OPS goals to some particular date range which must match to this table Staging.TEDW_S_EMPOWER.So,I made the above query join to this table like this
    SELECT
    Cast(TEDW_F_STORE_OPS_GOAL.LOCATION_NUM As Varchar) As LOCATION_NUM,
    Cast(TEDW_D_DATE_TYPE_1.DATE_SHORT As Varchar) As DATE_SHORT,
    'Store' As DRIVER_TYPE,
    'OPS GOAL' As DRIVER,
    SUM(OPS_GOAL) As ROW_TOTAL
    FROM Planning.TEDW_F_STORE_OPS_GOAL
    left outer join
    edw.Calendar.TEDW_D_DATE_TYPE_1
    ON TEDW_F_STORE_OPS_GOAL.DATE_KEY = TEDW_D_DATE_TYPE_1.DATE_KEY
    left outer join
    [EDW].[Location].[TEDW_D_LOCATION_TYPE_1_NEW]
    ON TEDW_F_STORE_OPS_GOAL.LOCATION_NUM = TEDW_D_LOCATION_TYPE_1_NEW.LOCATION_NUM
    left outer join
    Staging.TEDW_S_EMPOWER
    on TEDW_D_DATE_TYPE_1.DATE_SHORT = TEDW_S_EMPOWER.DATE_SHORT
    and TEDW_F_STORE_OPS_GOAL.LOCATION_NUM = TEDW_S_EMPOWER.LOCATION_NUM
    where TEDW_D_LOCATION_TYPE_1_NEW.LOCATION_TYPE_DESC = 'FULL PRICE STORE'
    group by
    TEDW_F_STORE_OPS_GOAL.LOCATION_NUM,
    TEDW_D_DATE_TYPE_1.DATE_SHORT
    having TEDW_D_DATE_TYPE_1.DATE_SHORT between MIN(TEDW_S_EMPOWER.date_short) and MAX(TEDW_S_EMPOWER.date_short)
    order by LOCATION_NUM desc
    , DATE_SHORT desc
    Now the output of this query will be like this
    3646 2014-04-17 Store OPS GOAL 7794.00000
    3646 2014-04-16 Store OPS GOAL 7425.00000
    3646 2014-04-15 Store OPS GOAL 7491.00000
    3646 2014-04-14 Store OPS GOAL 8073.00000
    If you can observe the OPS Goal Sum values have changed drastically, i was able to limit the date range i wanted but now i dont get the correct OPS Goal values.
    Can someone please help me with this?
    Thanks

    Hi RJP,
    Yes you are absolutely correct, in the "Staging.TEDW_S_EMPOWER"
    we have 3 rows for every date and location,so i changed my second sql to be something like this
    SELECT
    Cast(TEDW_F_STORE_OPS_GOAL.LOCATION_NUM As Varchar) As LOCATION_NUM,
    Cast(TEDW_D_DATE_TYPE_1.DATE_SHORT As Varchar) As DATE_SHORT,
    'Store' As DRIVER_TYPE,
    'OPS GOAL' As DRIVER,
    SUM(OPS_GOAL) /3 As ROW_TOTAL
    FROM Planning.TEDW_F_STORE_OPS_GOAL
    cross join
    EDW.Utility.TEDW_J_PROCESS_DATE
    inner join
    edw.Calendar.TEDW_D_DATE_TYPE_1
    ON TEDW_F_STORE_OPS_GOAL.DATE_KEY = TEDW_D_DATE_TYPE_1.DATE_KEY
    left outer join
    [EDW].[Location].[TEDW_D_LOCATION_TYPE_1_NEW]
    ON TEDW_F_STORE_OPS_GOAL.LOCATION_NUM = TEDW_D_LOCATION_TYPE_1_NEW.LOCATION_NUM
    left outer join
    Staging.TEDW_S_EMPOWER
    on TEDW_D_DATE_TYPE_1.DATE_SHORT = TEDW_S_EMPOWER.DATE_SHORT
    and TEDW_F_STORE_OPS_GOAL.LOCATION_NUM = TEDW_S_EMPOWER.LOCATION_NUM
    where TEDW_D_LOCATION_TYPE_1_NEW.LOCATION_TYPE_DESC = 'FULL PRICE STORE'
    group by
    TEDW_F_STORE_OPS_GOAL.LOCATION_NUM,
    TEDW_D_DATE_TYPE_1.DATE_SHORT
    having TEDW_D_DATE_TYPE_1.DATE_SHORT between MIN(TEDW_S_EMPOWER.date_short) and MAX(TEDW_S_EMPOWER.date_short)
    order by LOCATION_NUM desc
    , DATE_SHORT desc
    Now i receive the correct values as i am doing divided
    by 3,but may be in the future the empower tablemay have more than 3 ,is there any other effective way i can do this?
    below is the sample query and sample data from empower table.
    select * from Staging.TEDW_S_EMPOWER
    where DATE_SHORT = '2014-04-17'
    and LOCATION_NUM = 3020
    DRIVER_TYPE DRIVER DATE_SHORT
    store Sales 2014-04-17
    store Transactions 2014-04-17
    store Traffic 2014-04-17
    Thanks

  • How to get the desire output from sql query

    Consider an Order Table: ...
    Order Table:
    Order Id Item Qty
    O1 A1 5
    O2 A2 1
    O3 A3 3
    Please provide SQL which will explode the above data into single unit level records as shown below
    Desired Output:
    Order Id Order Id Qty
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O1 A1 1
    O2 A2 1
    O3 A3 1
    O3 A3 1
    O3 A3 1

    How do I ask a question on the forums?
    SQL and PL/SQL FAQ

  • SQL Query to get the desired result

    Can someone give the SQL to get this output.
    input is like this in table.
    cola     colb     fromdate     todate
    1     100     1/5/2010     1/9/2010
    1     101     1/10/2010     1/25/2010
    1     102     1/26/2010     12/31/2900
    2     201     3/1/2011     3/10/2011
    2     202     3/11/2011     12/31/2900
    3     301     1/5/2010     1/9/2010
    3     302     1/10/2010     1/25/2010
    3     303     1/26/2010     1/28/2010
    3     304     1/29/2010     12/31/2900
    I want to get the output like this.
    cola     colb     fromdate     todate
    1     100     1/5/2010     1/9/2010
    2     201     3/1/2011     3/10/2011
    3     301     1/5/2010     1/9/2010
    More info:
    I want to get the rows where fromdate is the min(fromdate) from each cola's unique values i.e. 1,2,3
    Please let me know the SQL.
    Thanks.
    [email protected]

    This way!!!
    with data(cola, colb, st_dt, ed_dt) as
    select 1,     100,     to_date('01/05/2010', 'MM/DD/YYYY'),     to_date('01/09/2010', 'MM/DD/YYYY') from dual union all
    select 1,     101,  to_date('01/10/2010', 'MM/DD/YYYY'),     to_date('01/25/2010', 'MM/DD/YYYY') from dual union all
    select 1,     102,     to_date('01/26/2010', 'MM/DD/YYYY'),     to_date('12/31/2900', 'MM/DD/YYYY') from dual union all
    select 2,     201,     to_date('03/01/2011', 'MM/DD/YYYY'),     to_date('03/10/2011', 'MM/DD/YYYY') from dual union all
    select 2,     202,     to_date('03/11/2011', 'MM/DD/YYYY'),     to_date('12/31/2900', 'MM/DD/YYYY') from dual union all
    select 3,     301,     to_date('01/05/2010', 'MM/DD/YYYY'),     to_date('01/09/2010', 'MM/DD/YYYY') from dual union all
    select 3,     302,     to_date('01/10/2010', 'MM/DD/YYYY'),     to_date('01/25/2010', 'MM/DD/YYYY') from dual union all
    select 3,     303,     to_date('01/26/2010', 'MM/DD/YYYY'),     to_date('01/28/2010', 'MM/DD/YYYY') from dual union all
    select 3,     304,     to_date('01/29/2010', 'MM/DD/YYYY'),     to_date('12/31/2900', 'MM/DD/YYYY') from dual
    select cola, colb, st_dt, ed_dt
      from (
            select cola, colb, st_dt, ed_dt,
                   row_number() over (partition by cola order by st_dt) rn
              from data
           ) a
    where a.rn = 1;
    COLA COLB ST_DT     ED_DT  
       1  100 05-JAN-10 09-JAN-10
       2  201 01-MAR-11 10-MAR-11
       3  301 05-JAN-10 09-JAN-10

  • Query:  Not getting the desired result. Please help

    QL> select * from ecev;
    entity_Tag
    7
    8
    9
    10
    1
    2
    3
    4
    5
    6
    SQL> select * from dg;
    DIGP_TAG CEPT_TAG
    600 1
    700 1
    800 1
    900 1
    100 1
    200 1
    500 1
    75 2
    SQL> select * from enow;
    ENTITY_CODE ENTITY_TAG DIGP_TAG TARGET_DA CEPT_TAG
    EXT 7           600      26-MAY-07 1
    EXT 8           700      26-MAY-07 1
    EXT 9           800      26-MAY-07 1
    EXT 10           800      22-APR-76 1
    EXT 1           100      20-OCT-06 1
    EXT 2           200      20-OCT-06 1
    EXT 3           500      20-OCT-06 1
    EXT 4           100      20-OCT-06 1
    EXT 5           100      20-OCT-06 1
    EXT 6           80      20-OCT-06 1
    EXT 1           75           2
    SQL> select * from tk;
    TK_TAG DIGP_TAG NARRATIVE DUE_DATE CEPT_TAG
    35 90 raj 27-MAY-07               1
    36 900 kkd 27-MAY-07               1
    37 100 kim 28-MAY-07               1
    30 100 ram 23-MAY-07               1
    31 100 kish 23-MAY-07               1
    32 200 mar 23-MAY-07               1
    33 100 gy 23-MAY-07               1
    34 100 mm 27-MAY-07               1
    8 rows selected.
    For the following query
    1 select
    2 d.ecev_tag ecev_tag,
    3 a.digp_tag entity_owner ,
    4 c.digp_tag task_owner,
    5 c.tk_tag task_tag,
    6 c.narrative
    7 from enow a,
    8 entl b,
    9 tk c,
    10 ecev d
    11 where ( a.DIGP_TAG=100 or c.digp_tag=100)
    12 and a.entity_tag=b.entity_tag(+)
    13 and b.tk_tag=c.tk_tag(+)
    14* and a.entity_tag=d.entity_tag
    SQL> select * from entl;
    TK_TAG ENTITY_TAG
    35 5
    36 10
    30 1
    31 1
    32 2
    33 3
    34 3
    Result:
    I get
    entity_tag ENTITY_OWNER TASK_OWNER TASK_TAG NARRATIVE
    5 100 90 35 raj          row1
    1 100 100 30 ram          row2
    1 75 100 30 ram          row3 1 100 100 31 kish          row4
    1 75 100 31 kish          row5 3 500 100 33 gy          row6
    3 500 100 34 mm          row7
    4 100                              row8
    Row1: yes, since entity_tag 5 is owned by digp_tag 100 (enow)
    Row2: yes, since entity_tag 1 is owned by digp_tag 100 (enow)
    Row3: NO, since entity_tag 1 is owned by digp_tag 75, which in turn is in cept_tag 2, since the passed digp_tag is 100 and it just belongs to cept_tag 1 we should not be getting this row.
    Row4: Yes, since tk_tag 31 is owned by digp_tag 100 (tk)
    Row5 NO, since entity_tag 1 is owned by digp_tag 75, which in turn is in cept_tag 2 since the passed digp_tag is 100 and it just belongs to cept_tag 1 we should not be getting this row.
    Row6: Yes, since tk_tag 33 is owned by digp_tag 100 (enow)
    Row7: Yes, since tk_tag 34 is owned by digp_tag 100(enow)
    Row8: Yes, since entity_tag 4 is owned by 100(enow
    I guess i have to join dg.cept_tag and a.cept_tag, but it is not working.....
    Can somebody understand this and help...
    Thanks
    Ram

    QL> select * from ecev;
    entity_Tag
    7
    8
    9
    0
    1
    2
    3
    4
    5
    6
    SQL> select * from dg;
    DIGP_TAG CEPT_TAG
    600 1
    700 1
    800 1
    900 1
    100 1
    200 1
    500 1
    75 2
    SQL> select * from enow;
    ENTITY_CODE ENTITY_TAG DIGP_TAG
    TARGET_DA CEPT_TAG
    EXT 7
              600      26-MAY-07 1
    8           700
         26-MAY-07 1
    EXT 9
              800      26-MAY-07 1
    10           800
         22-APR-76 1
    EXT 1
              100      20-OCT-06 1
    2           200
         20-OCT-06 1
    EXT 3
              500      20-OCT-06 1
    4           100
         20-OCT-06 1
    EXT 5
              100      20-OCT-06 1
    6           80
         20-OCT-06 1
    EXT 1
              75           2
    L> select * from tk;
    TK_TAG DIGP_TAG NARRATIVE DUE_DATE
    CEPT_TAG
    35 90 raj 27-MAY-07               1
    36 900 kkd 27-MAY-07               1
    37 100 kim 28-MAY-07               1
    30 100 ram 23-MAY-07               1
    31 100 kish 23-MAY-07               1
    32 200 mar 23-MAY-07               1
    33 100 gy 23-MAY-07               1
    34 100 mm 27-MAY-07               1
    8 rows selected.
    For the following query
    1 select
    2 d.ecev_tag ecev_tag,
    3 a.digp_tag entity_owner ,
    4 c.digp_tag task_owner,
    5 c.tk_tag task_tag,
    6 c.narrative
    7 from enow a,
    8 entl b,
    9 tk c,
    10 ecev d
    11 where ( a.DIGP_TAG=100 or c.digp_tag=100)
    12 and a.entity_tag=b.entity_tag(+)
    13 and b.tk_tag=c.tk_tag(+)
    14* and a.entity_tag=d.entity_tag
    SQL> select * from entl;
    TK_TAG ENTITY_TAG
    35 5
    36 10
    30 1
    31 1
    32 2
    33 3
    34 3
    sult:
    I get
    entity_tag ENTITY_OWNER TASK_OWNER TASK_TAG
    NARRATIVE
    5 100 90
    35 raj          row1
    100 100 30
    ram          row2
    75 100
    30 ram          row31 100 100
    31 kish          row4
    75 100
    31 kish          row5
    3 500 100
    33 gy          row6
    500 100 34
    mm          row7
    100                              row8
    1: yes, since entity_tag 5 is owned by digp_tag 100
    (enow)
    Row2: yes, since entity_tag 1 is owned by digp_tag
    100 (enow)
    Row3: NO, since entity_tag 1 is owned by digp_tag 75,
    which in turn is in cept_tag 2, since the passed
    digp_tag is 100 and it just belongs to cept_tag 1 we
    should not be getting this row.
    Row4: Yes, since tk_tag 31 is owned by digp_tag 100
    (tk)
    Row5 NO, since entity_tag 1 is owned by digp_tag 75,
    which in turn is in cept_tag 2 since the passed
    digp_tag is 100 and it just belongs to cept_tag 1 we
    should not be getting this row.
    Row6: Yes, since tk_tag 33 is owned by digp_tag 100
    (enow)
    Row7: Yes, since tk_tag 34 is owned by digp_tag
    100(enow)
    Row8: Yes, since entity_tag 4 is owned by 100(enow
    I guess i have to join dg.cept_tag and a.cept_tag,
    but it is not working.....
    Can somebody understand this and help...
    Thanks
    Ram
    Result:
    I get
    entity_tag ENTITY_OWNER TASK_OWNER TASK_TAG NARRATIVE
    5 100 90 35 raj     row1
    1 100 100 30 ram     row2
    1 75 100 30 ram     row3
    1 100 100 31 kish     row4
    1 75 100 31 kish     row5
    3 500 100 33 gy     row6
    3 500 100 34 mm     row7
    4 100                    row8

  • Sql query to get the given output

    Hi Friends
    My table data is given below
    user transaction_date transaction_type
    1 01-Aug-2011 a
    1 02-Aug-2011 c
    1 03-Aug-2011 a
    1 03-Aug-2011 b
    1 04-Aug-2011 a
    1 04-Aug-2011 b
    2 03-Aug-2011 a
    2 03-Aug-2011 b
    2 04-Aug-2011 c
    2 04-Aug-2011 b
    2 05-Aug-2011 a
    2 05-Aug-2011 b
    2 07-Aug-2011 a
    2 07-Aug-2011 b
    I want the count for each user as, how many times he did a transaction type 'b' immediately after transaction type a ?
    Like the output for above data should be
    user count
    1 2
    2 3
    Thanks in advance :)

    Assuming there was information in the transaction date to indicate the sequence of the transactions then it would be something like...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1 as usr, to_date('01-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') as transaction_date, 'a' as transaction_type from dual union all
      2             select 1, to_date('02-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'c' from dual union all
      3             select 1, to_date('03-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
      4             select 1, to_date('03-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
      5             select 1, to_date('04-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
      6             select 1, to_date('04-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
      7             select 2, to_date('03-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
      8             select 2, to_date('03-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
      9             select 2, to_date('04-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'c' from dual union all
    10             select 2, to_date('04-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
    11             select 2, to_date('05-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
    12             select 2, to_date('05-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual union all
    13             select 2, to_date('07-Aug-2011 00:00:00','DD-MON-YYYY HH24:MI:SS'), 'a' from dual union all
    14             select 2, to_date('07-Aug-2011 00:00:01','DD-MON-YYYY HH24:MI:SS'), 'b' from dual
    15            )
    16  --
    17  -- end of test data
    18  --
    19  select usr, sum(sum_ab) as sum_ab
    20  from (
    21        select usr
    22              ,case when transaction_type = 'b'
    23                     and lag(transaction_type) over (partition by usr order by transaction_date) = 'a'
    24               then 1
    25               else 0
    26               end as sum_ab
    27        from t
    28       )
    29* group by usr
    SQL> /
           USR     SUM_AB
             1          2
             2          3which is essentially the same as Etbin posted, but without the assumption that the ordering is by transaction_type.

  • Getting the desired Output

    Hi ALl,
    I have a table as Listed below.
    SQL> CREATE  TABLE T1 (c1 varchar2(10),c2 varchar2(10),c3 varchar2(10), c4 var
    ar2(10),audit1 number);
    Table created.
    SQL> DESC T1
    Name                                      Null?    Type
    C1                                                 VARCHAR2(10)
    C2                                                 VARCHAR2(10)
    C3                                                 VARCHAR2(10)
    C4                                                 VARCHAR2(10)
    AUDIT1                                             NUMBER
    SQL> INSERT INTO T1 VALUES('A','B',NULL,2,1);
    1 row created.
    SQL> INSERT INTO T1 VALUES('A','B',1,NULL,1);
    1 row created.
    SQL> COMMIT;
    Commit complete.
    SQL> SELECT * FROM T1;
    C1         C2         C3         C4             AUDIT1
    A          B                     2                   1
    A          B          1                              1
    SQL>I need to output from the above table as single row considering the Dupilcate values as once row ,and also nned to neglect the Null values from it and take the other possible value in it.
    C1         C2         C3         C4             AUDIT1
    A          B           1          2                   1

    Suppose a new row(below) is added, what do you expect as o/p?
    INSERT INTO T1 VALUES('A','B',1,3,2);Purvesh's query will give you the <tt>MAX(c4)</tt> but if you use <tt>SUM(c4)</tt> it'll also give you a single-row & NULLS removed but the values summed.
    -- "Query 1"
    select c1, c2, max(c3) c3, max(c4) c4, max(audit1) audit1
      from t1
    group by c1, c2;
    A     B     1     3     2
    -- "Query 2"
    select c1, c2, SUM(c3) c3, SUM(c4) c4, SUM(audit1) audit1
      from t1
    group by c1, c2;
    A     B     3     7     6Please clarify this.

  • GETTING NO ERROR BUT STILL NOT GETTING THE DESIRED OUTPUT!!!!!!!!

    HIII ,
    AM DOING AN APPLICATION IN WHICH I AM TRYING TO INSERT VALUES IN A TABLE(CREATED THRU JAVA DICTIONARY), USING EJBS(ENTITY BEAN AND SESSION BEAN) AND COMMAND BEAN..AND WEBDYNPRO....
    AFTER DEPLOYMENT EVRYTHNG IS RUNNING FINE BUT VALUES NOT GETTING ADDED IN THE TABLE...
    THE CODE FOR THE "ADD" BUTTON IS :
    wdContext.currentEmp_cmdElement().modelObject().add(wdContext.currentEmp_cmdElement().getEmpno(),wdContext.currentEmp_cmdElement().getEmpname());
    THE CODE FOR ADD function IN COMMANDBEAN IS
    public void add(String empno,String empname){
    try {
    local.createdata(empno,empname);
    } catch (Exception e) {
    e.printStackTrace();}
    PLZZ DO HELP OUT!!!

    try { aPreparedStatement.close();
               if(dmd!=null)
                            dmd = null;
               if(con!=null)
                            con.close();
                catch (Exception e)
                     e.printStackTrace();  
                }instead of catching the last error i printed it and it came up with an error of nullPointerException
    for the line of code
    try { aPreparedStatement.close();how can i fix this error please
    and thank you

  • How to get the desired output in the format ...?

    Hi all,
    I am having data like this.
    Col1 col2 col3
    1 2 1
    1 2 2
    1 2 3
    2 3 7
    2 3 8
    2 3 9
    I want to output the data like this
    1 2 1,2,3
    2 3 7,8,9
    How to get this.
    Thanks in advance,
    Pal

    In simple sql...
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> select * from t
      2  /
          COL1       COL2       COL3
             1          2          1
             1          2          2
             1          2          3
             2          3          7
             2          3          8
             2          3          9
    6 rows selected.
    SQL> select max(decode(rno,1,to_number(col1||col2||col3))) col1, max(decode(rno,2,col3)) col2, max(decode(rno,3,col3)) col3
      2    from (select row_number() over(partition by col1,col2 order by col3) rno, t.*
      3           from t)
      4  group by col1
      5  /
          COL1       COL2       COL3
           121          2          3
           237          8          9
    SQL>Thanks,
    Karthick.

  • How to get the desired output

    My Table Data
    ContractID     MaterialGroup      CommodityCode     Amount
    C123       Travel     200     100
    C123      Travel     201     100
    C123             Travel     203     100
    C124            Stationery     315     500
    C126            Travel             200     600
    C126            Admin             413     600
    My expected output
    ContractID     MaterialGroup      CommodityCode     Amount
    C123        Travel             200|201|203     100
    C124             Stationery             315                     500
    C126            Travel|Admin     200|413           600
    My Present Output
    CONTRACTID MATERIALGROUP    COMMODITYCODE AMOUNT
    C123            Travel|Travel|Travel  200|203|201        100
    C124            Stationery               315                     500
    C126            Travel|Admin           200|413               600
    My Query:
      SELECT ContractID,
      RTrim(
                  xmlagg(
                          xmlelement(e, MaterialGroup|| '|')).extract('//text()'),'|') MaterialGroup,
      RTrim(
                  xmlagg(
                          xmlelement(e, CommodityCode|| '|')).extract('//text()'),'|') CommodityCode,Amount
    FROM (
    WITH t1 AS (
    SELECT 'C123' ContractID,     'Travel'MaterialGroup,      200     CommodityCode, 100 Amount FROM dual UNION all
    SELECT 'C123',            'Travel',     201,     100 FROM dual UNION all
    SELECT 'C123',            'Travel',     203,     100 FROM dual UNION all
    SELECT 'C124','Stationery',     315,     500 FROM dual UNION all
    SELECT 'C126','Travel',     200,     600 FROM dual UNION all
    SELECT 'C126','Admin',413,     600 FROM dual)
    SELECT * FROM t1)
    GROUP BY ContractID,Amount;

    select t.*,
            case when cdm = 1 and cm > 1
                    then substr(materialgroup,1, instr(materialgroup,'|', cdm)-1)
                    else materialgroup end materialgroup_edit,
             case when cdm = 1 and cm > 1 then mm else materialgroup end materialgroup_edit2
    from
              select contractid,
              count(materialgroup) cm,
              count(distinct materialgroup) cdm,
              max(materialgroup) mm,
              rtrim(
                          xmlagg(
                                  xmlelement(e, materialgroup|| '|')).extract('//text()'),'|') materialgroup,
              rtrim(
                          xmlagg(
                                  xmlelement(e, commoditycode|| '|')).extract('//text()'),'|') commoditycode,amount
            from (
            with t1 as (
            select 'C123' contractid,    'Travel' materialgroup,     200    commoditycode, 100 amount from dual union all
            select 'C123',            'Travel',    201,    100 from dual union all
            select 'C123',            'Travel',    203,    100 from dual union all
            select 'C124','Stationery',    315,    500 from dual union all
            select 'C126','Travel',    200,    600 from dual union all
            select 'C126','Admin',413,    600 from dual  )
            select * from t1)
            group by contractid,amount
            ) t;
    CONTRACTID         CM        CDM MM         MATERIALGROUP        COMMODITYCODE            AMOUNT MATERIALGROUP_EDIT   MATERIALGROUP_EDIT2
    C123                3          1 Travel     Travel|Travel|Travel 200|203|201                 100 Travel               Travel             
    C124                1          1 Stationery Stationery           315                         500 Stationery           Stationery         
    C126                2          2 Travel     Travel|Admin         200|413                     600 Travel|Admin         Travel|Admin       
    3 rows selected.
    select contractid,
              case when count(distinct materialgroup) = 1 and count(materialgroup) > 1 then max(materialgroup)
              else
              rtrim(
                          xmlagg(
                                  xmlelement(e, materialgroup|| '|')).extract('//text()'),'|') end materialgroup,
              rtrim(
                          xmlagg(
                                  xmlelement(e, commoditycode|| '|')).extract('//text()'),'|') commoditycode,amount
            from (
            with t1 as (
            select 'C123' contractid,    'Travel' materialgroup,     200    commoditycode, 100 amount from dual union all
            select 'C123',            'Travel',    201,    100 from dual union all
            select 'C123',            'Travel',    203,    100 from dual union all
            select 'C124','Stationery',    315,    500 from dual union all
            select 'C126','Travel',    200,    600 from dual union all
            select 'C126','Admin',413,    600 from dual  )
            select * from t1)
            group by contractid,amount;
    CONTRACTID MATERIALGROUP        COMMODITYCODE            AMOUNT
    C123       Travel               200|203|201                 100
    C124       Stationery           315                         500
    C126       Travel|Admin         200|413                     600
    3 rows selected.
    with t1 as (
            select 'C123' contractid,    'Travel' materialgroup,     200    commoditycode, 100 amount from dual union all
            select 'C123',            'Travel',    201,    100 from dual union all
            select 'C123',            'Travel',    203,    100 from dual union all
            select 'C124','Stationery',    315,    500 from dual union all
            select 'C126','Travel',    200,    600 from dual union all
            select 'C126','Admin',413,    600 from dual  ),
            t2 as
            select contractid, materialgroup
            from t1
            group by contractid, materialgroup
    select  t1.contractid,
              t2.materialgroup,
              rtrim(
                          xmlagg(
                                  xmlelement(e, commoditycode|| '|')).extract('//text()'),'|') commoditycode,amount
    from t1,
                    select contractid,
                                rtrim(
                                      xmlagg(
                                              xmlelement(e, materialgroup|| '|')).extract('//text()'),'|') materialgroup
                    from t2
                    group by contractid
                    ) t2
    where t1.contractid = t2.contractid
    group by t1.contractid, t2.materialgroup, t1.amount;
    CONTRACTID MATERIALGROUP        COMMODITYCODE            AMOUNT
    C123       Travel               200|203|201                 100
    C124       Stationery           315                         500
    C126       Admin|Travel         200|413                     600
    3 rows selected.

  • How to modify the query to get the output in a single row

    Hi All,
    Below is the query i have written it works fine
    select DISTINCT right(left(CTACCT,13),4) AS LocationNum,
    tODS_GLBalance.FiscalYearId AS FiscalYearId,
    tODS_GLBalance.FiscalMonthOfYearId AS FiscalMonthOfYearId,
    --tods_GLMetadata.Metric,
    Case when
    tods_GLMetadata.Metric = 'Gross Margin'
    Then SUM(Balance)
    Else 0
    END AS GrossMargin,
    Case when
    tods_GLMetadata.Metric = 'Occupancy'
    Then SUM(Balance)
    Else 0
    END AS Occupancy,
    Case when
    tods_GLMetadata.Metric = 'Payroll Dollars'
    Then SUM(Balance)
    Else 0
    END AS Payroll,
    Case when
    tods_GLMetadata.Metric = 'CF Sales'
    Then SUM(Balance)
    Else 0
    END AS OperatingSales,
    Case when
    tods_GLMetadata.Metric = 'Operations'
    Then SUM(Balance)
    Else 0
    END AS OperatingExpenses
    -- 0 as payroll
    from ods.[JJill].[tODS_GLBalance]
    inner join ods.Staging.tODS_INF_GLPCT ON tODS_GLBalance.PageNum = tODS_INF_GLPCT.CTPAGE
    inner join ods.JJill.tods_GLMetadata ON tods_GLMetadata.AcctDescription = tODS_INF_GLPCT.CTDESC
    where
    (tODS_GLBalance.FiscalYearId = 2012) and
    (tODS_GLBalance.FiscalMonthOfYearId = 2) and
    (right(left(CTACCT,13),4)= 3020)
    group by
    right(left(CTACCT,13),4),
    tODS_GLBalance.FiscalYearId,
    tODS_GLBalance.FiscalMonthOfYearId,
    tods_GLMetadata.Metric
    This is the sample output,
    LocationNum FiscalYearId FiscalMonthOfYearId GrossMargin Occupancy Payroll OperatingSales OperatingExpenses
    3020 2012 2 -112477.00 0.00 0.00 0.00 0.00
    3020 2012 2 0.00 0.00 0.00 -158288.94 0.00
    3020 2012 2 0.00 0.00 0.00 0.00 5625.44
    3020 2012 2 0.00 0.00 24185.79 0.00 0.00
    3020 2012 2 0.00 31075.53 0.00 0.00 0.00
    But, i am expecting the output to be something like this
    LocationNum FiscalYearId FiscalMonthOfYearId GrossMargin Occupancy Payroll OperatingSales OperatingExpenses
    3020 2012 2 -112477.00 31075.53 24185.79 -158288.94 5625.44
    Can someone please help me with changing my query to get the desired output?
    Please let me know if you have any questions.
    Thanks

    Try this:
    SELECT DISTINCT
    RIGHT(LEFT(CTACCT,13),4) AS LocationNum, tODS_GLBalance.FiscalYearId AS FiscalYearId, tODS_GLBalance.FiscalMonthOfYearId AS FiscalMonthOfYearId,
    SUM(CASE WHEN tods_GLMetadata.Metric = 'Gross Margin' THEN Balance ELSE 0 END ) AS GrossMargin,
    SUM(CASE WHEN tods_GLMetadata.Metric = 'Occupancy' THEN Balance ELSE 0 END ) AS Occupancy,
    SUM(CASE WHEN tods_GLMetadata.Metric = 'Payroll Dollars' THEN Balance ELSE 0 END ) AS Payroll,
    SUM(CASE WHEN tods_GLMetadata.Metric = 'CF Sales' THEN Balance ELSE 0 END ) AS OperatingSales,
    SUM(CASE WHEN tods_GLMetadata.Metric = 'Operations' THEN Balance ELSE 0 END ) AS OperatingExpenses
    FROM ods.[JJill].[tODS_GLBalance]
    INNER JOIN ods.Staging.tODS_INF_GLPCT
    ON tODS_GLBalance.PageNum = tODS_INF_GLPCT.CTPAGE
    INNER JOIN ods.JJill.tods_GLMetadata
    ON tods_GLMetadata.AcctDescription = tODS_INF_GLPCT.CTDESC
    WHERE tODS_GLBalance.FiscalYearId = 2012
    AND tODS_GLBalance.FiscalMonthOfYearId = 2
    AND RIGHT(LEFT(CTACCT,13),4) = 3020
    GROUP BY right(left(CTACCT,13),4), tODS_GLBalance.FiscalYearId, tODS_GLBalance.FiscalMonthOfYearId, tods_GLMetadata.Metric

  • Need Help in Getting the below output

    Hi All,
    The below is the table data.
    col1     col2     col3     col4     col5     col6
    Aa     1     Con     100     10     -1
    Ba     2     Cam     50     2     33
    Aa     1     Con     200     20     -2
    Ca     3     val     2     5     -10
    Aa     1     Con     150     30     -3
    Ba     2     Cam     20     3     11
    Ca     3     val     3     5     -20
    Ba     2     Cam     30     5     22
    Ca     3     val     5     4     -30
    I need the output as below. Please help me in getting the output.
    col1     col2     col3     col4     col5     col6
    Aa     1     Con     100     10     -1
    Aa     1     Con     200     20     -2
    Aa     1     Con     150     30     -3
    Aa-Con Subtotal          450     60     -6
    Ba     2     Cam     20     3     11
    Ba     2     Cam     30     5     22
    Ba     2     Cam     50     2     33
    Ba-Cam Subtotal          100     10     66
    Ca     3     val     2     5     -10
    Ca     3     val     3     5     -20
    Ca     3     val     5     4     -30
    Ca-val Subtotal          10     14     -60
    I am using Oracle 10g. Let me know if other details required.
    Thanks in Advance.
    Regards,
    Ravi Kumar Ankarapu.

    You can try using ROLLUP and GROUPING SETS like how some of the other forum members have suggested. Another way of getting the desired output would be :
    with t as
    select 'Aa' col1, 1 col2, 'Con' col3, 100 col4, 10 col5, -1 col6 from dual
    union all
    select 'Ba', 2, 'Cam', 50, 2, 33 from dual
    union all
    select 'Aa', 1, 'Con', 200, 20, -2 from dual
    union all
    select 'Ca', 3, 'val', 2, 5, -10 from dual
    union all
    select 'Aa', 1, 'Con', 150, 30, -3 from dual
    union all
    select 'Ba', 2, 'Cam', 20, 3, 11 from dual
    union all
    select 'Ca', 3 ,'val', 3, 5, -20 from dual
    union all
    select 'Ba', 2, 'Cam', 30, 5, 22 from dual
    union all
    select 'Ca', 3, 'val', 5, 4, -30 from dual
    select * from
        select * from t
        union all
        select distinct * from
            select col1 || '-' || col3 || ' Sub-total' col1, null col2, null col3,
            sum(col4) over (partition by col1 order by col1) col4,
            sum(col5) over (partition by col1 order by col1) col5,
            sum(col6) over (partition by col1 order by col1) col6
            from t
    order by col1, col2, col3
    Output:
    "COL1"     "COL2"     "COL3"     "COL4"     "COL5"     "COL6"
    "Aa"     "1"     "Con"     "150"     "30"     "-3"
    "Aa"     "1"     "Con"     "100"     "10"     "-1"
    "Aa"     "1"     "Con"     "200"     "20"     "-2"
    "Aa-Con Sub-total"     ""     ""     "450"     "60"     "-6"
    "Ba"     "2"     "Cam"     "50"     "2"     "33"
    "Ba"     "2"     "Cam"     "20"     "3"     "11"
    "Ba"     "2"     "Cam"     "30"     "5"     "22"
    "Ba-Cam Sub-total"     ""     ""     "100"     "10"     "66"
    "Ca"     "3"     "val"     "5"     "4"     "-30"
    "Ca"     "3"     "val"     "2"     "5"     "-10"
    "Ca"     "3"     "val"     "3"     "5"     "-20"
    "Ca-val Sub-total"     ""     ""     "10"     "14"     "-60"

  • How to set Back ground job & get my desired output layout in sm36 & sm37 ?

    Hai SAP ABAP/BASIS Gurus,
    I am currently taking ME2N PO Pending value Report by setting variant in input screen & setting my own output layout  ie only four field ie.Plant, Material,Currency,Still to be delivered value will be visible in output screen
    I set Back ground in sm36 using my variant & i saw my output in sm37.. But I cant get output as my desired field..
    This is my problem..
    1) I Cant Get the Same output in shedule job [sm36 & sm37] like When i run in foreground for ME2n.
        I am using ALV Scope of list..
    2) If I use BEST Layout in Scope of list means i get the desired Output field layout as same as my foreground output field..
    I want to get the output layout as desired & also in ALV format[ Useful for excel work]
    Kindly guide me.
    Thanks in advance..
    swetha
    Edited by: Swetha SAP Girl on Jul 2, 2009 5:06 PM

    Hi Swetha,
    To get the missed fields in the spool, while creating the background job(execute in background option) you can see the printer setting block at the end of the background job settings window, in that check the 'Spool Request Max. Width 255 char' check box and then continue. It will generate the spool list with maximum width and you can see all the fields in the spool output.
    Thanks,
    Muthu
    Edited by: Muthu Prabakaran Selvam on Jul 3, 2009 2:35 PM

  • Query to get the data of all the columns in a table except any one column

    Can anyone please tell how to write a query to get the data of all the columns in a table except one particular column..
    For Example:
    Let us consider the EMP table.,
    From this table except the column comm all the remaining columns of the table should be listed
    For this we can write a query like this..
    Select empno, ename, job, mgr, sal, hiredate, deptno from emp;
    Just to avoid only one column, I mentioned all the remaining ( 7 ) columns of the table in the query..
    As the EMP table consists only 8 columns, it doesn't seem much difficult to mention all the columns in the query,
    but if a table have 100 columns in the table, then do we have to mention all the columns in the query..?
    Is there any other way of writing the query to get the required result..?
    Thanks..

    Your best best it to just list all the columns. Any other method will just cause more headaches and complicated code.
    If you really need to list all the columns for a table because you don't want to type them, just use something like...
    SQL> ed
    Wrote file afiedt.buf
      1  select trim(',' from sys_connect_by_path(column_name,',')) as columns
      2  from (select column_name, row_number() over (order by column_id) as column_id
      3        from user_tab_cols
      4        where column_name not in ('COMM')
      5        and   table_name = 'EMP'
      6       )
      7  where connect_by_isleaf = 1
      8  connect by column_id = prior column_id + 1
      9* start with column_id = 1
    SQL> /
    COLUMNS
    EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO
    SQL>

Maybe you are looking for

  • Purchase order version management

    Dear All.              I have configured the purchase order version management. but at the time of change ppurchase order i have changed the text field for that also new version is triggered. i want to control this if only qty and price and payment t

  • How to add new plug-in in phone gap using adf

    Right now device features which are exposed in adf does'nt have the capability to have a barcode reader functionality, however i seen a reference on internet about barcode reader plugin availability for phonegap. Since ADF internally uses phonegap, I

  • Bex Analyzer - Change Query Layout

    I want to change how groups are displayed in my Bex Excel Analyzer. I have a row of Cost Centre / Employee and Cost Element but I want Cost Centre to appear above Employee and Cost Element. E.g. Current: Cost Centre / Employee / Cost Element What I'd

  • About Oracle Certification

    I want to ask about Oracle Certification is there a hard copy for certification of Oracle E-Business Suite 12 Financial Management Certified Implementation Specialist: Oracle Payables after pass the exam ?? regards

  • Need for help in select tag with jsp

    i have a select tag about shoeing countriesin a register form and according to the selection of country i want to shoe the names of its cities .............i guess it has something to so with js and i have a little experience in it ..................