Complex Query which needs tuning

Hello :
I have a complex query that needs to be tuned. I have little experience in tuning the sql and hence taking the help of your guys.
The Query is as given below:
Database version 11g
SELECT DISTINCT P.RESPONSIBILITY, P.PRODUCT_MAJOR, P.PRODUCT_MINOR,
P.PRODUCT_SERIES, P.PRODUCT_CATEGORY AS Category1, SO.REGION_CODE,
SO.STORE_CODE, S.Store_Name, SOL.PRODUCT_CODE, PRI.REPLENISHMENT_TYPE,
PRI.SUPPLIER_CODE,
SOL.SOLD_WITH_NIC, SOL.SUGGESTED_PRICE,
PRI.INVOICE_COST, SOL.FIFO_COST,
SO.ORDER_TYPE_CODE, SOL.DOCUMENT_NUM,
SOS.SLSP_CD, '' AS FNAME, '' AS LNAME,
SOL.PRICE_EXCEPTION_CODE, SOL.AS_IS,
SOL.STATUS_DATE,
Sum(SOL.QUANTITY) AS SumOfQUANTITY,
Sum(SOL.EXTENDED_PRICE) AS SumOfEXTENDED_PRICE
--Format([SALES_ORDER].[STATUS_DATE],"mmm-yy") AS [Month]
FROM PRODUCT P,
PRODUCT_MAJORS PM,
SALES_ORDER_LINE SOL,
STORE S,
SALES_ORDER SO,
SALES_ORDER_SPLITS SOS,
PRODUCT_REGIONAL_INFO PRI,
REGION_MAP R
WHERE P.product_major = PM.PRODUCT_MAJOR
and SOL.PRODUCT_CODE = P.PRODUCT_CODE
and SO.STORE_CODE = S.STORE_CODE
AND SO.REGION_CODE = S.REGION_CODE
AND SOL.REGION_CODE = SO.REGION_CODE
AND SOL.DOCUMENT_NUM = SO.DOCUMENT_NUM
AND SOL.DELIVERY_SEQUENCE_NUM = SO.DELIVERY_SEQUENCE_NUM
AND SOL.STATUS_CODE = SO.STATUS_CODE
AND SOL.STATUS_DATE = SO.STATUS_DATE
AND SO.REGION_CODE = SOS.REGION_CODE
AND SO.DOCUMENT_NUM = SOS.DOCUMENT_NUM
AND SOL.PRODUCT_CODE = PRI.PRODUCT_CODE
AND PRI.REGION_CODE = R.CORP_REGION_CODE
AND SO.REGION_CODE = R.DS_REGION_CODE
AND P.PRODUCT_MAJOR In ('STEREO','TELEVISION','VIDEO')
AND SOL.STATUS_CODE = 'D'
AND SOL.STATUS_DATE BETWEEN '01-JUN-09' AND '30-JUN-09'
AND SO.STORE_CODE NOT IN
('10','20','30','40','70','91','95','93','94','96','97','98','99',
'9V','9W','9X','9Y','9Z','8Z',
'8Y','92','CZ','FR','FS','FT','FZ','FY','FX','FW','FV','GZ','GY','GU','GW','GV','GX')
GROUP BY
P.RESPONSIBILITY, P.PRODUCT_MAJOR, P.PRODUCT_MINOR, P.PRODUCT_SERIES, P.PRODUCT_CATEGORY,
SO.REGION_CODE, SO.STORE_CODE, /*S.Short Name, */
S.Store_Name, SOL.PRODUCT_CODE,
PRI.REPLENISHMENT_TYPE, PRI.SUPPLIER_CODE,
SOL.SOLD_WITH_NIC, SOL.SUGGESTED_PRICE, PRI.INVOICE_COST,
SOL.FIFO_COST, SO.ORDER_TYPE_CODE, SOL.DOCUMENT_NUM,
SOS.SLSP_CD, '', '', SOL.PRICE_EXCEPTION_CODE,
SOL.AS_IS, SOL.STATUS_DATE
Explain Plan:
SELECT STATEMENT, GOAL = ALL_ROWS               Cost=583     Cardinality=1     Bytes=253
HASH GROUP BY               Cost=583     Cardinality=1     Bytes=253
FILTER                         
NESTED LOOPS               Cost=583     Cardinality=1     Bytes=253
HASH JOIN OUTER               Cost=582     Cardinality=1     Bytes=234
NESTED LOOPS                         
NESTED LOOPS               Cost=571     Cardinality=1     Bytes=229
NESTED LOOPS               Cost=571     Cardinality=1     Bytes=207
NESTED LOOPS               Cost=569     Cardinality=2     Bytes=368
NESTED LOOPS               Cost=568     Cardinality=2     Bytes=360
NESTED LOOPS               Cost=556     Cardinality=3     Bytes=435
NESTED LOOPS     Cost=178     Cardinality=4     Bytes=336
NESTED LOOPS          Cost=7     Cardinality=1     Bytes=49
HASH JOIN               Cost=7     Cardinality=1     Bytes=39
VIEW     Object owner=CORP     Object name=index$_join$_015     Cost=2     Cardinality=3     Bytes=57
HASH JOIN                         
INLIST ITERATOR                         
INDEX UNIQUE SCAN     Object owner=CORP     Object name=PRODMJR_PK     Cost=0     Cardinality=3     Bytes=57
INDEX FAST FULL SCAN     Object owner=CORP     Object name=PRDMJR_PR_FK_I     Cost=1     Cardinality=3     Bytes=57
VIEW     Object owner=CORP     Object name=index$_join$_016     Cost=4     Cardinality=37     Bytes=740
HASH JOIN                         
INLIST ITERATOR                         
INDEX RANGE SCAN     Object owner=CORP     Object name=PRDMNR1     Cost=3     Cardinality=37     Bytes=740
INDEX FAST FULL SCAN     Object owner=CORP     Object name=PRDMNR_PK     Cost=4     Cardinality=37     Bytes=740
INDEX UNIQUE SCAN     Object owner=CORP     Object name=PRODMJR_PK     Cost=0     Cardinality=1     Bytes=10
MAT_VIEW ACCESS BY INDEX ROWID     Object owner=CORP     Object name=PRODUCTS     Cost=171     Cardinality=480     Bytes=16800
INDEX RANGE SCAN     Object owner=CORP     Object name=PRD2     Cost=3     Cardinality=681     
TABLE ACCESS BY INDEX ROWID     Object owner=DS     Object name=SALES_ORDER_LINE     Cost=556     Cardinality=1     Bytes=145
BITMAP CONVERSION TO ROWIDS                         
BITMAP INDEX SINGLE VALUE     Object owner=DS     Object name=SOL2               
TABLE ACCESS BY INDEX ROWID     Object owner=DS     Object name=SALES_ORDER     Cost=4     Cardinality=1     Bytes=35
INDEX RANGE SCAN     Object owner=DS     Object name=SO1     Cost=3     Cardinality=1     
TABLE ACCESS BY INDEX ROWID     Object owner=DS     Object name=REGION_MAP     Cost=1     Cardinality=1     Bytes=4
INDEX RANGE SCAN     Object owner=DS     Object name=REGCD     Cost=0     Cardinality=1     
MAT_VIEW ACCESS BY INDEX ROWID     Object owner=CORP     Object name=PRODUCT_REGIONAL_INFO     Cost=2     Cardinality=1     Bytes=23
INDEX UNIQUE SCAN     Object owner=CORP     Object name=PRDRI_PK     Cost=1     Cardinality=1     
INDEX UNIQUE SCAN     Object owner=CORP     Object name=BI_STORE_INFO_PK     Cost=0     Cardinality=1     
MAT_VIEW ACCESS BY INDEX ROWID     Object owner=CORP     Object name=BI_STORE_INFO     Cost=1     Cardinality=1     Bytes=22
VIEW     Object owner=DS     cost=11     Cardinality=342     Bytes=1710
HASH JOIN               Cost=11     Cardinality=342     Bytes=7866
MAT_VIEW ACCESS FULL     Object owner=CORP     Object name=STORE_CORP     Cost=5     Cardinality=429     Bytes=3003
NESTED LOOPS               Cost=5     Cardinality=478     Bytes=7648
MAT_VIEW ACCESS FULL     Object owner=CORP     Object name=STORE_GROUP     Cost=5     Cardinality=478     Bytes=5258
INDEX UNIQUE SCAN     Object owner=CORP     Object name=STORE_REGIONAL_INFO_PK     Cost=0     Cardinality=1     Bytes=5
INDEX RANGE SCAN     Object owner=DS     Object name=SOS_PK     Cost=2     Cardinality=1     Bytes=19
Regards,
BMP

