SQL HELP NEEDED IN QUERY FOR COUNT

I Have this Query
SUM (CASE WHEN b.VET_OTHR_ELIG_CDE IN ('02', '03', '04') THEN 1 END) AS VET_YES,
SUM (CASE WHEN b.VET_OTHR_ELIG_CDE = '01' THEN 1 END) VET_NO, COUNT (E.ACTV_CDE) AS CNT_ACTV_CDE
Now i need to Add two more columns from the same Query Showing the count of VET_YES and VET_NO i.e count of TOTAL Veterans And TOTAL Non Veterans
those two columns i will be using as summary columns in my report.The bolded columns are those which i need to show the total column .anyone please help in this issue ..
ACTV_DESC ACTV_CDE VET_YES VET_NO CNT_ACTV_CDE
INACT DUE 13993 2 1 3
NOW I NEED TO MAKE IT LIKE THIS
ACTV_DESC ACTV_CDE VET_YES VET_NO CNT_VET CNT_NONVET CNT_ACTV_CDE (This is the total count)
INACT DUE 13993 2 1 2 1 3
Thanks in Advance,
Dev Kishore.T
Message was edited by:
Dev Kishore
Message was edited by:
Dev Kishore

Check this link.
http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/aggreg.htm#sthref1612
Regards
Raj

