Help needed in framing SQL query.

Hi,
I have table having following schema:
PRODUCT_ID         INT
DATE                   DATETIME
ITEMS_SOLD         INT
This table contains data for a week (sunday to saturday). I want to write an SQL query to get (filter out) PRODUCT_ID of products which has same no. of ITEMS_SOLD for all 7 days. Also for given PRODUCT_ID I need to find the longest period of successive days where the no. of ITEMS_SOLD is same for all days in this period. Eg.(PRODUCT_ID 23 was sold as following along the week in no. of units sold: 4,6,6,6,6,7,4 .So the longest period is *4 days* from Monday to Thursday.) The first condition is special case of second condition where no. of days is 7.
Any help to get the SQL query will be appreciated.
Thanks,
Akshay.

PRODUCT_ID      DATE           ITEMS_SOLD
1          10/10/2011     3
1          11/10/2011     3
1          12/10/2011     3
1          13/10/2011     3
1           16/10/2011     5
2          10/10/2011     4
2           11/10/2011     4
2          12/10/2011     4
2          13/10/2011     4
2           14/10/2011     4
2          15/10/2011     4
2          16/10/2011     4
Output:
PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
1          3                4     
2          4               7
Explanation of results:
The table to be queried contains data for 1 week: from 10/10/2011(Sunday) to 16/10/2011(Saturday). Now, product with PRODUCT_ID '1' was sold on dates 10,11,12,13,16. Out of these 5 days 3 units were sold on 4 successive days (from 10-13). So output should be like :
PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
1          3               4     
as longest period of successive days is 4 where same no. of units were sold i.e 3 units (other period is of 1 day on 16th ).
For PRODUCT_ID 2 we have only one period of 7 days where 4 units were sold each day. So we output :
PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
2           4               7
Other case where same PRODUCT_ID have different units sold on each day should be ignored.
I hope that clarifies the problem more. Let me know in case I have missed out anything which should have been mentioned.
-Akshay.