First thing that i notice in this query is you are Using Distinct as well as Group by.
Your group by will always give you distinct results ,then again why do you need the Distinct?
For example
WITH t AS
     (SELECT 'clm1' col1, 'contract1' col2,10 value
        FROM DUAL
      UNION ALL
      SELECT 'clm1' , 'contract1' ,10 value
        FROM DUAL
      UNION ALL
      SELECT 'clm1', 'contract2',10
        FROM DUAL
      UNION ALL
      SELECT 'clm2', 'contract1',10
        FROM DUAL
      UNION ALL
      SELECT 'clm3', 'contract1',10
        FROM DUAL
      UNION ALL
      SELECT 'clm4', 'contract2',10
        FROM DUAL)
SELECT  distinct col1,col2,sum(value) from t
group by col1,col2Is always same as
WITH t AS
     (SELECT 'clm1' col1, 'contract1' col2,10 value
        FROM DUAL
      UNION ALL
      SELECT 'clm1' , 'contract1' ,10 value
        FROM DUAL
      UNION ALL
      SELECT 'clm1', 'contract2',10
        FROM DUAL
      UNION ALL
      SELECT 'clm2', 'contract1',10
        FROM DUAL
      UNION ALL
      SELECT 'clm3', 'contract1',10
        FROM DUAL
      UNION ALL
      SELECT 'clm4', 'contract2',10
        FROM DUAL)
SELECT  col1,col2,sum(value) from t
group by col1,col2And also
AND SOL.STATUS_DATE BETWEEN '01-JUN-09' AND '30-JUN-09'It would be best to use a to_date when hard coding your dates.
Edited by: user5495111 on Aug 6, 2009 1:32 PM