Similar Messages

  • Help needed in query for Materialized View

    Hi,
    I want to create a materialized view which has some precalcultaed values.
    I have four dimension tables out of which one is a Time Dimension table with levels as Year->Quarter->Month.
    The precalculations are the moving averages and cummulative values of Sales Amt on the dimension values over a period of 4 Months.
    The dimension tables are Clients, Products, Channel, Time.
    Fact Table is Sales_Fact which will have the sales amount for different members of the dimension.
    Since my fact table is very huge, i want to create a materialized view where i could store the required calculated measures, here Moving Average of Sales Amt for each Client, Product, Channel dimensions over a period of 4 Months.
    Can anybody help with writing the query for this..Any help in this regard will be appreciated..
    Please give me suggestions for this..

    Check this link.
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/aggreg.htm#sthref1612
    Regards
    Raj

  • Need help writing a query for following scenario

    Hi all, I need some help writing a query for the following case:
    One Table : My_Table
    Row Count: App 5000
    Columns of Interest: AA and BB
    Scenario: AA contains some names of which BB contains the corresponding ID. Some
    names are appearing more than once with different IDs. For example,
    AA BB
    Dummy 10
    Me 20
    Me 30
    Me 40
    You 70
    Me 50
    Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
    I would appreciate any input. Thanks

    Is it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
    With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
    WITH t AS (
    SELECT 'me' aa, 10 bb FROM dual
    UNION ALL
    SELECT 'me' aa, 20 bb FROM dual
    UNION ALL
    SELECT 'you' aa, 30 bb FROM dual
    UNION ALL
    SELECT 'you' aa, 30 bb FROM dual
    SELECT DISTINCT aa, bb
      FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
    WHERE cnt > 1;

  • How to write sql query for counting pairs from below table??

    Below is my SQL table structure.
    user_id | Name | join_side | left_leg | right_leg | Parent_id
    100001 Tinku Left 100002 100003 0
    100002 Harish Left 100004 100005 100001
    100003 Gorav Right 100006 100007 100001
    100004 Prince Left 100008 NULL 100002
    100005 Ajay Right NULL NULL 100002
    100006 Simran Left NULL NULL 100003
    100007 Raman Right NULL NULL 100003
    100008 Vijay Left NULL NULL 100004
    It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
    I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains  both left_leg and right_leg id, then it is called pair.)
    I know there are three pairs under id "100001" :-
    1.  100002 and 100003
    2.  100004 and 100005
    3.  100006 and 100007
        100008 will not be counted as pair because it does not have right leg..
     But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
    Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

    Sounds like this to me
    DECLARE @ID int
    SET @ID = 100001--your passed value
    SELECT left_leg,right_leg
    FROM table
    WHERE (user_id = @ID
    OR parent_id = @ID)
    AND left_leg IS NOT NULL
    AND right_leg IS NOT NULL
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Need helping in writing query for finding percentage of duration

    Can any one please help in writing query for this.
    The table is like this :-
    ID     Region     Month     Duration
    I1 R1     Jan     80
    I2     R2     Jan     70
    I3     R1     Jan     70
    I4     R3     Jan     40
    I5     R1     Feb     80
    I6     R2     Feb     30
    I7     R3     Mar     100
    I want to write a query to find
    % of duration for each and every region against each and every month.
    Please help in solving this query. I am in urgent need of this.
    Thanks in advance.

    I also have to do in MS Access 2003You also have to ask into an other forum since here it's an Oracle forum, to try to find Oracle solution.
    Nicolas.

  • Need a query for export table data .....

    Hi,
    I need a query for exporting the data in a table to a file.
    Can anyone help me ?
    Thanking You
    Jeneesh

    SQL> spool dept.txt
    SQL> select * from dept;
        DEPTNO DNAME          LOC
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
    SQL> spool off
    SQL> ed dept.txt

  • Need a query for monthly Report

    Hello All,
    I need a query for monthly report,
    comp_code
    emp_id
    dept_id
    work_day
    100
    A100
    MECH
    01/01/2013
    100
    A100
    MECH
    02/01/2013
    100
    A100
    MECH
    03/01/2013
    100
    A100
    MECH
    04/01/2013
    100
    A100
    MECH
    05/02/2013
    100
    A100
    MECH
    08/02/2013
    100
    A100
    MECH
    09/02/2013
    100
    A100
    MECH
    10/02/2013
    100
    A100
    MECH
    12/05/2013
    100
    A100
    MECH
    13/05/2013
    100
    A101
    CIV
    01/04/2013
    100
    A101
    CIV
    02/04/2013
    100
    A101
    CIV
    03/04/2013
    100
    A101
    CIV
    04/04/2013
    100
    A101
    CIV
    06/04/2013
    100
    A101
    CIV
    06/06/2013
    100
    A101
    CIV
    07/06/2013
    100
    A101
    CIV
    08/06/2013
    100
    A101
    CIV
    09/06/2013
    100
    A101
    CIV
    10/06/2013
    100
    A101
    CIV
    11/12/2013
    100
    A101
    CIV
    12/12/2013
    100
    A101
    CIV
    13/12/2013
    100
    A101
    CIV
    14/12/2013
        Dear friends this the sample table of my report.In which table has contain list of  employees with their working days(actual table has contain almost 5laks of records).
    suppose user choose the date between 01/01/2013 and 31/12/2013 then the result should be like this.
    comp_code
    emp_id
    dept_id
    month
    Total_work
    100
    A100
    MECH
    JANUARY
    4
    100
    A100
    MECH
    FEBRUARY
    2
    100
    A100
    MECH
    MARCH
    0
    100
    A100
    MECH
    APRIL
    0
    100
    A100
    MECH
    MAY
    2
    100
    A100
    MECH
    JUNE
    0
    100
    A100
    MECH
    JULY
    0
    100
    A100
    MECH
    AUGUST
    0
    100
    A100
    MECH
    SEPTEMBER
    0
    100
    A100
    MECH
    OCTOBER
    0
    100
    A100
    MECH
    NOVEMBER
    0
    100
    A100
    MECH
    DECEMBER
    0
    100
    A101
    CIV
    JANUARY
    0
    100
    A101
    CIV
    FEBRUARY
    0
    100
    A101
    CIV
    MARCH
    0
    100
    A101
    CIV
    APRIL
    5
    100
    A101
    CIV
    MAY
    0
    100
    A101
    CIV
    JUNE
    5
    100
    A101
    CIV
    JULY
    0
    100
    A101
    CIV
    AUGUST
    0
    100
    A101
    CIV
    SEPTEMBER
    0
    100
    A101
    CIV
    OCTOBER
    0
    100
    A101
    CIV
    NOVEMBER
    0
    100
    A101
    CIV
    DECEMBER
    4

    Hi,
    If you want the output to include months where no work was done (with 0 in the total_work column) then you need to outer-join a "table" that has one row per month, and make it a partitioned outer join:
    WITH  got_end_points   AS
       SELECT  TRUNC (MIN (work_day), 'MONTH')   AS first_month
       ,       TRUNC (MAX (work_day), 'MONTH')   AS last_month
       FROM    table_x
    ,   all_months   AS
       SELECT  ADD_MONTHS (first_month, LEVEL - 1)   AS a_month
       ,       ADD_MONTHS (first_month, LEVEL)       AS next_month
       FROM    got_end_points
       CONNECT BY  LEVEL <= 1 + MONTHS_BETWEEN (last_month, first_month)
    SELECT    t.comp_code
    ,         t.emp_id
    ,         t.dept_id
    ,         m.a_month
    ,         COUNT (t.work_day) AS total_work
    FROM             all_months  m
    LEFT OUTER JOIN  table_x     t  PARTITION BY (t.comp_code, t.emp_id, t.ept_id)
                                    ON   t.work_day  >= a.a_month
                                    AND  t.work_day  <  a.next_month
    GROUP BY  t.comp_code
    ,         t.emp_id
    ,         t.dept_id
    ,         m.a_month
    ORDER BY  t.comp_code
    ,         t.emp_id
    ,         t.dept_id
    ,         m.a_month
    As posted, this include every month that is actually in the table.  You can change the first sub-query if you want to enter first and last months.
    I hope this answers your question.
    If not, post  a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Point out where the query above is giving the wrong results, and explain, using specific examples, how you get the correct results from the given data in those places.  If you changed the query at all, post your code.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Need help with named query for 1-to-many entities

    Hi all,
    I'm in desperate need of a query and I can't seem to figure it out.
    Here's the situation:
    I have 2 entities in a one-to-many relationship. One ServerInstance to Many AppUrl's related by a join table
    @Entity
    public class ServerInstance implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;
    private String instancename;
    private String description;
    private String home;
    private String hostname;
    private String hostname_front;
    private String ipfront;
    private String ipback;
    private String os;
    private String layer;
    @OneToMany (fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    private List<AppUrl> appUrl = new ArrayList<AppUrl>();
    @Entity
    public class AppUrl implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;
    private String appurl;
    private String description;
    private String keyword;
    private Integer priority;
    private String curlresult;
    In the underlying database they are mapped by the table T_SERVERINSTANCE_T_APPURL
    What I need is a named query in the ServerInstance entity to find any AppUrl where curlresult = 'down'
    I tried the following:
    @NamedQuery
    (name = "findDownInstances", query = "SELECT i FROM ServerInstance i WHERE i.appUrl.result = 'down')
    This gave me the following error:
    invalid navigation expression [i.appUrl.result], cannot navigate collection valued association field [appUrl].
    Is there any way to create a query which will do what I want?
    Thanx in advance!

    can you provide the equivalent SQL (not JPQL!) query that you want, based on tables T_SERVERINSTANCE and T_APPURL, so that there are no misunderstandings ?

  • Help needed in Query

    I have one table Prd_mst, I need a query or function where whenever i pass the child_id into query or function i get the Parent_id of that
    example Data is below :-
    P_ID,P_NAME,C_ID
    null,Computer,1
    1,KeyBoard,2
    1,Mouse,3
    1,Mother Board,4
    3,Scroll Mouse,5
    3,Optical Scroll Mouse,6
    3,Fibre Scroll Mouse,7
    2,Multimedia Key Board,8
    2,Cordless Key Board,10
    2,Normal Key Board,9
    4,586 Mother Board,13
    4,386 Mother Board,11
    4,486 Mother Board,12
    4,P1 Mother Board,14
    4,P2 Mother Board,15
    4,P3 Mother Board,16
    4,P4 Mother Board,17
    14,533 Mhtz P1 CPU,19
    14,433 Mhtz P1 CPU,18
    19,533 Mhtz P1 CPU With 100Mhtz BUS,20
    19,533 Mhtz P1 CPU With 133Mhtz BUS,21
    18,433 Mhtz P1 CPU With 100Mhtz BUS,22
    So when ever i put the child id as 22 i should get the 14
    So when ever i put the child id as 21 i should get the 14
    So when ever i put the child id as 9 i should get the 1
    So when ever i put the child id as 15 i should get the 1
    So when ever i put the child id as 5 i should get the 1
    So when ever i put the child id as 3 i should get the null
    So when ever i put the child id as 14 i should get the 1.
    Please help me......
    There is one complex requirement in matrix format.
    Need P_ID , P_NAME , Count( child records)
    P_(nth), P_NAME(nth), Count(child record) nth times.
    Child is again the parent of some child.
    Report is needed for audit purpose.
    Message was edited by: Sandeep Sharma
    Sandeep Sharma

    SQL> CREATE TABLE SANDEEP(P_ID NUMBER,P_NAME VARCHAR2(100),C_ID NUMBER);
    Table created.
    SQL> INSERT INTO SANDEEP VALUES(null,'Computer',1);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(1,'KeyBoard',2);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(1,'Mouse',3);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(1,'Mother Board',4);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(3,'Scroll Mouse',5);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(3,'Optical Scroll Mouse',6);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(3,'Fibre Scroll Mouse',7);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(2,'Multimedia Key Board',8);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(2,'Cordless Key Board',10);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(2,'Normal Key Board',9);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'586 Mother Board',13);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'386 Mother Board',11);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'486 Mother Board',12);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'P1 Mother Board',14);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'P2 Mother Board',15);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'P3 Mother Board',16);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(4,'P4 Mother Board',17);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(14,'533 Mhtz P1 CPU',19);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(14,'433 Mhtz P1 CPU',18);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(19,'533 Mhtz P1 CPU With 100Mhtz BUS',20);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(19,'533 Mhtz P1 CPU With 133Mhtz BUS',21);
    1 row created.
    SQL> INSERT INTO SANDEEP VALUES(18,'433 Mhtz P1 CPU With 100Mhtz BUS',22);
    1 row created.
    SQL> COMMIT;
    Commit complete.
    SQL> set null null
    SQL> SELECT P_ID
      2    FROM SANDEEP
      3     WHERE LEVEL = 2
      4     START WITH C_ID = &1
      5     CONNECT BY C_ID = PRIOR P_ID;
    Enter value for 1: 22
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 22
          P_ID
            14
    SQL> /
    Enter value for 1: 21
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 21
          P_ID
            14
    SQL> /
    Enter value for 1: 9
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 9
          P_ID
             1
    SQL> /
    Enter value for 1: 15
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 15
          P_ID
             1
    SQL> /
    Enter value for 1: 5
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 5
          P_ID
             1
    SQL> /
    Enter value for 1: 3
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 3
          P_ID
    null
    SQL> /
    Enter value for 1: 14
    old   4:    START WITH C_ID = &1
    new   4:    START WITH C_ID = 14
          P_ID
             1
    SQL> SY.

  • Need a query for access tables

    Hi
    I need a query , help please:
    table1:
    id              description                  serial                    
    1                des1                           ser1
    2                des2                           ser2
    3                des3                           ser3
    table2:
    id            id1(id of table1)                date                
    stat
    1              1                                      
    2001                 A
    2              1                                      
    2008                 N
    3              2                                      
    2010                 A
    4              1                                      
    2012                 F
    ==============
    i need this data structure: (stat column must return for maximum date of id1 from table2)
    id(from table1)            serial(from table1)                  stat(from table2)
    1                                    ser1                
                           2012
    2                                    ser2             
                              2010
    3                                    ser3             
                               Null

    maybe you can try this.
    declare @t1 table
    (id int,[description] varchar(50),serial varchar(10))
    declare @t2 table
    (id int,id1 int,[date] char(4),stat char(10))
    insert into @t1 values
    (1,'des1','ser1'),
    (2,'des2','ser2'),
    (3,'des3','ser3')
    insert into @t2 values
    (1,1,'2001','A'),
    (2,1,'2008','N'),
    (3,2,'2010','A'),
    (4,1,'2012','F')
    select a.id,a.serial,max(b.date)
    from @t1 a
    left join @t2 b
    on a.id = b.id1
    group by a.id,a.serial
    | SQL PASS Taiwan Page |
    SQL PASS Taiwan Group
    |
    My Blog

  • Help to rewirte query for best performance

    Hi All,
    can you kindly help me to rewirte the below mentioned query for best performance. this is taking more than 20 min in our production server.
    SELECT cp.name,mis.secondary_type U_NAME,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-161,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-154,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-154,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-147,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-147,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-140,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-140,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-133,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-133,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-126,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-126,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-119,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-119,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-112,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-112,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-105,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-105,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-98,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-98,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-91,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-91,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-84,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-84,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-77,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-77,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-70,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-70,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-63,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-63,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-56,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-56,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-49,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-49,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-42,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-42,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-35,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-35,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-28,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-28,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-21,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-21,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-14,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage,
    count(CASE WHEN (mis.start_time between To_DATE(to_char(next_day (sysdate-14,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-7,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))THEN mis.event_audit_id END) Usage
    FROM mis_event_audit mis,USER u,com_pros cp where
    mis.user_id=u.email_address and u.cp_id=cp.cp_id
    and (mis.start_time between To_DATE(to_char(next_day (sysdate-161,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy') and To_DATE(to_char(next_day (sysdate-7,'monday'),'MM/DD/YYYY'),'mm/dd/yyyy'))
    GROUP BY cp.name, mis.secondary_type;
    Thanks,
    krish

    Hi, Krish,
    Something like this will probably be faster, because it cuts out most of the function calls:
    WITH     got_cnt          AS
         SELECT    cp.name
         ,       mis.secondary_type          AS u_name
         ,       COUNT (mis.event_audit_id)     AS cnt
         ,       ( TRUNC (mis.start_time, 'IW')
                - TRUNC (SYSDATE,        'IW')
                ) / 7                    AS week_num
         FROM      mis_event_audit  mis
         JOIN       user_table        u     ON   mis.user_id  = u.email_address     -- USER is not a good table name
         JOIN       com_pros        cp     ON   u.cp_id       = cp.cp_id
         WHERE       mis.start_time   >= TRUNC (SYSDATE, 'IW') - 161
         AND       mis.start_time   <  TRUNC (SYSDATE, 'IW')
         GROUP BY  cp.name
         ,            mis.secondary_type
         ,       TRUNC (mis.start_time, 'IW')
    SELECT       name
    ,       secondary_type
    ,       SUM (CASE WHEN week_num = 22 THEN cnt END)     AS week_23
    ,       SUM (CASE WHEN week_num = 21 THEN cnt END)     AS week_22
    ,       SUM (CASE WHEN week_num = 20 THEN cnt END)     AS week_21
    ,       SUM (CASE WHEN week_num =  0  THEN cnt END)     AS week_1
    FROM       got_cnt
    GROUP BY  name
    ,            secondary_type
    ;TRUNC (d, 'IW')       is midnight on the last Monday before or equal to the DATE d. It does not depend on you NLS settings.
    Whenever you're tempted to write an exprssion as complicated as
    ,     COUNT ( CASE
                       WHEN ( mis.start_time BETWEEN TO_DATE ( TO_CHAR ( NEXT_DAY  ( SYSDATE - 161
                                                                       , 'monday'
                                                  , 'MM/DD/YYYY'
                                        , 'MM/DD/YYYY'
                              AND     TO_DATE ( TO_CHAR ( NEXT_DAY ( SYSDATE - 154
                                                                 ,'monday'
                                                , 'MM/DD/YYYY'
                                          , 'MM/DD/YYYY'
                  THEN mis.event_audit_id
               END
             )               AS usageseek alternate ways. Oracle provides several handy functions, especially for manipulating DATEs. In particular "TO_DATE (TO_CHAR ...)" is almost never needed; think very carefully before doing a round-trip conversion like that.
    Besides being more efficient, this will be easier to debug and maintain.
    If you're using Oracle 11.1 (or higher), then you can also use SELECT ... PIVOT in the main query, but I doubt that will be any faster, and it might not be any simpler.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Simplify the problem as much as possible. For example, instead of posting a problem that covers the last 23 weeks, pretend that you're only interested in the last 3 weeks. You'll get a solution that's easy to adapt to any number of weeks.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}
    For performance problems, there's another page of the forum FAQ {message:id=9360003}, but, before you start that process, let's get a cleaner query, without so many functions.
    Edited by: Frank Kulash on Oct 2, 2012 11:50 AM
    Changed week_num to be non-negative

  • Need a Query for getting ItemCost from PO Vendor wise

    Hi,Experts We are not maintain different price list for  some particular items , We raised the PO for Multiple vendors with differnt price for the same material. We need a query to find the Item cost with Various Vendor .
    I need the query in the following format
    Po Number, Vendor Name,Item Name, Item Description,Qty, Price,Tax.
    Thanks
    Kamal

    Hi,
    Check this query which looks for the Item doctype Purchase Order and brings the data accordingly :
    select t0.docnum as 'PO Number', t0.CardName as 'Vendor name', t1.itemcode as 'Item Name',
    t1.Dscription as 'Item Description', t1.quantity as 'QTY', t1.Price as 'Price',
    t1.vatsum as 'Tax Amount'
    from OPOR t0 inner join POR1 t1 on t0.docentry = t1.docentry
    where t0.doctype ='I'
    Group by t1.itemcode, t1.Dscription, t0.docnum,
    t0.cardname, t1.quantity, t1.Price, t1.vatsum
    Order by t1.Itemcode
    Check if it helps.
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Query for counting

    can i achieve effect of counting of 1 to 50 (say) by a single query?
    eg. i wan a query for which output is:-
    1
    2
    3
    4
    50
    plz. note that no other table is available for joining purposes.

    An effective way of doing the same is using a query like:
    select rownum
    from (
    select 1, 2, 3, 4, 5, 6, 7, 8
    from dual
    group by cube (1, 2, 3, 4, 5, 6, 7, 8)
    The advantage of it is not needing a table with x rows to get number you want; also, this query as it is produces 2 ^ 10, i.e. 2 to 10th power, that gives us 1024 (number of rows, so our "maximum" value). If you need higher numbers than that, just ad to the select and group by cube more numbers, like follows:
    select rownum
    from (
    select 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    from dual
    group by cube (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
    This would give us a maximum value of 2 to 11th power, or 2048.
    I got this code on asktom.oracle.com, a very valuable resource!
    Regards,
    Bruno

  • Help needed in query optimization.....

    Hi
    I have this below query which automatically gets generated in siebel.
    Problem is when i execute this query in toad for "Nichole" it took 2 mins to return 30 records but when i execute this same query for "Adam" it returns more records than Nichole and return results in 1-2 secs. i Know executing query in toad is not a proper way but this below query will get executed in same way thru siebel application. Histogram is already created on column "Name" but of no help. Composite Index(CX_AA_EVNT_RADIO_BATCH_TIME) is there on column bot_id & timestp.
    Table s_org_ext has only 5 records of "Nichole" and 10 records of "Adam".
    Will including Column "Name" in Above composite index can help?
    Please anybody can suggest me anything in this? whatelse i can do to tune this query?
    Thanks for your help in advance
    Chandan Singh
    Also, don't know how better i can format this code :-(
    SELECT t7.conflict_id, t7.last_upd, t7.created, t7.last_upd_by,
    t7.created_by, t7.modification_num, t7.row_id, t7.last_upd,
    t7.aa_acount_id, t7.aa_last_step, t7.accessory, t7.account_exists,
    t7.account_number, t7.actn_cd, t7.action_counter, t7.activated_by,
    t7.address, t7.address_2, t7.address_validation_status,
    t4.account_name, t7.aggregated_account_id, t7.aggregated_flag,
    t7.asset_id, t7.auto_delivery_type, t7.auto_make, t7.auto_model,
    t7.auto_year, t7.automated_approval, t7.activation_frequency,
    FROM siebel.s_prod_int t1,
    siebel.s_org_ext t2,
    siebel.s_asset t3,
    siebel.av_account t4,
    siebel.s_org_ext t5,
    siebel.s_prod_int_x t6,
    siebel.av_event t7
    WHERE t1.row_id = t6.par_row_id(+)
    AND t7.promotion_code = t1.part_num(+)
    AND t7.asset_id = t3.row_id(+)
    AND t3.owner_accnt_id = t5.par_row_id(+)
    AND t7.id = t2.par_row_id --- S_ORG_EXT_U3
    AND t7.id1 = t4.row_id(+)
    AND (t2.NAME = 'Nichole')
    ORDER BY t7.bot_id, t7.timestp
    Below is the execution plan for above query.
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=399904 Card=5609601 Bytes=4930839279)
    1 0 NESTED LOOPS (OUTER) (Cost=399904 Card=5609601 Bytes=4930839279)
    2 1 NESTED LOOPS (OUTER) (Cost=343808 Card=5609601 Bytes=4824256860)
    3 2 NESTED LOOPS (OUTER) (Cost=231616 Card=5609601 Bytes=4740112845)
    4 3 NESTED LOOPS (OUTER) (Cost=175520 Card=5609601 Bytes=4482071199)
    5 4 NESTED LOOPS (OUTER) (Cost=119424 Card=5609601 Bytes=4325002371)
    6 5 NESTED LOOPS (Cost=63328 Card=5609601 Bytes=4240858356)
    7 6 TABLE ACCESS (BY INDEX ROWID) OF 'av_event' (Cost=6729 Card=5659910 Bytes=4126074390)
    8 7 INDEX (FULL SCAN) OF 'av_event_timpstp' (NON-UNIQUE) (Cost=24559 Card=5659910)
    9 6 TABLE ACCESS (BY INDEX ROWID) OF 'S_ORG_EXT' (Cost=1 Card=1 Bytes=27)
    10 9 INDEX (UNIQUE SCAN) OF 'S_ORG_EXT_U3' (UNIQUE)
    11 5 TABLE ACCESS (BY INDEX ROWID) OF 'S_PROD_INT' (Cost=1 Card=1 Bytes=15)
    12 11 INDEX (RANGE SCAN) OF 'S_PROD_INT_M3' (NON-UNIQUE)
    13 4 TABLE ACCESS (BY INDEX ROWID) OF 'av_account' (Cost=1 Card=1 Bytes=28)
    14 13 INDEX (UNIQUE SCAN) OF 'av_account_P1' (UNIQUE)
    15 3 TABLE ACCESS (BY INDEX ROWID) OF 'S_ASSET' (Cost=1 Card=1 Bytes=46)
    16 15 INDEX (UNIQUE SCAN) OF 'S_ASSET_P1' (UNIQUE)
    17 2 TABLE ACCESS (BY INDEX ROWID) OF 'S_PROD_INT_X' (Cost=1 Card=1 Bytes=15)
    18 17 INDEX (RANGE SCAN) OF 'S_PROD_INT_X_U1' (UNIQUE) (Cost=1 Card=1)
    19 1 TABLE ACCESS (BY INDEX ROWID) OF 'S_ORG_EXT' (Cost=1 Card=1 Bytes=19)
    20 19 INDEX (UNIQUE SCAN) OF 'S_ORG_EXT_U3' (UNIQUE)
    Below is the No. of records in each table.
    NUM_ROWS     LAST_ANALYZED     TABLE_NAME     BLOCKS
    5,594,027.00     09/08/2006 03:03:02     av_account     92,565.00
    6,659,910.00     09/22/2006 16:14:06     av_event      608,292.00
    15,384,080.00     09/22/2006 15:28:04     S_ASSET     1,244,124.00
    10,249,905.00     04/23/2006 23:51:44     S_ORG_EXT     2,313,814.00
    1,081.00     09/22/2006 15:26:12     S_PROD_INT     129.00
    602.00     09/08/2006 04:39:42     S_PROD_INT_X     10.00

    can you plz try this
    SELECT /*+ LEADING(t2) */ t7.conflict_id, t7.last_upd, t7.created, t7.last_upd_by,
    t7.created_by, t7.modification_num, t7.row_id, t7.last_upd,
    t7.aa_acount_id, t7.aa_last_step, t7.accessory, t7.account_exists,
    t7.account_number, t7.actn_cd, t7.action_counter, t7.activated_by,
    t7.address, t7.address_2, t7.address_validation_status,
    t4.account_name, t7.aggregated_account_id, t7.aggregated_flag,
    t7.asset_id, t7.auto_delivery_type, t7.auto_make, t7.auto_model,
    t7.auto_year, t7.automated_approval, t7.activation_frequency,
    FROM siebel.s_prod_int t1,
    siebel.s_org_ext t2,
    siebel.s_asset t3,
    siebel.av_account t4,
    siebel.s_org_ext t5,
    siebel.s_prod_int_x t6,
    siebel.av_event t7
    WHERE t1.row_id = t6.par_row_id(+)
    AND t7.promotion_code = t1.part_num(+)
    AND t7.asset_id = t3.row_id(+)
    AND t3.owner_accnt_id = t5.par_row_id(+)
    AND t7.id = t2.par_row_id --- S_ORG_EXT_U3
    AND t7.id1 = t4.row_id(+)
    AND (t2.NAME = 'Nichole')
    ORDER BY t7.bot_id, t7.timestp

  • Pl Dynamic sql help needed

    Hello Everyone
    I am using database 11g. Can someone pl suggest what are the rules for putting single quotes for dynamic sql like
    1) how many single quotes before/after a sql statement .
    2) Rules for putting single quotes if there exists i) a variable in the sql statement
    ii) a constant for e.g 100 in the sql statment.
    If you could give the answers with a simple select statement, it will be even better !!!
    Thanks and regards
    Gautam

    ms wrote:
    Hello Everyone
    I am using database 11g. Can someone pl suggest what are the rules for putting single quotes for dynamic sql like
    1) how many single quotes before/after a sql statement .
    2) Rules for putting single quotes if there exists i) a variable in the sql statement
    ii) a constant for e.g 100 in the sql statment.
    If you could give the answers with a simple select statement, it will be even better !!!
    Thanks and regards
    Gautamwrite the SQL statement as you would for any SQL client & enclose it using Q-quote
    http://askanantha.blogspot.com/2007/12/q-quote-operator-introduced-in-oracle.html
    Handle:     ms
    Status Level:     Newbie
    Registered:     Jun 3, 2007
    Total Posts:     46
    Total Questions:     17 (17 unresolved)
    WOW!
    *NEVER got any answer in 5+ years & still wasting time here again, still!
    You must be an eternal optimist.
    I hope I get credited for your FIRST answer.

Maybe you are looking for