Similar Messages

  • Help needed in executing SQL query...

    Hi,
    I am very new to JDeveloper. Curently i am tryin to execute an SQL query from the BPEL process, the output of the query is to be mapped to a variable field from a target xsd file. the query is fairly simple, like
    SELECT emp
    FROM emp_table
    WHERE emp_id=123
    The target field, namely "employee", is an element from the xsd file. I tried using Java embedding activity to connect to the db and execute the query through a piece of Java code, but couldn't find a way to assign the output of the query to the field. however lately i also discovered the Database Adapter services which helps me create a database connection, but still i am not sure as of how to handle and map the output of the query to the variable field.
    Can somebody please help me in resolving the issue either through Java Embed activity or Database Adapter services??
    Thanks in advance
    Anjan

    Anjan,
    I suggest you try the [url http://forums.oracle.com/forums/forum.jspa?forumID=212]BPEL Forum
    John

  • Help needed for SCCM SQL query

    Hello.
    I have the below query to extract the workstations build date as well as the hardware info. This works fine. 
    Select distinct
    v_R_System.Name0,
    v_GS_COMPUTER_SYSTEM.Manufacturer0,
    v_GS_COMPUTER_SYSTEM.Model0,
    v_GS_COMPUTER_SYSTEM_PRODUCT.Version0,
    v_GS_OPERATING_SYSTEM.Caption0,
    v_GS_OPERATING_SYSTEM.CSDVersion0,
    v_GS_OPERATING_SYSTEM.InstallDate0
    From v_R_System
    LEFT JOIN v_GS_OPERATING_SYSTEM ON v_GS_OPERATING_SYSTEM.ResourceID=v_R_System.ResourceID
    LEFT JOIN v_GS_COMPUTER_SYSTEM_PRODUCT ON v_GS_COMPUTER_SYSTEM_PRODUCT.ResourceID=v_R_System.ResourceID
    LEFT JOIN v_GS_COMPUTER_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID=v_R_System.ResourceID
    LEFT JOIN v_GS_SYSTEM_CONSOLE_USAGE ON v_R_System.ResourceID=v_GS_SYSTEM_CONSOLE_USAGE.ResourceID
    Where v_GS_OPERATING_SYSTEM.Caption0 = 'Microsoft Windows 7 Enterprise' and v_R_System.Is_Virtual_Machine0 =0
    order by v_GS_OPERATING_SYSTEM.InstallDate0 desc
    Now, I want a report of count of machines built based on "v_GS_COMPUTER_SYSTEM_PRODUCT.Version0" against every month and year of "v_GS_OPERATING_SYSTEM.InstallDate0".
    For example, I want to know the number of machines under a particular model (which appears under  v_GS_COMPUTER_SYSTEM_PRODUCT.Version0) built in June 2014. In this fashion I want a report for every model count for every month and year available under
    InstallDate0 column)
    Example:
    Jan 2013 ->  ThinkCentre M92p -> 55
    Jan 2013 ->  ThinkCentre M93 -> 40
    Feb 2013 ->  ThinkCentre M92p -> 10
    Feb 2013 ->  ThinkCentre M93 -> 39
    Jan 2014 ->  ThinkCentre M92p -> 20
    Jan 2014 ->  ThinkCentre M93 -> 25
    Feb 2014 ->  ThinkCentre M92p -> 12
    Feb 2014 ->  ThinkCentre M93 -> 35
    Can anyone help?

    After scratching my head a bit, I came up with the below. Do you find any flaw in it?
    Select
    v_GS_COMPUTER_SYSTEM.Manufacturer0 as Manufacturer,
    v_GS_COMPUTER_SYSTEM.Model0 as Model,
    v_GS_COMPUTER_SYSTEM_PRODUCT.Version0 as "Model Info",
    COUNT(*) as "No. of machines built",
    CASE WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '1' THEN 'January'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '2' THEN 'February'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '3' THEN 'March'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '4' THEN 'April'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '5' THEN 'May'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '6' THEN 'June'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '7' THEN 'July'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '8' THEN 'August'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '9' THEN 'September'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '10' THEN 'October'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '11' THEN 'November'
    WHEN Month(v_GS_OPERATING_SYSTEM.InstallDate0) = '12' THEN 'December'END as "Month",
    YEAR(v_GS_OPERATING_SYSTEM.InstallDate0) as "Year"
    From v_GS_COMPUTER_SYSTEM
    LEFT JOIN v_GS_OPERATING_SYSTEM ON v_GS_OPERATING_SYSTEM.ResourceID=v_GS_COMPUTER_SYSTEM.ResourceID
    LEFT JOIN v_GS_COMPUTER_SYSTEM_PRODUCT ON v_GS_COMPUTER_SYSTEM_PRODUCT.ResourceID=v_GS_COMPUTER_SYSTEM.ResourceID
    Where v_GS_OPERATING_SYSTEM.Caption0 = 'Microsoft Windows 7 Enterprise' Group By v_GS_COMPUTER_SYSTEM.Manufacturer0,
    v_GS_COMPUTER_SYSTEM.Model0,
    v_GS_COMPUTER_SYSTEM_PRODUCT.Version0,
    Month(v_GS_OPERATING_SYSTEM.InstallDate0),
    YEAR(v_GS_OPERATING_SYSTEM.InstallDate0)
    Order by v_GS_COMPUTER_SYSTEM_PRODUCT.Version0 desc

  • Need Oracle Payroll SQL Query

    Hello,
    I need Oracle Payroll SQL Query with result:
    First_name, Last_name, Payment_amount, Pay_date, Payroll_Frequency, Employement_status
    Any Help would be greatful appreciated

    You will need the following tales.
    per_all_people_f, per_all_assignments_f,pay_run_results,pay_run_result_values
    Query would e something like :
    select papf.first_name,
             papf.last_name,
             prrv.*
    From  apps.per_all_people_f papf
             apps.per_all_assignments_f paaf
             apps.pay_run_results prr,
             apps.pay_run_result_values prrv
    Where papf.person_id = paaf.person_id
    and papf.business_group_id = paaf.business_group_id
    and papf.current_employee_flag = 'Y'
    and paaf.primary_flag ='Y'
    and paaf.assignment_type = 'E'
    and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
    and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date 
    and prr.assignment_id = paaf.assignment_id

  • Help needed to optimize the query

    Help needed to optimize the query:
    The requirement is to select the record with max eff_date from HIST_TBL and that max eff_date should be > = '01-Jan-2007'.
    This is having high cost and taking around 15mins to execute.
    Can anyone help to fine-tune this??
       SELECT c.H_SEC,
                    c.S_PAID,
                    c.H_PAID,
                    table_c.EFF_DATE
       FROM    MTCH_TBL c
                    LEFT OUTER JOIN
                       (SELECT b.SEC_ALIAS,
                               b.EFF_DATE,
                               b.INSTANCE
                          FROM HIST_TBL b
                         WHERE b.EFF_DATE =
                                  (SELECT MAX (b2.EFF_DATE)
                                     FROM HIST_TBL b2
                                    WHERE b.SEC_ALIAS = b2.SEC_ALIAS
                                          AND b.INSTANCE =
                                                 b2.INSTANCE
                                          AND b2.EFF_DATE >= '01-Jan-2007')
                               OR b.EFF_DATE IS NULL) table_c
                    ON  table_c.SEC_ALIAS=c.H_SEC
                       AND table_c.INSTANCE = 100;

    To start with, I would avoid scanning HIST_TBL twice.
    Try this
    select c.h_sec
         , c.s_paid
         , c.h_paid
         , table_c.eff_date
      from mtch_tbl c
      left
      join (
              select sec_alias
                   , eff_date
                   , instance
                from (
                        select sec_alias
                             , eff_date
                             , instance
                             , max(eff_date) over(partition by sec_alias, instance) max_eff_date
                          from hist_tbl b
                         where eff_date >= to_date('01-jan-2007', 'dd-mon-yyyy')
                            or eff_date is null
               where eff_date = max_eff_date
                  or eff_date is null
           ) table_c
        on table_c.sec_alias = c.h_sec
       and table_c.instance  = 100;

  • Help me write a SQL query; urgent

    Hi, can somebody please help me write a SQL query.
    I have 3 tables each with the same column names (Col1, Col2, Col3). Col1 is PK with Unique Constraint.
    I wanted to add values of Col2 and Col3 (from all 3 tables) and put it in a separate table (i.e aggregated) of all values found in Col1.
    Does anybody help me please ?
    thanks alot.

    Please don't mark your question as urgent. You've been around here long enough that you should know that it will not get your question anwered any faster, and may just irritate people into not answering at all.
    I'm not sure exactly what you want.
    Are you saying you want t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3 for the rows that have the same c1 in all three tables?
    If so, it would be like this, I think: insert into t4
    select t1.c1
    , t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3
    from t1, t2, t3
    where t2.c1 = t1.c1
    and t3.c1 = t1.c1If that's not what you want, please clarify.
    And next time maybe you should post your SQL question in a SQL forum.

  • Need help in rewriting a sql query

    Can any one please tell me if there is any utility that can help me correcting the sql I have I need to tune the query as its taking lot of time. I want to use some tool that will help me re-formating the query.
    Any help in this regard will be highly appreciated.

    If you think that Oracle SQL Tuning Tools like SQL Tuning Advisor and SQL Access Advisor are not helping.
    You might look into thrid party tools like Quest- SQL Navigator and TOAD.
    But I don't advise this based on the following:
    Re: Oracle Third Party Tools and Oracle Database
    Oracle have enough tools of its own to satisfy the various needs.
    Adith

  • Need help in Report From SQL Query

    Hi All,
    I am facing a problem with a report. I need your help.
    I am creating a Report From SQL Query (Portal) with some arguments passed at runtime. I am able to view the output, if the query returns few rows ( arount 1000 rows). But for some inputs it needs to generate >15000 records, at this point the page is getting time out (i think!) and showing error page. I am able to execute query from the SQL Plus console ot using TOAD editor. Here the query is not taking more that 2 mins time to show the result.
    If i am executing from Portal i observed that, once i give the appropriate input and hit submit button a new oracle process is getting created for the query on UNIX (I am usign "TOP" command to check processes). The browser page will be shown error page after 5 minutes (i am assuming session time out!) , but on the backend the process will be executed for more than 30 mins.
    I tried also increase the page time out in httpd.conf, but no use.
    The data returned as a result of the query is sized more than 10 MB. Is caching this much data is possible by the browser page? is the returned data is creating any problem here.
    Please help me to find appropriate reasone for the failure?

    user602513 wrote:
    Hi All,
    I am facing a problem with a report. I need your help.
    I am creating a Report From SQL Query (Portal) with some arguments passed at runtime. I am able to view the output, if the query returns few rows ( arount 1000 rows). But for some inputs it needs to generate >15000 records, at this point the page is getting time out (i think!) and showing error page. I am able to execute query from the SQL Plus console ot using TOAD editor. Here the query is not taking more that 2 mins time to show the result.
    If i am executing from Portal i observed that, once i give the appropriate input and hit submit button a new oracle process is getting created for the query on UNIX (I am usign "TOP" command to check processes). The browser page will be shown error page after 5 minutes (i am assuming session time out!) , but on the backend the process will be executed for more than 30 mins.
    I tried also increase the page time out in httpd.conf, but no use.
    The data returned as a result of the query is sized more than 10 MB. Is caching this much data is possible by the browser page? is the returned data is creating any problem here.
    Please help me to find appropriate reasone for the failure?Do you get any errors or warnings or it is just the slow speed which is the issue?
    There could be a variety of reasons for the delayed processing of this report. That includes parameter settings for that page, cache settings, network configurations, etc.
    - explore best optimization for your query;
    - evaluate portal for best performance configuration; you may follow this note (Doc ID: *438794.1* ) for ideas;
    - third: for that particular page carrying that report, you can use caching wisely. browser cache is neither decent for large files, nor practical. instead, explore the page cache settings that portal provides.
    - also look for various log files (application.log and apache logs) if you are getting any warnings reflecting on some kind of processing halt.
    - and last but not the least: if you happen to bring up a portal report with more than 10000 rows for display then think about the usage of the report. Evaluate whether that report is good/useful for anything?
    HTH
    AMN

  • Need Help with Creating the SQl query

    Hi,
    SQL query gurus...
    INFORMATION:
    I have two table, CURRENT and PREVIOUS.(Table Defs below).
    CURRENT:
    Column1 - CURR_PARENT
    Column2 - CURR_CHILD
    Column3 - CURR_CHILD_ATTRIBUTE 1
    Column4 - CURR_CHILD_ATTRIBUTE 2
    Column5 - CURR_CHILD_ATTRIBUTE 3
    PREVIOUS:
    Column1 - PREV_PARENT
    Column2 - PREV_CHILD
    Column3 - PREV_CHILD_ATTRIBUTE 1
    Column4 - PREV_CHILD_ATTRIBUTE 2
    Column5 - PREV_CHILD_ATTRIBUTE 3
    PROBLEM STATEMENT
    Here the columns 3 to 5 are the attributes of the Child. Lets assume that I have two loads, One Today which goes to the CURRENT table and one yesterday which goes to the PREVIOUS table. Between these two loads there is a CHANGE in the value for Columns either 3/4/5 or all of them(doesnt matter if one or all).
    I want to determine what properties for the child have changed with the help of a MOST efficient SQL query.(PARENT+CHILD is unique key). The Database is ofcourse ORACLE.
    Please help.
    Regards,
    Parag

    Hi,
    The last message was not posted by the same user_name that started the thread.
    Please don't do that: it's confusing.
    Earlier replies give you the information you want, with one row of output (maximum) per row in current_tbl. There may be 1, 2 or 3 changes on a row.
    You just have to unpivot that data to get one row for every change, like this:
    WITH     single_row  AS
         SELECT     c.curr_parent
         ,     c.curr_child
         ,     c.curr_child_attribute1
         ,     c.curr_child_attribute2
         ,     c.curr_child_attribute3
         ,     DECODE (c.curr_child_attribute1, p.prev_child_attribute1, 0, 1) AS diff1
         ,     DECODE (c.curr_child_attribute2, p.prev_child_attribute2, 0, 2) AS diff2
         ,     DECODE (c.curr_child_attribute3, p.prev_child_attribute3, 0, 3) AS diff3
         FROM     current_tbl    c
         JOIN     previous_tbl   p     ON  c.curr_parent     = p.prev_parent
                                AND c.curr_child     = p.prev_child
         WHERE     c.curr_child_attribute1     != p.prev_child_attribute1
         OR     c.curr_child_attribute2     != p.prev_child_attribute2
         OR     c.curr_child_attribute3     != p.prev_child_attribute3
    ,     cntr     AS
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL <= 3
    SELECT     s.curr_parent     AS parent
    ,     s.curr_child     AS child
    ,     CASE     c.n
              WHEN  1  THEN  s.curr_child_attribute1
              WHEN  2  THEN  s.curr_child_attribute2
              WHEN  3  THEN  s.curr_child_attribute3
         END          AS attribute
    ,     c.n          AS attribute_value
    FROM     single_row     s
    JOIN     cntr          c     ON     c.n IN ( s.diff1
                                    , s.diff2
                                    , s.diff3
    ORDER BY  attribute_value
    ,            parent
    ,            child
    ;

  • Help need to create a query

    I need to fetch records from a table. Please help me to create a query
    The Tablename is Employee. It has the following records
    Department Empname Gender
    Finance Tom Male
    Finance Rick Male
    Finance Stacy Female
    Corporate Tom Male
    Corporate Rob Male
    I want to select the value of the Gender field from the Employee table corresponding to a Department
    If all the values in the Gender field are 'MALE' corresponding to 'finance' in the Department field, the value should be 'MALE'
    If there is a value 'FEMALE', the gender corresponding to the Empname 'TOM' should be considered as the gender

    Tables have rows - not records.
    Your question is a basic SQL language question - which means you do not know the SQL language. This forum is not a classroom for teaching you the SQL language.
    Use the following as the basic outline of how your SQL language statement need to look like for selecting the required from the table:
    SELECT
      <<sql projection goes here>>
    FROM <<table name goes here>>
    WHERE <<filter conditions go here>>
    {code}
    The SQL projection specifies the list of columns the SQL need to return to the caller.
    The filter condition is basic predicates and AND and OR  can be used for multiple predicates.
    Go to http://tahiti.oracle.com and look for the +SQL Reference Guide+ for the Oracle version you are using. The +SELECT+ statement syntax is covered in detail and sample statements are provided.
    And please do not expect this forum to be used as a classroom, or expect this forum to do your homework for a class.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Help needed to tune the Query:Statistics added

    Can someone DBA please help me to tune this query:
    SELECT DISTINCT K.ATTRIBUTE_VALUE AGENCY_ID,B.PROFILE_NM ,NVL(G.OFFICE_DESC,'--') OFFICE_DESC,f.OFFICE_ID,B.PROFILE_ID,'%' ROLE,'%' LAYOUT,
    CASE
    WHEN 'flagB' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING')
    WHEN 'flagO' = '%' THEN
    NVL(J.ISS_GRP_DESC,'ORDERING')
    WHEN 'flag' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING/ORDERING')
    ELSE
    NVL(J.ISS_GRP_DESC,' ')
    END ISS_GRP_DESC,
    DECODE(NVL(H.USERID,' ') ,' ','--','<a sbcuid_in=' || H.USERID || ' target=NEW >'||H.FIRSTNAME || ' ' || H.LASTNAME || '( ' || H.USERID || ' )</a>' ) USER_NAME
    FROM
    PROFILE_PORTAL B ,
    TBL_BDA_AGENCY_RESP_REP C ,
    TBL_BDA_AGENCY_OFFICE F,
    TBL_BDA_OFFICE G,
    USERS_PORTAL H,
    TBL_BDA_USR_ISS_GRP I ,
    TBL_BDA_ISS_GROUP J,
    ATTRIBUTE_VALUES_PORTAL K,
    PROFILE_TYPE_PORTAL L
    WHERE
    B.PROFILE_ID = F.AGENCY_ID (+)
    AND B.PROFILE_ID = C.AGENCY_ID (+)
    AND G.OFFICE_ID (+)= F.OFFICE_ID
    AND H.USERID (+)= C.RESP_USR_ID
    AND C.ISS_GRP_ID = I.ISS_GRP_ID (+)
    AND I.ISS_GRP_ID = J.ISS_GRP_ID(+)
    AND 'PROFILE.'||B.PROFILE_ID = K.ENTITY_ID(+)
    AND K.ATTRIBUTE_VALUE IS NOT NULL
    AND L.PROFILE_TYPE_ID = B.PROFILE_TYPE_ID
    AND L.APPLICATION_CD='BDA'
    AND NOT EXISTS (SELECT agency_id
    FROM TBL_BDA_AGENCY_RESP_REP t
    WHERE t.ISS_GRP_ID IN ('%')
    AND t.AGENCY_ID = C.AGENCY_ID)
    AND K.ATTRIBUTE_VALUE LIKE '%'
    AND UPPER(B.PROFILE_NM) LIKE UPPER('%')
    AND (to_char(NVL(B.PROFILE_ID,0)) LIKE '%' OR NVL(B.PROFILE_ID,0) IN ('a'))
    AND NVL(G.OFFICE_ID,0) IN ('%')
    AND (to_char(NVL(C.RESP_USR_ID,'0')) LIKE '%' OR NVL(C.RESP_USR_ID,'0') IN ('k'))
    ORDER BY PROFILE_NM
    The number of rows in these tables are as follows:
    PROFILE_PORTAL -- 2392
    TBL_BDA_AGENCY_RESP_REP 3508
    TBL_BDA_AGENCY_OFFICE 2151
    TBL_BDA_OFFICE 3
    USERS_PORTAL 270500
    TBL_BDA_USR_ISS_GRP 234
    TBL_BDA_ISS_GROUP 2
    ATTRIBUTE_VALUES_PORTAL 2790
    PROFILE_TYPE_PORTAL 3
    The Explain pal nhas given this o/p to me:
    SQL> select * from table(dbms_xplan.display) dual;
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes |TempSpc| Cost |
    | 0 | SELECT STATEMENT | | 807 | 102K| | 2533 |
    | 1 | SORT UNIQUE | | 807 | 102K| 232K| 82 |
    |* 2 | FILTER | | | | | |
    |* 3 | HASH JOIN OUTER | | 807 | 102K| | 52 |
    |* 4 | HASH JOIN OUTER | | 807 | 95226 | | 40 |
    |* 5 | TABLE ACCESS BY INDEX ROWID | ATTRIBUTE_VALUES | 1 | 23 | | 2 |
    | 6 | NESTED LOOPS | | 7 | 805 | | 37 |
    | 7 | NESTED LOOPS OUTER | | 6 | 552 | | 25 |
    |* 8 | FILTER | | | | | |
    | 9 | NESTED LOOPS OUTER | | | | | |
    |* 10 | FILTER | | | | | |
    | 11 | NESTED LOOPS OUTER | | | | | |
    | 12 | NESTED LOOPS OUTER | | 3 | 141 | | 10 |
    |* 13 | HASH JOIN | | 3 | 120 | | 7 |
    |* 14 | TABLE ACCESS FULL | PROFILE | 6 | 198 | | 4 |
    |* 15 | TABLE ACCESS FULL | PROFILE_TYPE | 1 | 7 | | 2 |
    |* 16 | INDEX RANGE SCAN | SYS_C0019777 | 1 | 7 | | 1 |
    | 17 | TABLE ACCESS BY INDEX ROWID| TBL_BDA_OFFICE | 1 | 10 | | 1 |
    |* 18 | INDEX UNIQUE SCAN | SYS_C0019800 | 1 | | | |
    | 19 | TABLE ACCESS BY INDEX ROWID | TBL_BDA_AGENCY_RESP_REP | 2 | 26 | | 2 |
    |* 20 | INDEX RANGE SCAN | IDX_AGECYRESP_AGNCYID | 2 | | | 1 |
    | 21 | TABLE ACCESS BY INDEX ROWID | USER_ | 1 | 22 | | 1 |
    |* 22 | INDEX UNIQUE SCAN | USER_PK | 1 | | | |
    |* 23 | INDEX RANGE SCAN | IDX_ATTVAL_ENTATTID | 1 | | | 1 |
    | 24 | TABLE ACCESS FULL | TBL_BDA_USR_ISS_GRP | 234 | 702 | | 2 |
    | 25 | TABLE ACCESS FULL | TBL_BDA_ISS_GROUP | 2 | 24 | | 2 |
    |* 26 | TABLE ACCESS BY INDEX ROWID | TBL_BDA_AGENCY_RESP_REP | 1 | 7 | | 3 |
    |* 27 | INDEX RANGE SCAN | IDX_AGECYRESP_AGNCYID | 2 | | | 1 |
    Predicate Information (identified by operation id):
    2 - filter( NOT EXISTS (SELECT /*+ */ 0 FROM "TBL_BDA_AGENCY_RESP_REP" "T" WHERE "T"."AGENCY_ID"=:B1
    AND "T"."ISS_GRP_ID"=TO_NUMBER('%')))
    3 - access("I"."ISS_GRP_ID"="J"."ISS_GRP_ID"(+))
    4 - access("SYS_ALIAS_1"."ISS_GRP_ID"="I"."ISS_GRP_ID"(+))
    5 - filter("K"."ATTRIBUTE_VALUE" IS NOT NULL AND "K"."ATTRIBUTE_VALUE" LIKE '%')
    8 - filter(NVL("SYS_ALIAS_1"."RESP_USR_ID",'0') LIKE '%' OR NVL("SYS_ALIAS_1"."RESP_USR_ID",'0')='k')
    10 - filter(NVL("G"."OFFICE_ID",0)=TO_NUMBER('%'))
    13 - access("L"."PROFILE_TYPE_ID"="B"."PROFILE_TYPE_ID")
    14 - filter(UPPER("B"."PROFILE_NM") LIKE '%' AND (TO_CHAR(NVL("B"."PROFILE_ID",0)) LIKE '%' OR
    NVL("B"."PROFILE_ID",0)=TO_NUMBER('a')))
    15 - filter("L"."APPLICATION_CD"='BDA')
    16 - access("B"."PROFILE_ID"="F"."AGENCY_ID"(+))
    18 - access("G"."OFFICE_ID"(+)="F"."OFFICE_ID")
    20 - access("B"."PROFILE_ID"="SYS_ALIAS_1"."AGENCY_ID"(+))
    22 - access("H"."USERID"(+)="SYS_ALIAS_1"."RESP_USR_ID")
    23 - access("K"."ENTITY_ID"='PROFILE.'||TO_CHAR("B"."PROFILE_ID"))
    26 - filter("T"."ISS_GRP_ID"=TO_NUMBER('%'))
    27 - access("T"."AGENCY_ID"=:B1)
    Note: cpu costing is off
    57 rows selected.
    Elapsed: 00:00:01.08
    Please help me.
    Aashish S.

    Hello Eric,
    Here is the code:
    SELECT DISTINCT
    K.ATTRIBUTE_VALUE AGENCY_ID,
    B.PROFILE_NM ,
    NVL(G.OFFICE_DESC,'--') OFFICE_DESC,
    f.OFFICE_ID,
    B.PROFILE_ID,
    '%' ROLE,
    '%' LAYOUT,
    case
    WHEN 'flagB' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING')
    WHEN 'flagO' = '%' THEN
    NVL(J.ISS_GRP_DESC,'ORDERING')
    WHEN 'flag' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING/ORDERING')
    else
    NVL(J.ISS_GRP_DESC,' ')
    END ISS_GRP_DESC,
    DECODE(NVL(H.USERID,' ') ,' ','--','&lt;a sbcuid_in=' || H.USERID || ' target=NEW &gt;'||H.FIRSTNAME || ' ' || H.LASTNAME ||
    '( ' || H.USERID || ' )&lt;/a&gt;' ) USER_NAME
    from
    PROFILE_PORTAL B ,
    TBL_BDA_AGENCY_RESP_REP C ,
    TBL_BDA_AGENCY_OFFICE F,
    TBL_BDA_OFFICE G,
    USERS_PORTAL H,
    TBL_BDA_USR_ISS_GRP I ,
    TBL_BDA_ISS_GROUP J,
    ATTRIBUTE_VALUES_PORTAL K,
    PROFILE_TYPE_PORTAL L
    WHERE
    B.PROFILE_ID = F.AGENCY_ID (+)
    AND B.PROFILE_ID = C.AGENCY_ID (+)
    AND G.OFFICE_ID (+)= F.OFFICE_ID
    AND H.USERID (+)= C.RESP_USR_ID
    AND C.ISS_GRP_ID = I.ISS_GRP_ID (+)
    AND I.ISS_GRP_ID = J.ISS_GRP_ID(+)
    AND 'PROFILE.'||B.PROFILE_ID = K.ENTITY_ID(+)
    AND K.ATTRIBUTE_VALUE IS NOT NULL
    AND L.PROFILE_TYPE_ID = B.PROFILE_TYPE_ID
    AND L.APPLICATION_CD='BDA'
    AND NOT EXISTS
    (SELECT agency_id
    FROM TBL_BDA_AGENCY_RESP_REP t
    WHERE t.ISS_GRP_ID IN (1)
    AND t.AGENCY_ID = C.AGENCY_ID)
    AND K.ATTRIBUTE_VALUE LIKE '%'
    AND UPPER(B.PROFILE_NM) LIKE UPPER('%')
    AND (to_char(NVL(B.PROFILE_ID,0))
    LIKE '%'
    OR NVL(B.PROFILE_ID,0) IN (1))
    AND NVL(G.OFFICE_ID,0) IN (1)
    AND (to_char(NVL(C.RESP_USR_ID,'0'))
    LIKE '%'
    OR NVL(C.RESP_USR_ID,'0') IN ('%'))
    ORDER BY PROFILE_NM
    This is the Query and the query takes some mins. to run in prod environment.
    From the Query plan ,I am not able to get any idea for optimization.
    Now,Can you tell me which steps I need to follow to run it faster and which all modifications should be made?
    Thanks.
    Aashish S.

  • Need to adjust SQL query for left outer join in Crystal Reports 2008.

    I need to change this SQL 2005 query.....
    SELECT     omnicell_anl.DeviceIDsLastMetricTable.member_id, omnicell_anl.DeviceIDsLastMetricTable.[Device Name],
                          omnicell_anl.DeviceIDsLastMetricTable.Account, omnicell_anl.labeledLastMetrics.PROPERTY_NAME,
                          omnicell_anl.labeledLastMetrics.latest_monitor_value
    FROM         omnicell_anl.DeviceIDsLastMetricTable LEFT OUTER JOIN
                          omnicell_anl.labeledLastMetrics ON omnicell_anl.DeviceIDsLastMetricTable.member_id = omnicell_anl.labeledLastMetrics.member_id
    WHERE
        omnicell_anl.labeledLastMetrics.PROPERTY_NAME = ?MyProperty
    To this query in Crystal Reports Report Writer:
    SELECT     omnicell_anl.DeviceIDsLastMetricTable.member_id, omnicell_anl.DeviceIDsLastMetricTable.[Device Name],
                          omnicell_anl.DeviceIDsLastMetricTable.Account, omnicell_anl.labeledLastMetrics.PROPERTY_NAME,
                          omnicell_anl.labeledLastMetrics.latest_monitor_value
    FROM         omnicell_anl.DeviceIDsLastMetricTable LEFT OUTER JOIN
                          omnicell_anl.labeledLastMetrics ON omnicell_anl.DeviceIDsLastMetricTable.member_id = omnicell_anl.labeledLastMetrics.member_id AND
                          omnicell_anl.labeledLastMetrics.PROPERTY_NAME = ?MyProperty
    I can't seem to get the left outer join function of Crystal Reports to emulate the same SQL query in SQL 2005.  Any ideas on how I can create this same query in Crystal 2008?
    Thanks,
    Dominic
    Edited by: Dominic Carissimi on Oct 28, 2008 7:55 PM
    Edited by: Dominic Carissimi on Oct 28, 2008 7:56 PM

    If you want the list of values for command level parameter then you need to add another command like
    select PropertyField from table
    and delete the links between this command and the existing command.
    Now go to field explorer and edit the command level parameter and make it as dynamic and add the property field from newly added command.
    Hope this helps!
    Raghavendra

  • Please help me with this SQL query

    I am practicing SQL queries and have come across one involving fetching data from 3 different tables.
    The three tables are as below
    <pre>
    Country
    location_id          country
    loc1          Spain
    loc2          England
    loc3          Spain
    loc4          USA
    loc5          Italy
    loc6          USA
    loc7          USA
    </pre>
    <pre>
    User
    user_id location_id
    u1 loc1
    u2 loc1
    u3 loc2
    u4 loc2
    u5 loc1
    u6 loc3
    </pre>
    <pre>
    Post
    post_id user_id
    p1 u1
    p2 u1
    p3 u2
    p4 u3
    p5 u1
    p6 u2
    </pre>
    I am trying to write an SQL query - for each country of users, display the average number of posts
    I understand the logic behind this that we first need to group together all the locations and then the users belonging to one country and then find the average of their posts.
    But, i'm having a difficulty in putting this in SQL form. Could someone please help me with this query.
    Thanks.

    select
    country.country,
    count(*) Totalpostspercountry,
    count(distinct post.user_id) Totaldistincuserspercountry,
    count(*)/count(distinct post.user_id) Avgpostsperuserbycountry
    from
    country, muser, post
    where country.location_id = muser.location_id
    and muser.user_id = post.user_id
    group by country.country
    The output is like this for your sample data - hope this is what you were looking for :)
    COUNTRY,TOTALPOSTSPERCOUNTRY,TOTALDISTINCUSERSPERCOUNTRY,AVGPOSTSPERUSERBYCOUNTRY
    England,1,1,1,
    Spain,5,2,2.5,

  • Need a SCCM SQL Query Report for Installed Software with Packages and Applications.

    I need a report that will show the number of installs of all of the workstations applications and packages over a given period of time. 
    This will let us know how effective our deployments are and how well the on-demand software is being adopted.
    Any help?

    Your Install Source might be an option as most SCCM deployments will install from C:\Windows\ccmcache\xx [where xx is a random folder name]. Most software vendors will put an install source in the registry, but not all do, so it won't be completely accurate.
    When software is installed manually, the install source path won't be C:\Windows\ccmcache\xx.
    Here is a SQL query I have for Install Source:
    SELECT v_GS_COMPUTER_SYSTEM.Name0 as 'Computer Name', v_GS_INSTALLED_SOFTWARE.ProductName0 as 'Software Title', v_GS_INSTALLED_SOFTWARE.InstallSource0 as 'Install Source', v_GS_INSTALLED_SOFTWARE.ProductVersion0 as 'Version', v_GS_INSTALLED_SOFTWARE.InstalledLocation0
    as 'Installed Location', v_GS_INSTALLED_SOFTWARE.InstallDate0 as 'Install Date'
    FROM v_GS_COMPUTER_SYSTEM INNER JOIN v_GS_INSTALLED_SOFTWARE ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_INSTALLED_SOFTWARE.ResourceID
    WHERE v_GS_INSTALLED_SOFTWARE.ProductName0 like '%Office 365 Pro%'
    ORDER BY v_GS_COMPUTER_SYSTEM.Name0

  • Need generic dynamic sql query to generate nodes depending on dealer levels

    Input table:
    create table #test(dealerid integer ,dealerlvl integer)
    insert into #test values(1,1)
    insert into #test values(1,2)
    insert into #test values(1,3)
    insert into #test values(1,4)
    insert into #test values(2,1)
    insert into #test values(2,2)
    insert into #test values(2,3)
    insert into #test values(2,4)
    insert into #test values(2,5)
    insert into #test values(2,6)
    go
    create table #test2(dealerid integer,node integer,prntnode integer,dealerlvl integer)
    insert into #test2 values (1,234,124,2)
    insert into #test2 values (1,123,234,1)
    insert into #test2 values (1,238,123,2)
    insert into #test2 values (1,235,238,3)
    insert into #test2 values (1,253,235,4)
    insert into #test2 values (2,21674,124,3)
    insert into #test2 values (2,1233,21674,1)
    insert into #test2 values (2,2144,1233,2)
    insert into #test2 values (2,2354,2144,3)
    insert into #test2 values (2,24353,2354,4)
    insert into #test2 values (2,245213,24353,5)
    insert into #test2 values (2,2213,245213,6)
    Expected result :
    I have two test case here with dealerID1 and dealerID 2 
    Result for DealerID1
    Result needed for DealerID2:
    the levels for dealers might change (Dealer1 has 4 levels, and Dealer 2 has 6 levels) so i need to create an dynamic sql query which lists each node as separate columns depending on the levels. 
    I have hacked the query to give the result I need 
    select a.dealerid,a.node as Lvl1,b.node as lvl2,c.node as lvl3,d.node as lvl4
    from #test2 a 
    join #test2 b on a.node=b.prntnode
    join #test2 c on b.node=c.prntnode
    join #test2 d on c.node=d.prntnode
    where a.dealerid=1 and a.dealerlvl=2
    select  a.dealerid,a.node asLvl1,
    b.node as lvl2,c.node as lvl3,d.node as lvl4,e.node as lvl5,f.node as lvl6--,a.dealerlvl,a.dealerid
    from #test2 a 
    join #test2 b on a.node=b.prntnode
    join #test2 c on b.node=c.prntnode
    join #test2 d on c.node=d.prntnode
    join #test2 e on d.node=e.prntnode
    join #test2 f on e.node=f.prntnode
    where a.dealerid=2 and a.dealerlvl=3
    I am sure there is a better way to do this with dynamic sql. please help.
    Thanks

    -- Dynamic PIVOT
     DECLARE @T AS TABLE(y INT NOT NULL PRIMARY KEY);
    DECLARE
       @cols AS NVARCHAR(MAX),
       @y    AS INT,
       @sql  AS NVARCHAR(MAX);
    -- Construct the column list for the IN clause
     SET @cols = STUFF(
       (SELECT N',' + QUOTENAME(y) AS [text()]
        FROM (SELECT DISTINCT dealerlvl AS y FROM dbo.test2) AS Y
        ORDER BY y
        FOR XML PATH('')),
       1, 1, N'');
    -- Construct the full T-SQL statement
     -- and execute dynamically
     SET @sql = N'SELECT *
     FROM (SELECT dealerid, dealerlvl, node
           FROM dbo.Test2) AS D
       PIVOT(MAX(node) FOR dealerlvl IN(' + @cols + N')) AS P;';
    EXEC sp_executesql @sql;
     GO

Maybe you are looking for