Similar Messages

  • Complex query advice needed

    I have a mysql table that basically represents a log of daily events. I need to be able to get the total number of events for each week in a year. How can I efficiently query the database and tally the toal records for each week in a year, given that my events are recorded daily? Any advice would be much appreciated. Thanks.

    Presumably the events have a timestamp.
    One possibility (you need to deal with border conditions)
    1. Find a function that returns day of the year from a timestamp (dayInYear)
    2. Determine offset from beginning of year to 'first week' in year. (Offset)
    3. week = (dayInYear(timestamp) - Offset)/7
    4. Write a query that does a sum using group by on the week value.

  • How can i imrove peroformance of complex Query

    How can i improve performance of complex Query
    which used CASE WHEN , GROUP By Rollup
    for example
    SELECT CASE WHEN null then a
    ELSE b END
    FROM (
    SELECT a,b,c,..........
    FROM table1
    UNION ALL
    SELECT a,b,c,..........
    FROM table2
    GROUP BY ROLLUP a,b,c ,............
    this query take 18 min to return 180000 record
    is there is any way to improve the performance of this query

    Hello
    Have you considered using
    ALTER SYSTEM SET _fast=TRUE;-)
    Alternatively you could provide some more information such as an execution plan for your query:
    Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.4.0 - Production
    SQL> set linesize 200
    SQL> set pages 50000
    SQL> EXPLAIN PLAN FOR
      2  select * from dual;
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id  | Operation            |  Name       | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT     |             |       |       |       |
    |   1 |  TABLE ACCESS FULL   | DUAL        |       |       |       |
    Note: rule based optimization
    9 rows selected.If you want general info on tuning, I suggest you read the Perfomance tuning guide and the Data Warehousing guide. These are the best place to find information about concepts and techniques. The forums are better suited to specific questions.
    HTH
    David

  • Trying to form complex query - need help

    I have a fairly complex query that I need to join the results of to show actual and goal by day. The actuals are an aggregation of records that get put in every day, while the targets are a single entry in range format indicating an active range for which the target applies. I'm working on a query that will put things together by month and I'm running into a snag. Can someone please point out where appropriate naming needs to go to get this to come together?
    This one works:
    (select DATE_INDEX, SUM(LDS) as TTLLDS, SUM(TONS) as TTLTONS from
        (select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
           (select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
                 CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
                      ELSE SUM(w.SPOT_WEIGHT)
                  END as WT
              from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
             where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
               and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
               and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
               and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
             group by c.DATE_INDEX, c.VEH_LOC
            union
            select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
                 DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
              from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
             where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
               and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
               and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
             group by c.DATE_INDEX, c.VEH_LOC)
          group by DATE_INDEX, VEH_LOC)
      group by DATE_INDEX)Now I need to add in the following query:
    select (u.MACH_TPH_D+u.MACH_TPH_N)/2 as MTPH_TGT, (u.LABOR_TPH_D+u.LABOR_TPH_N)/2 as LTPH_TGT
         from UTIL_TARGET_LOADERS u
        where u.ORG_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)The join needs to be based on VEH_LOC and DAY in the form:
       ... WHERE u.ORG_ID = x.VEH_LOC
              AND x.DATE_INDEX between u.START_DATE and NVL(u.END_DATE,sysdate)I had one that worked just fine when only one entity was involved; the complication arises in that this is a division-level report so I have to individually resolve the subordinates and their goals before I can aggregate. This is one of two queries I need to tie together using a WITH clause so I can pivot the whole thing and present it in month-by-month fashion. When I try to tie it together like the query below, I get: invalid relational operator.
    select ttls.DATE_INDEX, SUM(ttls.LDS) as TTLLDS, SUM(ttls.TONS) as TTLTONS, u.TARGET_LTPH, u.TARGET_MTPH
      from UTIL_TARGET_LOADERS u,
        (select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
           (select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
                 CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
                      ELSE SUM(w.SPOT_WEIGHT)
                  END as WT
              from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
             where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
               and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
               and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
               and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
             group by c.DATE_INDEX, c.VEH_LOC
            union
            select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
                 DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
              from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
             where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
               and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
               and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
             group by c.DATE_INDEX, c.VEH_LOC)
          group by DATE_INDEX, VEH_LOC) ttls
        where ttls.DATE_INDEX beween u.START_DATE and NVL(u.END_DATE,sysdate)
          and ttls.VEH_LOC = u.ORG_ID
      group by ttls.DATE_INDEXI know this is a nested mess, as it has to grab the production from two tables for a range of VEH_LOC values and sum and aggregate by day and VEH_LOC, then I have to try and match that to the targets based on VEH_LOC and day. My final query is to aggregate the whole mess of sums and averages by month.
    I'd appreciate it if someone can point me in the right direction.

    Figured it out.
    select ttl.DATE_INDEX, SUM(ttl.LDS) as TTLLDS, SUM(ttl.TONS) as TTLTONS,
         AVG((u.MACH_TPH_D+u.MACH_TPH_N)/2) as MTPH_TGT,
         AVG((u.LABOR_TPH_D+u.LABOR_TPH_N)/2) as LTPH_TGT
      from
        (select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
           (select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
                 CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
                      ELSE SUM(w.SPOT_WEIGHT)
                  END as WT
              from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
             where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
               and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
               and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
               and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
             group by c.DATE_INDEX, c.VEH_LOC
            union
            select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
                 DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
              from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
             where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
               and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
               and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
             group by c.DATE_INDEX, c.VEH_LOC)
          group by DATE_INDEX, VEH_LOC) ttl, UTIL_TARGET_LOADERS u
      where u.ORG_ID = ttl.VEH_LOC
        and ttl.DATE_INDEX between u.START_DATE and NVL(U.END_DATE,sysdate)
      group by ttl.DATE_INDEX, (u.LABOR_TPH_D+u.LABOR_TPH_N)/2

  • NEED HELP IN REDUCING THE LENGTH OF QUERY WHICH IS ATTACHED IN A FILE PL_SQ

    Hi Everyone,
    THANKS FOR POSTING THE REPLIES FOR MY LAST THREAD. THAT REALLY HELPED. I CAME BACK AGAIN BECAUSE I NEED HELP FROM THIS FORUM IN REDUING THE LENGTH OF MY QUERY WHICH I HAD GIVEN BELOW. i DIDN'T HAD THE CHOICE OF ATTACHING A FILE OF MY DATA HERE.ITS VERY HUGE DATA SO I AM NOT ABLE TO PROVIDE ATLEAST SAMPLE DATA BECAUSE THE COLUMNS ARE OVERLAPPING IF I AM PASTING THE DATA HERE. I HOPE IT WILL BE UNDERSTANDABLE. THE QUERY WHICH I WROTE IS TOO LONG AND TAKING HOURS TOGETHER TO RUN AND DISPLAY RESULT. IT WOULD BE A GREAT FAVOUR TO ME IF ANYONE CAN REDUCE THE LINES IN MY QUERY AND MAKE IT RUN FAST....PLEASE I WAS NOT KNOWING THE WAY HOW TO MAKE IT SHORT. IT HAS MANY LOGICS IN IT... :? PLEASE SUGGEST ME IF THERE IS ANY OPTION FOR ATTACHING A FILE SO THAT I CAN SEND MY SAMPLE DATA...
    select pt.product code,
    pt.product status,
    pt.store code,
    pt.store status,
    pr.scheduled date,
    pr.actual date,
    pl.scheduled date,
    pl.actual date,
    ps.scheduled date,
    ps.actual date,
    fo.scheduled date,
    fo.actual date,
    fb.scheduled date,
    fb.actual date,
    fod.scheduled date,
    fod.actual date,
    lo.scheduled date,
    lo.actual date,
    lom.scheduled date,
    lom.actual date,
    lod.scheduled date,
    lod.actual date,
    /*pr*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(pr.scheduled date,
    cast(pr.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Planned' THEN
    NVL2(pr.scheduled date,
    cast(pr.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_pr_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    pr.scheduled date is null THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    pr.scheduled date < CURRENT_DATE THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    pr.scheduled date >= CURRENT_DATE THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'Planned' and
    pr.scheduled date is null THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'Planned' and
    pr.scheduled date < CURRENT_DATE THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Planned' and
    pr.scheduled date >= CURRENT_DATE THEN
    NVL2(pr.actual date,
    cast(pr.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_pr_ACT,
    /*pl*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(pl.scheduled date,
    cast(pl.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Planned' THEN
    NVL2(pl.scheduled date,
    cast(pl.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_pl_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    pl.scheduled date is null THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    pl.scheduled date < CURRENT_DATE THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    pl.scheduled date >= CURRENT_DATE THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'Planned' and
    pl.scheduled date is null THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'Planned' and
    pl.scheduled date < CURRENT_DATE THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Planned' and
    pl.scheduled date >= CURRENT_DATE THEN
    NVL2(pl.actual date,
    cast(pl.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_pl_ACT,
    /*ps*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(ps.scheduled date,
    cast(ps.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' THEN
    NVL2(ps.scheduled date,
    cast(ps.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_ps_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    ps.scheduled date is null THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    ps.scheduled date < CURRENT_DATE THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    ps.scheduled date >= CURRENT_DATE THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    ps.scheduled date is null THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    ps.scheduled date < CURRENT_DATE THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    ps.scheduled date >= CURRENT_DATE THEN
    NVL2(ps.actual date,
    cast(ps.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_ps_ACT,
    /*fo*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(fo.scheduled date,
    cast(fo.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' THEN
    NVL2(fo.scheduled date,
    cast(fo.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_fo_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fo.scheduled date is null THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fo.scheduled date < CURRENT_DATE THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fo.scheduled date >= CURRENT_DATE THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    fo.scheduled date is null THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    fo.scheduled date < CURRENT_DATE THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    fo.scheduled date >= CURRENT_DATE THEN
    NVL2(fo.actual date,
    cast(fo.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_fo_ACT,
    /*fb*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(fb.scheduled date,
    cast(fb.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' THEN
    NVL2(fb.scheduled date,
    cast(fb.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_fb_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fb.scheduled date is null THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fb.scheduled date < CURRENT_DATE THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fb.scheduled date >= CURRENT_DATE THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    fb.scheduled date is null THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    fb.scheduled date < CURRENT_DATE THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    fb.scheduled date >= CURRENT_DATE THEN
    NVL2(fb.actual date,
    cast(fb.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_fb_ACT,
    /*fod*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Compldeted' OR
    pt.store status = 'Active' THEN
    NVL2(fod.scheduled date,
    cast(fod.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'pldanned' THEN
    NVL2(fod.scheduled date,
    cast(fod.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_fod_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fod.scheduled date is null THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fod.scheduled date < CURRENT_DATE THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    fod.scheduled date >= CURRENT_DATE THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    fod.scheduled date is null THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    fod.scheduled date < CURRENT_DATE THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    fod.scheduled date >= CURRENT_DATE THEN
    NVL2(fod.actual date,
    cast(fod.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_fod_ACT,
    /*lo*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(lo.scheduled date,
    cast(lo.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' THEN
    NVL2(lo.scheduled date,
    cast(lo.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_lo_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lo.scheduled date is null THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lo.scheduled date < CURRENT_DATE THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lo.scheduled date >= CURRENT_DATE THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    lo.scheduled date is null THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    lo.scheduled date < CURRENT_DATE THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    lo.scheduled date >= CURRENT_DATE THEN
    NVL2(lo.actual date,
    cast(lo.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_lo_ACT,
    /*lom*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(lom.scheduled date,
    cast(lom.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' THEN
    NVL2(lom.scheduled date,
    cast(lom.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_lom_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lom.scheduled date is null THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lom.scheduled date < CURRENT_DATE THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lom.scheduled date >= CURRENT_DATE THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    lom.scheduled date is null THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    lom.scheduled date < CURRENT_DATE THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    lom.scheduled date >= CURRENT_DATE THEN
    NVL2(lom.actual date,
    cast(lom.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_lom_ACT,
    /*lod*/
    case
    WHEN pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' OR
    pt.store status = 'Active' THEN
    NVL2(lod.scheduled date,
    cast(lod.scheduled date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' THEN
    NVL2(lod.scheduled date,
    cast(lod.scheduled date AS nvarchar2(30)),
    'Null')
    end as DER_lod_SHC,
    case
    when pt.store status = 'Cancelled' OR
    pt.store status = 'Stopped' THEN
    cast('N/A' AS nvarchar2(30))
    WHEN pt.store status = 'Completed' THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lod.scheduled date is null THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lod.scheduled date < CURRENT_DATE THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'Active' and
    lod.scheduled date >= CURRENT_DATE THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    lod.scheduled date is null THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'N/A')
    when pt.store status = 'planned' and
    lod.scheduled date < CURRENT_DATE THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'Null')
    when pt.store status = 'planned' and
    lod.scheduled date >= CURRENT_DATE THEN
    NVL2(lod.actual date,
    cast(lod.actual date AS nvarchar2(30)),
    'N/A')
    end as DER_lod_ACT
    from
    product table pt,
    event table pr,
    event table pl,
    event table ps,
    event table fo,
    event table fb,
    event table fod,
    event table lo,
    event table lom,
    event table lod
    where
    pt.product code = pr.product code
    and pt.store code = pr.store code
    and pt.product code = pl.product code
    and pt.store code = pl.store code
    and pt.product code = fo.product code
    and pt.store code = fo.store code
    and pt.product code = fb.product code
    and pt.store code = fb.store code
    and pt.product code = fod.product code
    and pt.store code = fod.store code
    and pt.product code = lo.product code
    and pt.store code = lo.store code
    and pt.product code = lom.product code
    and pt.store code = lom.store code
    and pt.product code = lod.product code
    and pt.store code = lod.store code
    and pr.product code=pl.product code
    and pl.store code=ps.store code
    and ps.product code=fo.product code
    and fo.store code=fb.store code
    and fb.product code=fod.product code
    and fod.store code=lo.store code
    and lo.product code=lom.product code
    and lom.store code=lod.store code
    and pr.event code=1001
    and pl.event code=1002
    and ps.event code=1003
    and fo.event code=1004
    and fb.event code=1005
    and fod.event code=1006
    and lo.event code=1007
    and lom.event code=1008
    and lod.event code=1009
    order by product code,store code;
    THANKS IN ADVANCE FOR SPARING YOUR TIME IN UNDERSTANDING MY PROVLEM....
    REGARDS,
    MYTHRI.

    986300 wrote:
    from
    product table pt,
    event table pr,
    event table pl,
    event table ps,
    event table fo,
    event table fb,
    event table fod,
    event table lo,
    event table lom,
    event table lodabove is NOT valid SQL syntax

  • Need a record from second query which is not a part of main query.

    I have this Query which Leads me to two Rwos of Data
    select papf.employee_number E_CODE
    ,to_char(paaf1.effective_start_date,'DD-MON-RRRR') EFFECTIVE_START_DATE
    ,DECODE(to_char(paaf1.effective_end_date,'DD-MON-RRRR'),'31-DEC-4712',NULL,to_char(paaf1.effective_end_date,'DD-MON-RRRR')) EFFECTIVE_END_DATE
    ,TRIM(SUBSTR(PAAF1.ASS_ATTRIBUTE21,INSTR(PAAF1.ASS_ATTRIBUTE21,'-')+1)) PREVIOUS_CO
    from apps.per_all_assignments_f paaf
    ,apps.per_all_people_f papf
    ,apps.per_grades pg
    ,apps.per_jobs pj
    ,apps.per_person_types ppt
    ,apps.per_person_type_usages_f pptuf
    ,apps.per_all_assignments_f paaf1
    where 1=1
    and papf.person_id = paaf.person_id
    and pptuf.person_id = papf.person_id
    and pptuf.person_type_id = ppt.person_type_id
    and ppt.user_person_type = 'Employee'
    and papf.current_employee_flag ='Y'
    and paaf.primary_flag = 'Y'
    and paaf1.primary_flag = 'Y'
    and paaf1.grade_id = pg.grade_id
    and paaf1.job_id = pj.job_id
    and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
    and trunc(sysdate) between pptuf.effective_start_date and pptuf.effective_end_date
    and papf.person_id = paaf1.person_id
    and (TRIM(UPPER(paaf1.ass_attribute24)) <> TRIM(UPPER(paaf.ass_attribute24))
    OR TRIM(UPPER(paaf1.ass_attribute21)) <> TRIM(UPPER(paaf.ass_attribute21))
    OR TRIM(UPPER(paaf1.ass_attribute22)) <> TRIM(UPPER(paaf.ass_attribute22))
    OR TRIM(UPPER(paaf1.ass_attribute25)) <> TRIM(UPPER(paaf.ass_attribute25))
    OR TRIM(UPPER(paaf1.ass_attribute23)) <> TRIM(UPPER(paaf.ass_attribute23))
    OR paaf1.grade_id <> paaf.grade_id)
    and paaf1.effective_end_date = paaf.effective_start_date - 1
    and papf.employee_number in ('10620')
    and paaf1.effective_start_date >= '01-JAN-1950'
    ---------------------------OUT PUT-----------------------------
    E_CODE     EFFECTIVE_START_DATE     EFFECTIVE_END_DATE     PREVIOUS_CO
    Row1 10620     17-SEP-2009     30-NOV-2009     CORPORATE
    Row2 10620     19-NOV-2007     31-JAN-2008     CORPORATE
    Problem is enire output of the query is perfectly fine but in the second row at column effective_start_date insted of 19-NOV-2007 in need a value from another query. there must not be any change in rest of the columns data including first row.
    i.e select ORIGINAL_DATE_OF_HIRE from per_all_people_f
    where employee_number = '10620'
    and rownum < 2
    ---------------------------OUT PUT----------------------------
    15-MAY-2006
    Is there is any approach to get this thing.
    Thanks in advance
    Bachan.
    Edited by: Bachan on Sep 20, 2010 8:17 PM

    maybe a union for your second row.
    select E_CODE,
           EFFECTIVE_START_DATE,
           EFFECTIVE_END_DATE,
           PREVIOUS_CO
      from (select rownum rn,
                   papf.employee_number E_CODE
                   ,to_char(paaf1.effective_start_date,'DD-MON-RRRR') EFFECTIVE_START_DATE
                   ,DECODE(to_char(paaf1.effective_end_date,'DD-MON-RRRR'),'31-DEC-4712',NULL,to_char(paaf1.effective_end_date,'DD-MON-RRRR')) EFFECTIVE_END_DATE
                   ,TRIM(SUBSTR(PAAF1.***_ATTRIBUTE21,INSTR(PAAF1.***_ATTRIBUTE21,'-')+1)) PREVIOUS_CO
              from apps.per_all_assignments_f paaf
                   ,apps.per_all_people_f papf
                   ,apps.per_grades pg
                   ,apps.per_jobs pj
                   ,apps.per_person_types ppt
                   ,apps.per_person_type_usages_f pptuf
                   ,apps.per_all_assignments_f paaf1
             where 1=1
               and papf.person_id = paaf.person_id
               and pptuf.person_id = papf.person_id
               and pptuf.person_type_id = ppt.person_type_id
               and ppt.user_person_type = 'Employee'
               and papf.current_employee_flag ='Y'
               and paaf.primary_flag = 'Y'
               and paaf1.primary_flag = 'Y'
               and paaf1.grade_id = pg.grade_id
               and paaf1.job_id = pj.job_id
               and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
               and trunc(sysdate) between pptuf.effective_start_date and pptuf.effective_end_date
               and papf.person_id = paaf1.person_id
               and (TRIM(UPPER(paaf1.***_attribute24)) TRIM(UPPER(paaf.***_attribute24))
               OR TRIM(UPPER(paaf1.***_attribute21)) TRIM(UPPER(paaf.***_attribute21))
               OR TRIM(UPPER(paaf1.***_attribute22)) TRIM(UPPER(paaf.***_attribute22))
               OR TRIM(UPPER(paaf1.***_attribute25)) TRIM(UPPER(paaf.***_attribute25))
               OR TRIM(UPPER(paaf1.***_attribute23)) TRIM(UPPER(paaf.***_attribute23))
               OR paaf1.grade_id paaf.grade_id)
               and paaf1.effective_end_date = paaf.effective_start_date - 1
               and papf.employee_number in ('10620')
               and paaf1.effective_start_date >= '01-JAN-1950'0
      where rn = 1
    union all
    select employee_number E_CODE,
           ORIGINAL_DATE_OF_HIRE,
           EFFECTIVE_END_DATE,
           TRIM(SUBSTR(PAAF1.***_ATTRIBUTE21,INSTR(PAAF1.***_ATTRIBUTE21,'-')+1)) PREVIOUS_CO
      from per_all_people_f
    where employee_number = '10620'
       and rownum < 2note: untested

  • Need complex query  with joins and AGGREGATE  functions.

    Hello Everyone ;
    Good Morning to all ;
    I have 3 tables with 2 lakhs record. I need to check query performance.. How CBO rewrites my query in materialized view ?
    I want to make complex join with AGGREGATE FUNCTION.
    my table details
    SQL> select from tab;*
    TNAME TABTYPE CLUSTERID
    DEPT TABLE
    PAYROLL TABLE
    EMP TABLE
    SQL> desc emp
    Name
    EID
    ENAME
    EDOB
    EGENDER
    EQUAL
    EGRADUATION
    EDESIGNATION
    ELEVEL
    EDOMAIN_ID
    EMOB_NO
    SQL> desc dept
    Name
    EID
    DNAME
    DMANAGER
    DCONTACT_NO
    DPROJ_NAME
    SQL> desc payroll
    Name
    EID
    PF_NO
    SAL_ACC_NO
    SALARY
    BONUS
    I want to make  complex query  with joins and AGGREGATE  functions.
    Dept names are : IT , ITES , Accounts , Mgmt , Hr
    GRADUATIONS are : Engineering , Arts , Accounts , business_applications
    I want to select records who are working in IT and ITES and graduation should be "Engineering"
    salary > 20000 and < = 22800 and bonus > 1000 and <= 1999 with count for males and females Separately ;
    Please help me to make a such complex query with joins ..
    Thanks in advance ..
    Edited by: 969352 on May 25, 2013 11:34 AM

    969352 wrote:
    why do you avoid providing requested & NEEDED details?I do NOT understand what do you expect ?
    My Goal is :
    1. When executing my own query i need to check expalin plan.please proceed to do so
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9010.htm#SQLRF01601
    2. IF i enable query rewrite option .. i want to check explain plan ( how optimizer rewrites my query ) ? please proceed to do so
    http://docs.oracle.com/cd/E11882_01/server.112/e16638/ex_plan.htm#PFGRF009
    3. My only aim is QUERY PERFORMANCE with QUERY REWRITE clause in materialized view.It is an admirable goal.
    Best Wishes on your quest for performance improvements.

  • Need SQL Query which saves query results to an excel file

    Hi,
    I am looking for a sql query which saves/ export results of an query to a excel file
    Thanks,
    Chetan

    I am looking for a sql query which saves/ export results of an query to a excel file
    Thanks,
    Chetan
    Options:
    1) You can use
    BCP command to export result to excel
    2) Insert into excel by using OPENROWSET
    3) Use Import and Export Wizard and provide SQL query as source and Excel as destination
    Please refer:
    http://stackoverflow.com/questions/87735/how-do-you-transfer-or-export-sql-server-2005-data-to-excel
    http://www.codeproject.com/Questions/617527/Export-sql-query-result-in-excel-file
    Cheers,
    Vaibhav Chaudhari
    [MCTS],
    [MCP]

  • How to create a Service based on complex query

    Hi,
    I'm using JDev 11.1.2.2.0 for developing fusion based application based on EJB3.0 and JPA. I search on net and got a link for sample application as shown below:
    http://www.oracle.com/webfolder/technetwork/tutorials/obe/jdev/obe11jdev/ps1/ejb/ejb.html#t2s1
    But above application is based on "Entities based on Tables" but my requirement to build a service based on query which contains multiple tables which means I want to create a generic one...
    please let me know what I need to change or please provide any link for docs.

    Hi Desmukh,
    I want to create a wsdl for complex queries based on ADF fusion applicaiton using EJB3.0 and JPA...!
    the link which you provided 'entity with tables' which is restricting for my requirement but I want similar implementation for complex queries :(

  • Complex query help

    Hope someone can help with this.
    I have a database structure :
    table : photos
    Photo_ID (PK)
    Title
    Orientation
    etc
    table : keywords
    Keyword_ID (PK)
    Keyword
    Category
    table : photokeywords
    Photo_ID
    Keyword_ID
    I have a search page that uses checkboxes allowing users to
    search on keywords, that returns any photos which match the
    selected keywords.
    In the search page the checkboxes are created using an array,
    and are displayed as :
    //Display the checkbox
    echo "<td width=\"2%\">";
    echo "<input type=\"checkbox\"
    class=\"tickbox_".$row_type."\"";
    if (in_array($keyword['Keyword_ID'],$photokeywords)) { echo
    " checked"; }
    echo " name=\"ckbox[".$keyword['Keyword_ID']."]\"
    id=\"ckbox[".$keyword['Keyword_ID']."]\">";
    echo "</td>\n";
    Someone kindly helped me out some time ago to write a fairly
    complex (for me!) query that counts the matches, so enabling the
    results to be ordered by the number of matches, ie the most
    relevant matches first.
    The code for this is :
    //db session
    mysql_select_db($database_Photolibrary, $Photolibrary);
    if (isset($_GET['ckbox'])){
    // get profile keys
    $ckbox = array_keys($_GET['ckbox']);
    // sql string
    $sql = 'SELECT *, Count(*) As rank FROM photos, photokeywords
    WHERE photos.Photo_ID =
    photokeywords.Photo_ID AND photokeywords.Keyword_ID IN(' .
    implode(',', $ckbox).') GROUP BY
    photos.Photo_ID ORDER BY rank DESC, Title';
    $photos = mysql_query($sql) or die(mysql_error());
    $row_photos = mysql_fetch_assoc($photos);
    @$totalRows = mysql_num_rows($photos);
    The page I have is using Tom Muck's pages list and horizontal
    looper behaviours to display the results.
    Everything works fine except the pages list, where there is
    more than one page of results. I've emailed the ever helpful Tom
    who has advised me that the query may need to be constructed in DW,
    as the Pages List behaviour relies on DW's code to manage the
    paging.
    So I've gotten as far as the basic query code :
    <?php
    mysql_select_db($database_Photolibrary, $Photolibrary);
    $query_photos = "SELECT *, Count(*) As rank FROM photos,
    photokeywords WHERE photos.Photo_ID = photokeywords.Photo_ID AND
    photokeywords.Keyword_ID IN(' . implode(',', $ckbox).') GROUP BY
    photos.Photo_ID ORDER BY rank DESC, Title";
    $photos = mysql_query($query_photos, $Photolibrary) or
    die(mysql_error());
    $row_photos = mysql_fetch_assoc($photos);
    $totalRows_photos = mysql_num_rows($photos);
    ?>
    But am unsure where I need to call the ckbox values,
    previously :
    if (isset($_GET['ckbox'])){
    // get profile keys
    $ckbox = array_keys($_GET['ckbox']);
    in the DW construction.
    I've been playing around with different things in the query's
    variables, such as
    Name : Keyword_ID
    Default value : 1
    Run time value : $_GET['ckbox']
    but to no avail.
    If anyone can help out with this it'd be much appreciated.
    Cheers.

    Something like this?
    SQL> var P_DISCOUNT_RATE number
    SQL> var P_BASE_YEAR number
    SQL> var P_FUTURE_YEAR number
    SQL> exec :P_DISCOUNT_RATE := 0.1; :P_BASE_YEAR := 2003; :P_FUTURE_YEAR := 2009;
    PL/SQL-procedure is geslaagd.
    SQL> select sum(npd)
      2    from ( select nvl
      3                  ( ( select sum(sal)
      4                        from emp
      5                       where to_number(to_char(hiredate,'yyyy')) + 25 = :P_FUTURE_YEAR - l
      6                    )
      7                  , 0
      8                  ) / power(1 + :P_DISCOUNT_RATE, l) npd
      9             from ( select level-1 l from dual connect by level <= 1 + :P_FUTURE_YEAR - :P_BASE_YEAR )
    10         )
    11  /
                                  SUM(NPD)
    22248,89010313503176012567447578717301
    1 rij is geselecteerd.I counted 7 iterations for the values 2003 and 2009, so you might want to explain a little more.
    Regards,
    Rob.

  • Complex query

    I am novice in PL/Sql. I need some help in writing a complex query.
    I imagine the following table structure.
    Type(string) Date(date) count(int)
    Given a date range
    The query needs to group the records on type and return the record (type and count) which has the max date (its just date, no time is involved) for each group (which is based on type) . If there are more than one records which have the max date then the average count should be returned for that type
    i would be glad if someone could give any ideas as to how to go about this query. Thanking you in advance.
    regards.

    Heres the query ... Forget the period .... wht this query is supposed to do is group on assigned ki for a given date range. then it has to get the value which is for the last record for that Ki in the date range. If there are 2 records which have max date then it should give an average.
    e.g
    assignedKI / date / value
    a 1st may 2008 10
    b 2nd may 2008 12
    c 1st may 2008 13
    a 30 - apr-2008 16
    b 4th may 2008 17
    a 1st may 2008 20
    The query should return
    a 1st may 2008 15 (which is the average as there are 2 values for the max date for a)
    b 4th may 2008 17
    c 1st may 2008 13
    the following query doesnot work ....
    SELECT
    kiv2.assigned_k_i,
    kiv2.ki_name,
    kiv.period,
    max( kiv2.ki_value_date) ,
    avg(kiv2.ki_value)
    FROM
    SELECT
    assigned_k_i,
    period p1,
    TO_CHAR(ki_value_date, period) period,
    MAX(ki_value_date) ki_value_date
    FROM
    v_ki_value,
    (select ? period from dual)
         WHERE
         (status = 'APPROVED' OR status = 'AUTOAPPROVED')
    AND ( trunc(to_date(to_char(ki_value_date, 'DD/MM/YYYY'), 'DD/MM/YYYY')) >= ?)
    AND ( trunc(to_date(to_char(ki_value_date, 'DD/MM/YYYY'), 'DD/MM/YYYY')) &lt;= ?)
    AND (INSTR(?, TO_CHAR(assigned_k_i)) > 0)
    GROUP BY
    assigned_k_i,
    TO_CHAR(ki_value_date, period)
    ) kiv,
    v_ki_value kiv2
    WHERE
    kiv.assigned_k_i = kiv2.assigned_k_i
    AND to_char(kiv.ki_value_date, kiv.p1) = to_char(kiv2.ki_value_date, kiv.p1)
    GROUP BY
    kiv2.assigned_k_i,
    kiv2.ki_name,
    kiv.period,
    kiv2.ki_value
    ORDER BY
    kiv2.assigned_k_i

  • Complex Query in toplink descriptor

    I have a complex query with "Order by" and "Max" aggregation function and I am trying to convert it to a named query. I am using toplink descriptor in Jdeveloper 11G.
    1. Is it possible to write a named query which returns values from more than two different table?
    2. how to specify Max operation in the Toplink descriptor.
    Here is my Query
    select * from table1 t1 ,table2 t2
    where
    t1.id  = t2.id  and
    t1.name ='Name' and
    t1.status<>'DELETE' and
    t2.a_id = (select max(a_id) from table2 t21 where
    t21.a_id = t1.id and
    group by id)

    TopLink supports sub-selects through sub-queries in TopLink Expressions, and through JPQL. I'm not sure on the JDev support though, you may need to use a code customizer or amendment to add the named query.
    James : http://www.eclipselink.org

  • How to store data from a complex query and only fresh hourly or daily?

    We have a report which runs quite slow (1-2 minutes) because the query is quite complicate, so we would like to run this query daily only and store in a table so for those procedures that need to use this complex query as a subquery, can just join to this table directly to get results.
    However, I am not sure what kind of object I should use to store data for this complex query. Is data in global temp table only persist within transaction? I need something that can persist the data and be access by procedures.
    Any suggestions are welcome,
    Cheers

    Thank you for your reply. I looked at the materialized view earlier on, but have some difficulties to use it. So I have some questions here:
    1.The complex query is not a sum or aggregate functions, it just need to get data from different tables based on different conditions, in this case is it still appropriate to use meterialized view?
    2.If it is, I created one, but how to use it in my procedure? From the articles I read, it seems I can't just query from this view directly. So do I need to keep the complex query in my procedure and how the procedure will use the meterialized view instead?
    3. I also put the complex query in a normal view, then create a materialized view for this normal view (I expect the data from the complex query will be cache here), then in the procedure I just select * from my_NormalView, but it takes the same time to run even when I set the QUERY_REWRITE_ENABLED to true in the alter session. So I am not sure what else I need to do to make sure the procedure use the materialized view instead of the normal view. Can I query from the Materialized View directly?
    Below in the code I copied from one of the article to create the materialized view based on my normal view:
    CREATE MATERIALIZED VIEW HK3ControlDB.MW_RIRating
    PCTFREE 5 PCTUSED 60
    TABLESPACE "USERS"
    STORAGE (INITIAL 50K NEXT 50K)
    USING INDEX STORAGE (INITIAL 25K NEXT 25K)
    REFRESH START WITH ROUND(SYSDATE + 1) + 11/24
    NEXT NEXT_DAY(TRUNC(SYSDATE), 'MONDAY') + 15/24
    enable query rewrite
    AS SELECT * FROM HK3ControlDB.VW_RIRating;
    Cheers

  • Complex query crashing database!

    Hi
    Our 10g database runs on Windows 2003 (with 32 GB RAM on server).
    Whenever a user is running a particular complex query, the database just crashes suddenly.
    In fact, it doesn't write anything in alert log file for any problem.
    The user starts the query, it runs for sometime, then suddenly database crashes!
    Only way to work there after is to restart the database.
    Although I asked the user to not to run that query again but it is generated by some application which may generate similar query again!
    So, how I can prevent database from crashing again?
    Will creating a resource profile to allocate finite resource help? Problem is since database crashes so suddenly, I can't find any info from log files!
    Thanx

    The database instance crashes. We have to start the ORACLE.EXE again to up the database.
    Here is last few lines of the alert.log
    Fri Feb 05 11:14:59 2010
    Setting recovery target incarnation to 1
    Fri Feb 05 11:14:59 2010
    Successful mount of redo thread 1, with mount id 1911309359
    Fri Feb 05 11:14:59 2010
    Database mounted in Exclusive Mode
    Completed: alter database mount exclusive
    Fri Feb 05 11:14:59 2010
    alter database open
    Fri Feb 05 11:15:00 2010
    Beginning crash recovery of 1 threads
    parallel recovery started with 7 processes
    Fri Feb 05 11:15:00 2010
    Started redo scan
    Fri Feb 05 11:15:00 2010
    Completed redo scan
    3886 redo blocks read, 276 data blocks need recovery
    Fri Feb 05 11:15:00 2010
    Started redo application at
    Thread 1: logseq 179837, block 3
    Fri Feb 05 11:15:00 2010
    Recovery of Online Redo Log: Thread 1 Group 6 Seq 179837 Reading mem 0
      Mem# 0 errs 0: F:\DB LOG FILES\ORADATA\REDO06.LOG
      Mem# 1 errs 0: F:\DB LOG FILES MULTIPLEXED\ORADATA\REDO06.LOG
    Fri Feb 05 11:15:00 2010
    Completed redo application
    Fri Feb 05 11:15:00 2010
    Completed crash recovery at
    Thread 1: logseq 179837, block 3889, scn 10584777673
    276 data blocks read, 276 data blocks written, 3886 redo blocks read
    Fri Feb 05 11:15:01 2010
    LGWR: STARTING ARCH PROCESSES
    ARC0 started with pid=21, OS id=5752
    Fri Feb 05 11:15:01 2010
    ARC0: Archival started
    ARC1: Archival started
    ARC1 started with pid=22, OS id=4548
    Fri Feb 05 11:15:02 2010
    LGWR: STARTING ARCH PROCESSES COMPLETE
    Thread 1 advanced to log sequence 179838
    Thread 1 opened at log sequence 179838
      Current log# 7 seq# 179838 mem# 0: F:\DB LOG FILES\ORADATA\REDO07.LOG
      Current log# 7 seq# 179838 mem# 1: F:\DB LOG FILES MULTIPLEXED\ORADATA\REDO07.LOG
    Successful open of redo thread 1
    Fri Feb 05 11:15:02 2010
    ARC1: STARTING ARCH PROCESSES
    Fri Feb 05 11:15:02 2010
    ARC0: Becoming the 'no FAL' ARCH
    ARC0: Becoming the 'no SRL' ARCH
    Fri Feb 05 11:15:02 2010
    SMON: enabling cache recovery
    Fri Feb 05 11:15:02 2010
    ARC2: Archival started
    ARC1: STARTING ARCH PROCESSES COMPLETE
    ARC2 started with pid=23, OS id=6120
    Fri Feb 05 11:15:02 2010
    Successfully onlined Undo Tablespace 1.
    Fri Feb 05 11:15:02 2010
    SMON: enabling tx recovery
    Fri Feb 05 11:15:02 2010
    Database Characterset is WE8MSWIN1252
    replication_dependency_tracking turned off (no async multimaster replication found)
    Starting background process QMNC
    QMNC started with pid=25, OS id=5312
    Fri Feb 05 11:15:03 2010
    Completed: alter database open
    Fri Feb 05 11:15:03 2010
    ARC1: Becoming the heartbeat ARCH
    Fri Feb 05 11:16:01 2010
    Shutting down archive processes
    Fri Feb 05 11:16:06 2010
    ARCH shutting down
    ARC2: Archival stopped

  • Working with multiple results of a complex query

    Hi all!
    As I "advance" in learning PL/SQL with oracle, I now get stuck in handling multiple results of a complex query. As far as I know, I cannot use a cursor here, as there is no table where the cursor could point to.
    Here is the concept of what I want to do (pseudocode):
    foreach result in SELECT * FROM table_1, table_n WHERE key_1 = foreign_key_in_n;
        -- do someting with the resultHere is my attemt, that freezes the browser gui and throws an internal database error:
    declare
        type t_stock is record(
                baggage_id  baggage.baggage_id%type,
                section_id  sections.section_id%type,
                shelf_id    shelves.shelf_id%type
        v_stock t_stock;
        rcnt number(2);
    begin
    dbms_output.put_line(TO_CHAR(rcnt));
    loop
         SELECT COUNT(*) INTO rcnt FROM (
                   SELECT baggage.baggage_id, sections.section_id, shelves.shelf_id
                   FROM baggage, sections, shelves
                   WHERE baggage.baggage_id = sections.contained_baggage_id
                        AND shelves.is_connex_to_section_id = sections.section_id);
         IF rcnt <= 0 THEN
              exit;
         END IF;
         SELECT baggage.baggage_id, sections.section_id, shelves.shelf_id INTO v_stock
         FROM baggage, sections, shelves
         WHERE baggage.baggage_id = sections.contained_baggage_id
              AND shelves.is_connex_to_section_id = sections.section_id
              AND ROWNUM < 2;
         UPDATE sections SET contained_baggage_id = NULL WHERE section_id = v_stock.baggage_id;
         commit; -- do I need that?
    end loop;
    END;
    /So, is there a way to traverse a list of results from a complex query? Maybe without creating a temporary table (or is that the better way?).
    regards, Alex
    I reformatted the code
    pktm

    Ok, here are the details:
    The tables are used to model kind of a transport system. There are terminals connected with sections that may contain 1 piece of baggage. The baggage is moved by a procedure through a transport system. After each of these "moving steps", I check if the baggage is in front of the shelf it should be in.
    [To be honest, the give statement doesn't contain the info, in which shelf the baggage wil bee inserted. That was spared out because of the lack of a working piece of code :)]
    But: if we consider the fact, that a baggage is in front of such a shelf in the way, that it should be put in this shelf, then all this makes some sense.
    - move baggae through a transport system
    - see if you can put baggage into a shelf
    In order to "put baggage in a shelf", I need to remove it from the transport section. As the transport system is not normalized, I need to update the section where the baggage was in.
    Uhm... yes it's a task that doesn't make too much sense. It seems to be some kind of general spirit in university homework :)
    But: the FOR r IN (Statement) lloks good. I'll use that.
    And, the ROWNUM < 2 is used to limit the size of the result to 1, there is no need to have a specific ordering. It's just because - afaik - oracle doesn't have a limit clause. I would appreciate your help if you know a better way to do limit resultsets.
    best regards, Alex

Maybe you are looking for

  • Problem with share

    Hello, i've made a new film and now i'm ready. until today i've never any problems with imovie but now i can't  share my film. the button doesn't open a selection. Can anyone help me? Thanks

  • Time zone menu does not appear when I turn on time zone support

    I am trying to change the time zone on my calendar.  When I turn on the time zone support in advanced preferences no time zone menu appears. Does anyone know another way to change the time zone?

  • IS-Utilities FI-CA

    Hi all, im a billing consultant,we have an issue related to FI-CA. where early we did not have 4% VAT for adhoc(billing when the consumption is zero/unknown) billing. Now we need to add 4% VAT for this transaction type. How to configure this process?

  • How to designate path to the correct Preview folder within Lightroom

    If I was asked what is the best and most inthuitive progra for photography : I would say Lightroom. If I was asked wich is the most dangerous, unnatural and complicated software for a photographer : I'd definitaly say Lightroom ! In the catalogs pref

  • PowerBook Sleeve

    After much debate and consideration, I've decided to purchase a PowerBook 12". I'm a student, so I plan to carry it with me in my "regular" backpack. I've been looking at all the cases and sleeves that are available, but, because there are so many, I