UNION VS DISTINCT

Hi All,
I have a question on union. is doing a select distinct and a union expensive or select and then union without a distinct?
I know UNION does a distinct internally. Basically I need to know which is costlier?
SELECT   DISTINCT SEC_MSTR_ID FROM od_order
UNION
SELECT   DISTINCT SEC_MSTR_ID FROM od_order
Explain Plan:
Plan
SELECT STATEMENT  CHOOSECost: 2,973  Bytes: 81,756  Cardinality: 13,626                 
     4 SORT UNIQUE  Cost: 2,973  Bytes: 81,756  Cardinality: 13,626            
          3 UNION-ALL       
               1 TABLE ACCESS FULL TABLE D_OD.OD_ORDER Cost: 1,031  Bytes: 1,118,880  Cardinality: 186,480 
               2 TABLE ACCESS FULL TABLE D_OD.OD_ORDER Cost: 1,031  Bytes: 1,118,880  Cardinality: 186,480  VS
SELECT   SEC_MSTR_ID FROM od_order
UNION
SELECT   SEC_MSTR_ID FROM od_order
Explain plan:
Plan
SELECT STATEMENT  CHOOSECost: 4,034  Bytes: 2,237,760  Cardinality: 372,960                 
     4 SORT UNIQUE  Cost: 4,034  Bytes: 2,237,760  Cardinality: 372,960            
          3 UNION-ALL       
               1 TABLE ACCESS FULL TABLE D_OD.OD_ORDER Cost: 1,031  Bytes: 1,118,880  Cardinality: 186,480 
               2 TABLE ACCESS FULL TABLE D_OD.OD_ORDER Cost: 1,031  Bytes: 1,118,880  Cardinality: 186,480  Which one is costly? Logically I feel both are same?
Thanks,
Saff

Solomon:
You might want to try with a bigger table. I can replicate both the different estimates for the number of rows returned, and the cost difference using a larger table.
SQL> select gtnctl_run_no from prregis_t
  2  union
  3  select gtnctl_run_no from prregis_t;
Execution Plan
Plan hash value: 3806563321
| Id  | Operation         | Name               | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT  |                    |  7386K|    28M|       | 10733  (52)| 00:03:37 |
|   1 |  SORT UNIQUE      |                    |  7386K|    28M|   169M| 10733  (52)| 00:03:37 |
|   2 |   UNION-ALL       |                    |       |       |       |            |          |
|   3 |    INDEX FULL SCAN| A91034_0_PRREGIS_T |  3693K|    14M|       |    40   (0)| 00:00:01 |
|   4 |    INDEX FULL SCAN| A91034_0_PRREGIS_T |  3693K|    14M|       |    40   (0)| 00:00:01 |
SQL> select distinct gtnctl_run_no from prregis_t
  2  union
  3  select distinct gtnctl_run_no from prregis_t;
Execution Plan
Plan hash value: 3806563321
| Id  | Operation         | Name               | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT  |                    |  1656 |  6624 |   469  (92)| 00:00:10 |
|   1 |  SORT UNIQUE      |                    |  1656 |  6624 |   469  (92)| 00:00:10 |
|   2 |   UNION-ALL       |                    |       |       |            |          |
|   3 |    INDEX FULL SCAN| A91034_0_PRREGIS_T |  3693K|    14M|    40   (0)| 00:00:01 |
|   4 |    INDEX FULL SCAN| A91034_0_PRREGIS_T |  3693K|    14M|    40   (0)| 00:00:01 |
----------------------------------------------------------------------------------------My guess would be that the optimizer in generating its cost and cardinality estimates for the distinct query takes the distinct into account, but does not actually do the distinct on each subset and then the final resultset.
John

Similar Messages

  • Union all-distinct and remove duplicates from nested table?

    Hi all,
    I need a select that will bulk collect some data in my nested table.
    I have two tables from which I need to select all the accounts only once.(remove duplicates).
    Tried to search on the forum...but no luck.
    I have a table with one column:
    create table a1(account_no number);
    and a second table with 3 columns.
    create table a2 (account_no number, name number, desc varchar2 (100));
    I have a nested table like:
    table of a2%rowtype;
    Can I select from this two table in one select and put in my nested table just one row per account?
    if a I have in a 2a row like :
    1 'test' 'test2'
    2 aaaa aa
    and in a1 a row like:
    1
    I want to put in my nested table just (1, null,null and 2,aaaa, aa)) or (1,test,test2 and 2,aaaa, aa). it does no matter what row from those two I insert.
    Second question:
    If I use:
    BANNER
    Oracle9i Release 9.2.0.5.0 - Production
    PL/SQL Release 9.2.0.5.0 - Production
    CORE     9.2.0.6.0     Production
    TNS for 32-bit Windows: Version 9.2.0.5.0 - Production
    NLSRTL Version 9.2.0.5.0 - Production
    SQL>
    what is the best solution to remove duplicates from a neste table like mine?
    I thought that I can build another nested table and loop in my first nt and for each row I check in there was the same account in previous lines.
    it will be like:
    for i in 1....nt_first.count loop
    for j in 1..i loop
        --check if my line was in previous lines. if it was...do not move it in my second collection
    end loop;it is this best option in oracle 9i?

    I have a table with one column:
    create table a1(account_no number);
    and a second table with 3 columns.
    create table a2 (account_no number, name number, desc varchar2 (100));
    all I need are the accounts. the rest ar extra data
    that I can ignore in this step. But if it is
    available, it is ok to use it.
    using one select in this case is is much better that
    trying to remove duplicates parsing some nested table
    with FOR many times?
    Thankshi,
    try to use union. Union automatically removes duplicates between two or more tables.
    with t1 AS
           (select '3300000' account_no FROM DUAL UNION
            select '6500000' account_no FROM DUAL union
            select '6500000' account_no FROM DUAL union
            select '6500000' account_no FROM DUAL union
            select '6500000' account_no FROM DUAL
           select * from t1ACCOUNT_NO
    3300000
    6500000

  • UNION statement in ABAP SQL

    Hi,
    How to achieve the UNION SQL operation results in ABAP? To be specific, I want to retrieve STCD1 field value from vendor master table (LFA1) for all vendors (LIFNR) that exist in table BSIK or BSAK.
    And I want to achieve the results in single SQL statement. So in essence, the resultant SQL would be something like:
    SELECT STCD1
    INTO TABLE my_internal_table
    FROM LFA1
    WHERE LIFNR IN (SELECT DISTINCT LIFNR
                                                FROM BSIK
                                UNION
                                SELECT DISTINCT LIFNR
                                               FROM BSAK)
    But the UNION is not a valid keyword in ABAP. Any idea how to achieve this?
    Regards,
    Chetan

    hi chetan,
    do you have any where condition to filter the records from bsak and bsik?
    your requirement cannot be realized in a single statement.
    you have to use two separate selects from bsak and bsik, then, collect all the vendors in one internal table and at last you have to use this table to get the vendor info from lfa1.
    regards,
    ravi

  • Distinct count of values which are displayed in two columns in SSAS

    Hi,
    How is it possible to get distinct count of two columns in fact table, meaning how many different appearances are in both columns.
    Please note I DO NOT mean distinct count on the concatenated fields.
    I’ll demonstrate with example:
    Fact table- deals with CustomerID and SupplierID and deals data.
    I need distinct count of companies which are either customer/ supplier in the fact table.
    (making a concatenated field and distinct count on that would not give what I need).
    Ex: fact :
    Deal1, Customer X, Supplier Y …
    Deal2, Customer X, Supplier Z …
    Distinct count of companies in the above should be 3.
    How can this be done?
    Thanks
    Namnami
    Anyone?? There must be a way to do this?!

    Are you trying to do this using the SSAS model, or just off the data using SQL?  If you were to just use SQL I would create two sub queries and union them to remove duplicates.
    ie.
    SELECT DISTINCT customer FROM table_1
    UNION
    SELECT DISTINCT supplier FROM table_1
    This will give you one unique master list.

  • SQL Query Tuning with  UNIONs

    HI:
    Can some please give some hints for the better performane of the following query:
    Select a.Hedged_Trade_ID, min(a.Trade_Date) Trade_Date, a.DealStartDate, a.MaturityDate
    FROM
    ( Select      distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From          CMS_FUTURES_TRANS a, CMS_PAS_ACCT_TRADE b
    Where     trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           b.org_trade_id = to_number(decode(substr(a.Hedged_Trade_Id,1,1), 'U', 0, a.Hedged_Trade_Id))
    AND           b.fgic_company = paramCompany
    UNION
    Select     distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From          CMS_SWAP_ALLOC a, CMS_PAS_ACCT_TRADE b
    Where           trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           b.org_trade_id = to_number(decode(substr(a.Hedged_Trade_Id,1,1), 'U', 0, a.Hedged_Trade_Id))
    AND           b.fgic_company = paramCompany
    UNION
    Select      distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   to_date('01/01/2001', 'mm/dd/yyyy') DealStartDate,
                   to_date('01/01/9999', 'mm/dd/yyyy') MaturityDate
    From           CMS_FUTURES_TRANS a
    Where      trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.hedged_trade_id IN (     Select     unassigned_id
              From     cms_fas_company
              Where      company_id = paramCompany)
    UNION
    Select      distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   to_date('01/01/2001', 'mm/dd/yyyy') DealStartDate,
                   to_date('01/01/9999', 'mm/dd/yyyy') MaturityDate
    From          CMS_SWAP_ALLOC a
    Where           trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.hedged_trade_id IN (     Select      unassigned_id
         From      cms_fas_company
         Where      company_id = paramCompany)
    UNION
    Select      distinct to_char(a.Org_Trade_id) Hedged_Trade_ID,
                   a.History_Date Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From           CMS_PAS_ACCT_TRADE_HIST a, CMS_PAS_ACCT_TRADE b
    Where           trunc(a.LAST_UPDATE) = to_date(paramRunDate + 1, 'mm/dd/yyyy')
    AND           a.History_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           b.org_trade_id = a.org_trade_id
    AND           b.fgic_company = paramCompany
    UNION
    Select          distinct c.Hedged_Trade_id,
                   DECODE(ABS(to_date(a.History_Date,'mm/dd/yyyy') - to_date(c.Trade_Date,'mm/dd/yyyy')),                     to_date(a.History_Date,'mm/dd/yyyy') - to_date(c.Trade_Date,'mm/dd/yyyy'),                     to_date(a.History_Date,'mm/dd/yyyy'), to_date(c.Trade_Date,'mm/dd/yyyy')) Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From           CMS_PAS_ACCT_TRADE_HIST a, CMS_PAS_ACCT_TRADE b, CMS_SWAP_ALLOC c
    Where           c.swap_trade_id = a.org_trade_id
    AND           b.org_trade_id = to_number(decode(substr(c.hedged_trade_id,1,1), 'U', 0, c.hedged_trade_id))
    AND           b.fgic_company = paramCompany
    AND           trunc(a.LAST_UPDATE) = to_date(paramRunDate + 1, 'mm/dd/yyyy')
    AND           trunc(a.History_Date) < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           c.trade_date =      (     Select     MAX(trade_date)
              from     CMS_SWAP_ALLOC d
              Where     d.swap_trade_id = c.swap_trade_id
              AND     d.trade_date <= to_date(paramRunDate, 'mm/dd/yyyy')) ) a
    Group By a.Hedged_Trade_ID, a.DealStartDate, a.MaturityDate;

    Overall I suggest taking each one of your selects and running an explain plan to confirm they are behaving well in your database.
    How many rows does this select return? Big IN lists can be bad
        a.hedged_trade_id in(select unassigned_id
                                        from   cms_fas_company
                                        where  company_id = paramcompany)This can be rewritten
       and exists (select 1
                        from   cms_fas_company
                        where  company_id = paramcompany
                        and unassigned_id  =  a.hedged_trade_id in
    )

  • Outer Joins with multiple conditions - alternatives to UNION?

    It is my understanding that Oracle 8i does not directly support mutiple conditions for outer joins. For instance, I'm looking for PEOPLE who may, or may not, be on MEDICATIONS.
    All MEDICATIONS are listed with DRUGNAME and DRUGID. There thousand of different drugs. I'm only looking for PEOPLE on either one or two of them (to make it simple) or no drug at all (ignoring all other DRUGS they're on..) IF they are on the DRUG, it's gerts printed.
    I'd ideally do a LEFT OUTER JOIN to do this with multiple conditions:
    SELECT DISTINCT
    PERSON.NAME,
    MEDICATION.DRUGNAME,
    MEDICATION.DRUGID
    FROM
    PERSON,
    MEDICATION
    WHERE
    PERSON.ID = MEDICATION.ID (+) AND
    (MEDICATION.DRUGID (+) = 632 OR
    MEDICATION.DRUGID (+) = 956)
    This, of course, is not valid, at least in 8i...
    So I've taken the UNION approach:
    SELECT DISTINCT
    PERSON.NAME,
    MEDICATION.DRUGNAME,
    MEDICATION.DRUGID
    FROM
    PERSON,
    MEDICATION
    WHERE
    PERSON.ID = MEDICATION.ID (+) AND
    MEDICATION.DRUGID (+) = 632
    UNION
    SELECT DISTINCT
    PERSON.NAME,
    MEDICATION.DRUGNAME,
    MEDICATION.DRUGID
    FROM
    PERSON,
    MEDICATION
    WHERE
    PERSON.ID = MEDICATION.ID (+) AND
    MEDICATION.DRUGID (+) = 956
    This of course, does work, but as I've added more drugs this becomes quite unwieldly and not really sure what this does to performance. (There are also several more joins to other tables, not relevent here.)
    In addition, if I import this into Crystal Reports 8.5 (as opposed to the Crystal Query Designer), it refuses to recognize the UNION.
    -- Any suggestions for alternative syntax ???
    -- Has this been addressed in 9i ???
    Thanks,
    Will

    You could try
    select Distinct Person.Name, Med.DrugName, Med.DrugId
    from Person,
    (select ID, DrugName, DrugId from Medication
    where DrugId in (632, 956) ) Med
    where Person.ID = Med.ID(+)
    SELECT DISTINCT
    PERSON.NAME,
    MEDICATION.DRUGNAME,
    MEDICATION.DRUGID
    FROM
    PERSON,
    MEDICATION
    WHERE
    PERSON.ID = MEDICATION.ID (+) AND
    (MEDICATION.DRUGID (+) = 632 OR
    MEDICATION.DRUGID (+) = 956)
    This, of course, is not valid, at least in 8i...
    So I've taken the UNION approach:
    SELECT DISTINCT
    PERSON.NAME,
    MEDICATION.DRUGNAME,
    MEDICATION.DRUGID
    FROM
    PERSON,
    MEDICATION
    WHERE
    PERSON.ID = MEDICATION.ID (+) AND
    MEDICATION.DRUGID (+) = 632
    UNION
    SELECT DISTINCT
    PERSON.NAME,
    MEDICATION.DRUGNAME,
    MEDICATION.DRUGID
    FROM
    PERSON,
    MEDICATION
    WHERE
    PERSON.ID = MEDICATION.ID (+) AND
    MEDICATION.DRUGID (+) = 956
    This of course, does work, but as I've added more drugs this becomes quite unwieldly and not really sure what this does to performance. (There are also several more joins to other tables, not relevent here.)
    In addition, if I import this into Crystal Reports 8.5 (as opposed to the Crystal Query Designer), it refuses to recognize the UNION.
    -- Any suggestions for alternative syntax ???
    -- Has this been addressed in 9i ???
    Thanks,
    Will

  • Error in union

    SELECT distinct m_product_id,m_locator_id, sum(movementqty) AS opstk,to_char('0') as clstk,to_char('0') as movementqty,to_char('0')as MI,to_char('0') as INV,to_char('0') as GR,to_char('0') as GS,to_char('0') as MO
    from m_transaction
    where m_product_id='1136094' and m_locator_id='1000021'
    and movementdate < '09-oct-2010'
    GROUP BY m_product_id,m_locator_id
    union
    SELECT distinct m_product_id,m_locator_id, to_char('0') as opstk,sum(movementqty) AS clstk,to_char('0') as movementqty,to_char('0')as MI,to_char('0') as INV,to_char('0') as GR,to_char('0') as GS,to_char('0') as MO
    from m_transaction
    where m_product_id='1136094' and m_locator_id='1000021'
    and movementdate <'22-oct-2010'
    GROUP BY m_product_id,m_locator_id
    when using this query it shows error message in oracle 10g
    "expression must have same datatypes as corresponding expression"
    How to rectify it.
    please give suggestions urgent.

    Hi,
    you use sum, but in the opposite select a "to_char('0')", so the first one will be number, the second a char. Use just a "0" for the opposite of the sum:
    SELECT distinct m_product_id,m_locator_id, sum(movementqty) AS opstk, 0 as clstk, 0 as movementqty, 0 as MI, 0 as INV,0 as GR,0 as GS,0 as MO
    from m_transaction
    where m_product_id='1136094' and m_locator_id='1000021'
    and movementdate <'22-oct-2010'
    GROUP BY m_product_id,m_locator_id
    union
    SELECT distinct m_product_id,m_locator_id, 0 as opstk,sum(movementqty) AS clstk, 0 as movementqty, 0 as MI, 0 as INV, 0 as GR,0 as GS,0 as MO
    from m_transaction
    where m_product_id='1136094' and m_locator_id='1000021'
    and movementdate <'22-oct-2010'
    GROUP BY m_product_id,m_locator_idHerald ten Dam
    http://htendam.wordpress.com

  • SQL - JOIN using UNION ?? UNION using JOIN ?? with example!

    I was asked this question during one of my interviews. Can you do JOIN using UNION keyword? Can you do UNION using JOIN keyword?
    That is -
    1. I should get same output as JOIN without using JOIN keyword, but using UNION Keyword?
    2. I should get same output as UNION without using UNION keyword, but using JOIN Keyword?
    Can you give me an example of how to do this if possible?

    Hi,
    Welcome to the forum!
    user13067794 wrote:
    I was asked this question during one of my interviews. Can you do JOIN using UNION keyword? Can you do UNION using JOIN keyword?The correct answer to those questions is: Why would you want to? All versions of Oracle (and probably any other database product) provide JOIN to do certain things and UNION to do other things. Why not use those features the way they were designed to be used? Even if it is possible to do what you ask, it's going to be more complicated and less efficient.
    If you really must:
    That is -
    1. I should get same output as JOIN without using JOIN keyword, but using UNION Keyword? You can select the relevant columns from each table, and NULLs for all the columns from other tables, in a UNION query. Then you can use GROUP BY or analytic functions to combine data from different rows. For example, this JOIN:
    SELECT     d.dname
    ,     e.mgr
    FROM     scott.dept     d
    JOIN     scott.emp     e  ON     d.deptno  = e.deptno
    ;could be written using UNION, but no JOIN, like this:
    WITH     union_data     AS
         SELECT     deptno
         ,     dname
         ,     NULL     AS empno
         ,     NULL     AS mgr
         FROM     scott.dept
        UNION ALL
         SELECT     deptno
         ,     NULL     AS dname
         ,     empno
         ,     mgr
         FROM     scott.emp
    ,     quasi_join     AS
         SELECT     MAX (dname) OVER (PARTITION BY deptno)     AS dname
         ,     mgr
         ,     empno
         FROM     union_data
    SELECT     dname
    ,     mgr
    FROM     quasi_join
    WHERE     empno     IS NOT NULL
    ;Depending on your tables and your requirements, you might be able to do something a little simpler.
    2. I should get same output as UNION without using UNION keyword, but using JOIN Keyword?A FULL OUTER JOIN is similar to UNION.
    This UNION query:
    SELECT     dname          AS txt
    FROM     scott.dept
    UNION
    SELECT     TO_CHAR (mgr)     AS txt
    FROM     scott.emp
    ;Can be written like this, using JOIN but no UNION:
    SELECT DISTINCT
         NVL2 ( e.empno
              , TO_CHAR (e.mgr)
              , d.dname
              )          AS txt
    FROM          scott.dept     d
    FULL OUTER JOIN     scott.emp     e  ON       1 = 2
    user13067794 wrote:I too don't any example as such, but I am thinking on this line -
    Select a.x, b.y
    from a,b
    where a.key=b.key and sal<5000
    UNION
    Select a.x, b.y
    From a,b
    Where a.key=b.key and sal>7000
    can we get same result using JOIN?That's a very special case. You can get the same results without using UNION like this:
    Select distinct
         a.x
    ,      b.y
    from      a
    ,     b
    where      a.key     = b.key
    and      (        sal < 5000
         OR     sal > 7000
    Can we do something similar using UNION without using JOIN keyword??What you posted does not use the JOIN keyword.
    To get the same results without using a join (either with or without the JOIN keyword), you can use UNION together with aggregate or analytic functions, as I showed earlier.
    Edited by: Frank Kulash on Jul 5, 2011 9:01 PM

  • UNION keyword in Qaaws Filter Object

    Hi All,
    Could you please help me in the following problem?
    I need to combine two queries in Qaaws using the keyword UNION, How can I get UNION keyword in Filter Objects e in Qaaws? In Qaaws Filter Objects,  I am able to see only  2 options (And,  Or)
    In the Universe under SQL parameters, Allow use of Union, Intersect and minus option is already checked.
    In Universe,  using 'Query Panel'  I am able to generate this query using 'Combine Queries' option.
    However I am unable to find such option in Qaaws. Can any one help me please in this regard?
    My query is :
    SELECT DISTINCT
      SUM(SHP_DW_CARRIER_SPEND_TB.NET_CHARGES),
    SHP_DW_CARRIER_SPEND_TB.CARRIER_ID
    FROM
      SHP_DW_CARRIER_SPEND_TB
    WHERE
      SHP_DW_CARRIER_SPEND_TB.CUSTOMER_ID  =  7920
      AND  ( SHP_DW_CARRIER_SPEND_TB.CARRIER_ID in (21,22,23,85135,85192,85137,85136)  )
    GROUP BY
      SHP_DW_CARRIER_SPEND_TB.CARRIER_ID
    UNION
    SELECT DISTINCT
      SUM(SHP_DW_CARRIER_SPEND_TB.NET_CHARGES), -2
    FROM
      SHP_DW_CARRIER_SPEND_TB
    WHERE
      SHP_DW_CARRIER_SPEND_TB.CUSTOMER_ID  =  7920
       AND  ( SHP_DW_CARRIER_SPEND_TB.CARRIER_ID NOT in (21,22,23,85135,85192,85137,85136)  )
    Thanks in advance.
    Regards,
    Kishore Babu M. N.

    Hi User,
    Filter view will not work with union request.
    You have a workaround here, http://obiee101.blogspot.com/2008/04/obiee-combined-with-similar-request-and.html
    Rgds,
    Dpka

  • With a UNION query my drill downs disappear

    Hi all,
    When a make a query in the query generator of SBO, all my drill downs or arrows vanish when I make a union of two or more queries. When I keep the queries separate, the drill downs are there. Has anybody experienced the same phenomenon? Has this been solved in a patch that I am unaware of or does it not happen in 2005 SP01? I am currently on 2005 SP00 patch 9.
    Thanks in advance for the feedback.
    André

    Hi Alexey,
    That certainly works. I have pasted my query below, and as you can see, its a little more complex than that. Would you be able to tell me where I can start on this?
    Thanks,
    André
    SELECT DISTINCT
                          Serial.SuppSerial AS , GSHeader.DocNum AS PurchDocNum, GSHeader.DocDate AS PurchDocDate,
                          SerialInMove.CardCode AS SupplierCode, SerialInMove.CardName AS SupplierName, GoodsReceipt.ItemCode AS PurchItemCode,
                          GoodsReceipt.Dscription AS PurchDscription, GoodsReceipt.PriceBefDi AS PurchPriceBefDi, GoodsReceipt.DiscPrcnt AS PurchDiscPrcnt,
                          GoodsReceipt.Price AS PurchPrice, InvHeader.DocNum AS InvoiceDocNum, InvHeader.DocDate AS InvDocDate,
                          SerialOutMove.CardCode AS CusomerCode, SerialOutMove.CardName AS CustomerName, Invoice.ItemCode AS InvItemCode,
                          Invoice.Dscription AS InvDscription, Invoice.PriceBefDi AS InvPriceBefDi, Invoice.DiscPrcnt AS InvDiscPrcnt, Invoice.Price AS InvPrice,
                          ROUND(Invoice.Price / GoodsReceipt.Price, 2) AS ProfitMargin, SalesEmp.SlpName AS VTW
    FROM         dbo.OSRI Serial INNER JOIN
                          dbo.SRI1 SerialInMove ON Serial.ItemCode = SerialInMove.ItemCode AND Serial.SysSerial = SerialInMove.SysSerial INNER JOIN
                          dbo.SRI1 SerialOutMove ON Serial.ItemCode = SerialOutMove.ItemCode AND Serial.SysSerial = SerialOutMove.SysSerial INNER JOIN
                          dbo.DLN1 Delivery ON SerialOutMove.BaseEntry = Delivery.DocEntry AND SerialOutMove.ItemCode = Delivery.ItemCode AND
                          SerialOutMove.BaseLinNum = Delivery.LineNum INNER JOIN
                          dbo.IGN1 GoodsReceipt ON SerialInMove.BaseEntry = GoodsReceipt.DocEntry AND SerialInMove.ItemCode = GoodsReceipt.ItemCode AND
                          SerialInMove.BaseLinNum = GoodsReceipt.LineNum INNER JOIN
                          dbo.OIGN GSHeader ON GoodsReceipt.DocEntry = GSHeader.DocEntry LEFT OUTER JOIN
                          dbo.OINV InvHeader INNER JOIN
                          dbo.INV1 Invoice ON InvHeader.DocEntry = Invoice.DocEntry ON Delivery.ItemCode = Invoice.ItemCode AND
                          Delivery.DocEntry = Invoice.BaseEntry FULL OUTER JOIN
                          dbo.OSLP SalesEmp ON Invoice.SlpCode = SalesEmp.SlpCode
    WHERE     (SerialInMove.Direction = '0') AND (SerialOutMove.Direction = '1') AND (SerialInMove.BaseType = 59) AND (SerialOutMove.BaseType = 15) AND
                          (NOT (GoodsReceipt.ItemCode LIKE N'MET%')) AND (InvHeader.DocDate BETWEEN CONVERT(DATETIME, '2006-05-01 00:00:00', 102) AND
                          CONVERT(DATETIME, '2006-08-30 00:00:00', 102))
    UNION
    SELECT DISTINCT
                          SERIAL.SuppSerial AS , GRHeader.DocNum AS PurchDocNum, GRHeader.DocDate AS PurchDocDate,
                          SerialInMove.CardCode AS SupplierCode, SerialInMove.CardName AS SupplierName, GoodsReceipt.ItemCode AS PurchItemCode,
                          GoodsReceipt.Dscription AS PurchDscription, GoodsReceipt.PriceBefDi AS PurchPriceBefDi, GoodsReceipt.DiscPrcnt AS PurchDiscPrcnt,
                          GoodsReceipt.Price AS PurchPrice, InvHeader.DocNum AS InvoiceDocNum, InvHeader.DocDate AS InvDocDate,
                          SerialOutMove.CardCode AS CusomerCode, SerialOutMove.CardName AS CustomerName, Invoice.ItemCode AS InvItemCode,
                          Invoice.Dscription AS InvDscription, Invoice.PriceBefDi AS InvPriceBefDi, Invoice.DiscPrcnt AS InvDiscPrcnt, Invoice.Price AS InvPrice,
                          ROUND(Invoice.Price / GoodsReceipt.Price, 2) AS ProfitMargin, SalesEmp.SlpName AS VTW
    FROM         dbo.OSRI SERIAL INNER JOIN
                          dbo.SRI1 SerialInMove ON SERIAL.ItemCode = SerialInMove.ItemCode AND SERIAL.SysSerial = SerialInMove.SysSerial INNER JOIN
                          dbo.SRI1 SerialOutMove ON SERIAL.ItemCode = SerialOutMove.ItemCode AND SERIAL.SysSerial = SerialOutMove.SysSerial INNER JOIN
                          dbo.IGN1 GoodsReceipt ON SerialInMove.BaseEntry = GoodsReceipt.DocEntry AND SerialInMove.ItemCode = GoodsReceipt.ItemCode AND
                          SerialInMove.BaseLinNum = GoodsReceipt.LineNum INNER JOIN
                          dbo.OIGN GRHeader ON GoodsReceipt.DocEntry = GRHeader.DocEntry FULL OUTER JOIN
                          dbo.OSLP SalesEmp INNER JOIN
                          dbo.OINV InvHeader INNER JOIN
                          dbo.INV1 Invoice ON InvHeader.DocEntry = Invoice.DocEntry ON SalesEmp.SlpCode = Invoice.SlpCode ON
                          SerialOutMove.BaseEntry = Invoice.DocEntry AND SerialOutMove.ItemCode = Invoice.ItemCode AND
                          SerialOutMove.BaseLinNum = Invoice.LineNum
    WHERE     (SerialInMove.Direction = '0') AND (SerialOutMove.Direction = '1') AND (SerialInMove.BaseType = 59) AND (SerialOutMove.BaseType = 13) AND
                          (NOT (GoodsReceipt.ItemCode LIKE N'MET%')) AND (InvHeader.DocDate BETWEEN CONVERT(DATETIME, '2006-05-01 00:00:00', 102) AND
                          CONVERT(DATETIME, '2006-08-30 00:00:00', 102))
    UNION
    SELECT DISTINCT
                          Serial.SuppSerial AS , GRPOHeader.DocNum AS PurchDocNum, GRPOHeader.DocDate AS PurchDocDate,
                          SerialInMove.CardCode AS SupplierCode, SerialInMove.CardName AS SupplierName, GoodsReceiptPO.ItemCode AS PurchItemCode,
                          GoodsReceiptPO.Dscription AS PurchDscription, GoodsReceiptPO.PriceBefDi AS PurchPriceBefDi, GoodsReceiptPO.DiscPrcnt AS PurchDiscPrcnt,
                          GoodsReceiptPO.Price AS PurchPrice, InvHeader.DocNum AS InvoiceDocNum, InvHeader.DocDate AS InvDocDate,
                          SerialOutMove.CardCode AS CusomerCode, SerialOutMove.CardName AS CustomerName, Invoice.ItemCode AS InvItemCode,
                          Invoice.Dscription AS InvDscription, Invoice.PriceBefDi AS InvPriceBefDi, Invoice.DiscPrcnt AS InvDiscPrcnt, Invoice.Price AS InvPrice,
                          ROUND(Invoice.Price / GoodsReceiptPO.Price, 2) AS ProfitMargin, SalesEmp.SlpName AS VTW
    FROM         dbo.OSRI Serial INNER JOIN
                          dbo.SRI1 SerialInMove ON Serial.ItemCode = SerialInMove.ItemCode AND Serial.SysSerial = SerialInMove.SysSerial INNER JOIN
                          dbo.SRI1 SerialOutMove ON Serial.ItemCode = SerialOutMove.ItemCode AND Serial.SysSerial = SerialOutMove.SysSerial INNER JOIN
                          dbo.DLN1 Delivery ON SerialOutMove.BaseEntry = Delivery.DocEntry AND SerialOutMove.ItemCode = Delivery.ItemCode AND
                          SerialOutMove.BaseLinNum = Delivery.LineNum INNER JOIN
                          dbo.PDN1 GoodsReceiptPO ON SerialInMove.ItemCode = GoodsReceiptPO.ItemCode AND SerialInMove.BaseEntry = GoodsReceiptPO.DocEntry AND
                          SerialInMove.BaseLinNum = GoodsReceiptPO.LineNum INNER JOIN
                          dbo.OPDN GRPOHeader ON GoodsReceiptPO.DocEntry = GRPOHeader.DocEntry LEFT OUTER JOIN
                          dbo.OINV InvHeader INNER JOIN
                          dbo.INV1 Invoice ON InvHeader.DocEntry = Invoice.DocEntry ON Delivery.ItemCode = Invoice.ItemCode AND
                          Delivery.DocEntry = Invoice.BaseEntry FULL OUTER JOIN
                          dbo.OSLP SalesEmp ON Invoice.SlpCode = SalesEmp.SlpCode
    WHERE     (SerialInMove.Direction = '0') AND (SerialOutMove.Direction = '1') AND (SerialInMove.BaseType = 20) AND (SerialOutMove.BaseType = 15) AND
                          (InvHeader.DocDate BETWEEN CONVERT(DATETIME, '2006-05-01 00:00:00', 102) AND CONVERT(DATETIME, '2006-08-30 00:00:00', 102)) AND
                          (NOT (GoodsReceiptPO.ItemCode LIKE N'MET%'))
    UNION
    SELECT DISTINCT
                          Serial.SuppSerial AS , GRPOHeader.DocNum AS PurchDocNum, GRPOHeader.DocDate AS PurchDocDate,
                          SerialInMove.CardCode AS SupplierCode, SerialInMove.CardName AS SupplierName, GoodsReceiptPO.ItemCode AS PurchItemCode,
                          GoodsReceiptPO.Dscription AS PurchDscription, GoodsReceiptPO.PriceBefDi AS PurchPriceBefDi, GoodsReceiptPO.DiscPrcnt AS PurchDiscPrcnt,
                          GoodsReceiptPO.Price AS PurchPrice, InvHeader.DocNum AS InvoiceDocNum, InvHeader.DocDate AS InvDocDate,
                          SerialOutMove.CardCode AS CusomerCode, SerialOutMove.CardName AS CustomerName, Invoice.ItemCode AS InvItemCode,
                          Invoice.Dscription AS InvDscription, Invoice.PriceBefDi AS InvPriceBefDi, Invoice.DiscPrcnt AS InvDiscPrcnt, Invoice.Price AS InvPrice,
                          ROUND(Invoice.Price / GoodsReceiptPO.Price, 2) AS ProfitMargin, SalesEmp.SlpName AS VTW
    FROM         dbo.OSRI Serial INNER JOIN
                          dbo.SRI1 SerialInMove ON Serial.ItemCode = SerialInMove.ItemCode AND Serial.SysSerial = SerialInMove.SysSerial INNER JOIN
                          dbo.SRI1 SerialOutMove ON Serial.ItemCode = SerialOutMove.ItemCode AND Serial.SysSerial = SerialOutMove.SysSerial INNER JOIN
                          dbo.PDN1 GoodsReceiptPO ON SerialInMove.BaseEntry = GoodsReceiptPO.DocEntry AND SerialInMove.BaseLinNum = GoodsReceiptPO.LineNum AND
                           SerialInMove.ItemCode = GoodsReceiptPO.ItemCode INNER JOIN
                          dbo.OPDN GRPOHeader ON GoodsReceiptPO.DocEntry = GRPOHeader.DocEntry FULL OUTER JOIN
                          dbo.OSLP SalesEmp INNER JOIN
                          dbo.OINV InvHeader INNER JOIN
                          dbo.INV1 Invoice ON InvHeader.DocEntry = Invoice.DocEntry ON SalesEmp.SlpCode = Invoice.SlpCode ON
                          SerialOutMove.BaseEntry = Invoice.DocEntry AND SerialOutMove.ItemCode = Invoice.ItemCode AND
                          SerialOutMove.BaseLinNum = Invoice.LineNum
    WHERE     (SerialInMove.Direction = '0') AND (SerialOutMove.Direction = '1') AND (SerialInMove.BaseType = 20) AND (SerialOutMove.BaseType = 13) AND
                          (NOT (GoodsReceiptPO.ItemCode LIKE N'MET%')) AND (InvHeader.DocDate BETWEEN CONVERT(DATETIME, '2006-05-01 00:00:00', 102) AND
                          CONVERT(DATETIME, '2006-08-30 00:00:00', 102))
    ORDER BY InvHeader.DocDate

  • Union on store procedure

    Is it possible to have union on a store procedure and an sql within an application like
    create or replace
    PACKAGE BODY MyStoredProc AS
    PROCEDURE DeparmentName (ColumnA IN VARCHAR2,
    wPO OUT curUsr) IS
    BEGIN
    OPEN wPO FOR
    exec MyStoredProcedure 1
    union
    SELECT distinct departmentName
    FROM "schemaNAME".Department d
    WHERE d.ColumnA='5000'
    END;

    Hi Suzie,
    please take a look at [url https://forums.oracle.com/forums/thread.jspa?threadID=886365&tstart=0]Understanding Ref Cursors.
    The entry "1. What is a ref cursor and how is the data fetched" shows some examples of ref-cursors. How they may work or may fail.
    (pointer to data not* container of data, ...)
    The next entry "2. How can we use a ref cursor in a SQL query?"
    describes how they can be used in SQL.
    The last entry "3. What is
    the point of ref cursors?" helps when to use or not to use ref-cursors.
    Bye
    stratmo
    Added:*
    Sadly I don't know java.
    If you are looking for a "parametrized view" then ...
    create or replace
    Package Pipeline_Func_Replace
    As
       Gcc_Context       Varchar2(32767) := NULL;
       Function    Get_Context Return Varchar2 ;
       procedure   Set_Context(Pic_Context In Varchar2);
       function    Set_Context(Pic_Context In Varchar2) return integer;
    End Pipeline_Func_Replace;
    create or replace
    Package Body Pipeline_Func_Replace
    as
       function get_context
       return varchar2
       as
       begin
          return gcc_context;
       end get_context;
       procedure set_context(pic_context in varchar2)
       as
       begin
          gcc_context := pic_context;
       end set_context;
       function set_context(pic_context in varchar2)
       return integer
       as
       begin
          set_context(pic_context);
          return 1;
       end set_context;
    end pipeline_func_replace;
    create or replace view restricted_emps
    as
    select a.*
    from hr.employees a
    where a.manager_id = Pipeline_Func_Replace.get_context; -- the parameter
    -- one can set the context with
    select Pipeline_Func_Replace.set_context('101') from dual;
    -- or with
    begin
       Pipeline_Func_Replace.set_context(pic_context => 101);
    end;
    /... is a possibility.
    Edited by: stratmo on Dec 20, 2012 11:58 PM

  • DISTINCT and EXISTS

    I have come across a article which states exists is much faster in execution than distinct .
    Union all works faster than union .
    Can anyone explain the reasons?
    Responses are appreciated!

    Here is one, selected somewhat at random from a Google search on "oracle union versus union all:"
    UNION ALL is preferable to UNION because UNION returns distinct rows selected by both queries while UNION ALL returns all the rows. Therefore, if the table has duplicates, UNION will remove them, which is not what we want. If the table has no duplicates, UNION will force a sort and cause performance degradation as compared to UNION ALL.
    http://www.quest-pipelines.com/pipelines/plsql/tips01.htm
    The other:
    http://www.oracle-base.com/articles/misc/EfficientSQLStatements.php#Exists
    It is harder to be any clearer than that.

  • Replacing NULL and EmptyString('') with "Unknown" in SSRS parameter dropdown

    All,
    What I want to do is, in the SSRS parameter drop down, instead of showing NULL and Blank values(), i want to categorize them as "Unknown", so, if the user selects "Unknown" from the SSRS drop down parameter, he should be able to see all
    the records that have NULL values or empty strings in that particular column in the result set.
    Can you tell me, how should I handle it in my main stored proc as well as in the dataset?
    Right now, i have something like this:
    Where
    (t1.name in (select value from dbo.Split(@TName,',')) OR @TName IN ('All'))
    -- Where t1.Name has empty strings and NULL values. Both of these values should be categorized under "Unknown"
    -- How would the dataset query look like? Right now I have this query for populating the drop down for that parameter:
    Select All
    UNION
    Select Distinct Name
    Order BY 1

    Hello,
    Please refer to the following stored procedure:
    SELECT
    CASE WHEN TName IS NULL OR TName = ''
    THEN 'Unknown' ELSE TName END AS TName
    From DemoTable
    Then, use following query code to get the parameter values:
    SELECT Distinct
    CASE WHEN TName IS NULL OR TName = '' THEN 'Unknown'
    ELSE TName END AS TName
    FROM DemoTable
    Regards,
    Alisa Tang
    Alisa Tang
    TechNet Community Support

  • Help me query tunning

    Hi Guru's
    Can you please help me query tunning.
    Database Version : Oracle 11g - 11.2.0.3
    select  distinct  corporation_name custer_name,
            glog_util.remove_domain(SHIP_BUY.SERVPROV_GID ) SCAC,
            glog_util.remove_domain(ship_buy.shipment_gid) buy_shipment_gid,
            F_Get_SELL_ID_STRING(SHIP_BUY.SHIPMENT_GID) sell_shipment_gid,
            ship_buy.domain_name,    
            F_GET_ORDER_RELEASE_GID('B',SHIP_BUY.SHIPMENT_GID,0) ORDER_RELEASE_GID,
            f_get_refnum_string('SHIPMENT', ship_buy.shipment_gid, 'MBOL_NUMBER_CLEANSED')MBOL_NUMBER,
            F_GET_POD_RECEIVED_DATE (ship_BUY.SHIPMENT_GID) POD_RECEIVED_DATE,
            f_get_exp_accrue_amt(ship_buy.shipment_gid,'SHIPMENT') Total_accrual_amount    
    from shipment ship_buy,
            invoice inv,
            invoice_shipment si,
            --voucher v,
            corporation corp
    where corp.domain_name=ship_buy.domain_name
            and corp.is_domain_master='Y'
          and 1=1
          AND ship_buy.domain_name like 'UPS/CP/DFP/%'
          and F_GET_POD_RECEIVED_DATE (ship_BUY.SHIPMENT_GID)  <= to_char(to_date('31-JUL-2013', 'DD-MON-YYYY'), 'dd-mon-yyyy')
            --and V.INVOICE_GID(+) = inv.invoice_gid
            --and ship_buy.domain_name = 'UPS/CP/VZNB'
            and si.shipment_gid(+) = SHIP_BUY.SHIPMENT_GID
            AND SI.INVOICE_GID = INV.INVOICE_GID(+)
            and SHIP_BUY.INSERT_DATE > '1-JAN-2007'
            and SHIP_BUY.USER_DEFINED1_ICON_GID = 'ACCEPTED'
    UNION        
    select  distinct  corporation_name custer_name,
            glog_util.remove_domain(SHIP_BUY.SERVPROV_GID ) SCAC,
            glog_util.remove_domain(ship_buy.shipment_gid) buy_shipment_gid,
            F_GET_SELL_ID_STRING( SHIP_BUY.SHIPMENT_GID) sell_shipment_gid,
            ship_buy.domain_name,    
            F_GET_ORDER_RELEASE_GID('B',SHIP_BUY.SHIPMENT_GID,0) ORDER_RELEASE_GID,
            f_get_refnum_string('SHIPMENT', ship_buy.shipment_gid, 'MBOL_NUMBER_CLEANSED')MBOL_NUMBER,
            F_GET_POD_RECEIVED_DATE (ship_BUY.SHIPMENT_GID) POD_RECEIVED_DATE,
            f_get_exp_accrue_amt(inv.invoice_gid,'INVOICE') Total_accrual_amount  
    from shipment ship_buy,
            invoice inv,
            invoice_shipment si,
          -- voucher v,
            corporation corp
    where corp.domain_name=ship_buy.domain_name
            and corp.is_domain_master='Y'
            and 1=1
            AND ship_buy.domain_name like 'UPS/CP/DFP/%'
            and F_GET_POD_RECEIVED_DATE (ship_BUY.SHIPMENT_GID)  <= to_char(to_date('31-JUL-2013', 'DD-MON-YYYY'), 'dd-mon-yyyy')
          --AND INV.DOMAIN_NAME = 'UPS/CP/VZNB'
          --and V.INVOICE_GID(+) = inv.invoice_gid
            and si.shipment_gid(+) = SHIP_BUY.SHIPMENT_GID
            AND SI.INVOICE_GID = INV.INVOICE_GID(+)
            and SHIP_BUY.INSERT_DATE > '1-JAN-2007'
            and INV.USER_DEFINED1_ICON_GID = 'ACCEPTED'
    GROUP BY corporation_name,SHIP_BUY.SHIPMENT_GID,SHIP_BUY.SERVPROV_GID,ship_buy.domain_name,inv.invoice_gid
    ORDER BY CUSTER_NAME, BUY_SHIPMENT_GID;
    And I generated the execution plan :
    | Id  | Operation                          | Name                    | Rows  | Bytes | Cost (%CPU)| Time    |
    |  0 | SELECT STATEMENT                  |                        |    3 |  448 |  415  (2)| 00:00:05 |
    |  1 |  SORT UNIQUE                      |                        |    3 |  448 |  414  (87)| 00:00:05 |
    |  2 |  UNION-ALL                        |                        |      |      |            |          |
    |  3 |    NESTED LOOPS OUTER              |                        |    3 |  384 |    57  (0)| 00:00:01 |
    |*  4 |    HASH JOIN                      |                        |    3 |  294 |    54  (0)| 00:00:01 |
    |*  5 |      TABLE ACCESS BY INDEX ROWID  | SHIPMENT                |    3 |  195 |    40  (0)| 00:00:01 |
    |*  6 |      INDEX SKIP SCAN              | IND_SHIP_DOM_ICON      |    54 |      |    25  (0)| 00:00:01 |
    |*  7 |      TABLE ACCESS FULL            | CORPORATION            |    4 |  132 |    14  (0)| 00:00:01 |
    |*  8 |    INDEX RANGE SCAN              | IND_INVOICESHIP_SHP_GID |    1 |    30 |    1  (0)| 00:00:01 |
    |  9 |    HASH GROUP BY                  |                        |    1 |  192 |  356  (1)| 00:00:05 |
    |* 10 |    HASH JOIN                      |                        |    1 |  192 |  354  (1)| 00:00:05 |
    |  11 |      NESTED LOOPS                  |                        |      |      |            |          |
    |  12 |      NESTED LOOPS                |                        |    1 |  159 |  339  (0)| 00:00:05 |
    |  13 |        NESTED LOOPS                |                        |  145 | 13920 |  194  (0)| 00:00:03 |
    |  14 |        TABLE ACCESS BY INDEX ROWID| INVOICE                |  145 |  5220 |    49  (0)| 00:00:01 |
    |* 15 |          INDEX SKIP SCAN          | IDX_INV_TYP_ICON_NAM    |  145 |      |    17  (0)| 00:00:01 |
    |* 16 |        INDEX RANGE SCAN          | UK_INVOICE_SHIPMENT    |    1 |    60 |    1  (0)| 00:00:01 |
    |* 17 |        INDEX UNIQUE SCAN          | PK_SHIPMENT            |    1 |      |    1  (0)| 00:00:01 |
    |* 18 |      TABLE ACCESS BY INDEX ROWID  | SHIPMENT                |    1 |    63 |    1  (0)| 00:00:01 |
    |* 19 |      TABLE ACCESS FULL            | CORPORATION            |    4 |  132 |    14  (0)| 00:00:01 |
    Predicate Information (identified by operation id):
      4 - access("CORP"."DOMAIN_NAME"="SHIP_BUY"."DOMAIN_NAME")
      5 - filter("F_GET_POD_RECEIVED_DATE"("SHIP_BUY"."SHIPMENT_GID")<=TO_DATE(' 2013-07-31 00:00:00',
                  'syyyy-mm-dd hh24:mi:ss') AND "SHIP_BUY"."INSERT_DATE">TO_DATE(' 2007-01-01 00:00:00', 'syyyy-mm-dd
                  hh24:mi:ss'))
      6 - access("SHIP_BUY"."USER_DEFINED1_ICON_GID"='ACCEPTED' AND "SHIP_BUY"."DOMAIN_NAME" LIKE
                  'UPS/CP/DFP/%')
          filter("SHIP_BUY"."DOMAIN_NAME" LIKE 'UPS/CP/DFP/%' AND
                  "SHIP_BUY"."USER_DEFINED1_ICON_GID"='ACCEPTED')
      7 - filter("CORP"."IS_DOMAIN_MASTER"='Y' AND "CORP"."DOMAIN_NAME" LIKE 'UPS/CP/DFP/%')
      8 - access("SI"."SHIPMENT_GID"(+)="SHIP_BUY"."SHIPMENT_GID")
      10 - access("CORP"."DOMAIN_NAME"="SHIP_BUY"."DOMAIN_NAME")
      15 - access("INV"."USER_DEFINED1_ICON_GID"='ACCEPTED')
          filter("INV"."USER_DEFINED1_ICON_GID"='ACCEPTED')
      16 - access("SI"."INVOICE_GID"="INV"."INVOICE_GID")
      17 - access("SI"."SHIPMENT_GID"="SHIP_BUY"."SHIPMENT_GID")
          filter("F_GET_POD_RECEIVED_DATE"("SHIP_BUY"."SHIPMENT_GID")<=TO_DATE(' 2013-07-31 00:00:00',
                  'syyyy-mm-dd hh24:mi:ss'))
      18 - filter("SHIP_BUY"."DOMAIN_NAME" LIKE 'UPS/CP/DFP/%' AND "SHIP_BUY"."INSERT_DATE">TO_DATE('
                  2007-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
      19 - filter("CORP"."IS_DOMAIN_MASTER"='Y' AND "CORP"."DOMAIN_NAME" LIKE 'UPS/CP/DFP/%')
    Statistics
        246247  recursive calls
              2  db block gets
        1660067  consistent gets
          13839  physical reads
              0  redo size
        592054  bytes sent via SQL*Net to client
          6024  bytes received via SQL*Net from client
            502  SQL*Net roundtrips to/from client
          15296  sorts (memory)
              0  sorts (disk)
          7513  rows processed

    Hmmm...why does this look familiar?
    F_GET_POD_RECEIVED_DATE (ship_BUY.SHIPMENT_GID)  <= to_char(to_date('31-JUL-2013', 'DD-MON-YYYY'), 'dd-mon-yyyy')
    SHIP_BUY.INSERT_DATE > '1-JAN-2007'
    Like I said in your other thread about this, these two lines need to be fixed and your function needs to be fixed so the return statement doesn't do an implicit date conversion.
    Can't you see what that first line is doing?  You're taking a character string, turning it into a date, then back to a character string.
    If nothing else, these lines should be...
    F_GET_POD_RECEIVED_DATE (ship_BUY.SHIPMENT_GID)  <= to_date('31-JUL-2013', 'DD-MON-YYYY')
    SHIP_BUY.INSERT_DATE > to_date('01-JAN-2007','DD-MON-YYYY')
    (assuming insert_date is a proper date format, fingers crossed)

  • Oracle error 01841  Help please!

    I am using 10g on Unix. I am converting queries to Oracle and am down to a few errors that I cannot resolve on my own. I have this query
    SELECT DISTINCT TI.TICKET_ID,
    (QUEUE_DATE-(TO_CHAR(TRUNC(SYSDATE),'YYYYMMDD'))) AS DAYS_OPEN,
    TI.QUEUE_DATE,
    L1.LONG_DESC AS ITEM_STATUS,
    R.REQUEST_DESC,
    H.HELP_DESK_NAME,
    U.NAME_LAST || ', ' || U.NAME_FIRST AS ASSIGNED_TECH
    FROM WEBDESK.WD_TICKET T
    INNER JOIN WEBDESK.WD_TICKET_ITEM TI ON ((TI.TICKET_ID = T.TICKET_ID) AND (TI.ROUTE_GROUP_ID = 'G5097'))
    LEFT OUTER JOIN WEBDESK.WD_GROUP G ON (TI.ROUTE_GROUP_ID = GROUP_ID)
    LEFT OUTER JOIN WEBDESK.WD_USER U ON (U.USER_ID = TI.TECH_USER_ID)
    LEFT OUTER JOIN WEBDESK.WD_REQUEST R ON (T.REQUEST_ID = R.REQUEST_ID)
    LEFT OUTER JOIN WEBDESK.WD_LOOK_UP L ON (T.REQUEST_CATEGORY_NO = L.LOOKUP_ID)
    LEFT OUTER JOIN WEBDESK.WD_LOOK_UP L1 ON (TI.STATUS_NO = L1.LOOKUP_ID)
    LEFT OUTER JOIN WEBDESK.WD_HELP_DESK H ON (T.HELP_DESK_ID = H.HELP_DESK_ID)
    LEFT OUTER JOIN
    (SELECT IR.TICKET_ID,IR.TICKET_ITEM_ID
    FROM WEBDESK.WD_TICKET_IROUTE IR
    INNER JOIN WEBDESK.WD_TICKET_ITEM TIM ON ((IR.TICKET_ID = TIM.TICKET_ID)
    AND (IR.ITEM_ID_DEPENDENCY = TIM.TICKET_ITEM_ID)
    AND (TIM.END_DATE = '31-DEC-9999'))) LIR ON ((TI.TICKET_ID = LIR.TICKET_ID)
    AND (TI.TICKET_ITEM_ID = LIR.TICKET_ITEM_ID))
    WHERE ((LIR.TICKET_ID IS NULL)
    AND (TI.TECH_USER_ID IS NULL))
    --AND (TI.END_DATE = '31-DEC-9999')
    UNION
    SELECT DISTINCT TI.TICKET_ID,
    (TI.QUEUE_DATE-(TO_CHAR(TRUNC(SYSDATE),'YYYYMMDD'))) AS DAYS_OPEN,
    TI.QUEUE_DATE,
    L1.LONG_DESC AS ITEM_STATUS,
    R.REQUEST_DESC,
    H.HELP_DESK_NAME,
    U.NAME_LAST || ', ' || U.NAME_FIRST AS ASSIGNED_TECH
    FROM WEBDESK.WD_TICKET T
    INNER JOIN WEBDESK.WD_TICKET_ITEM TI ON (TI.TICKET_ID = T.TICKET_ID)
    INNER JOIN WEBDESK.WD_USER_GROUP UG ON ((TI.ROUTE_GROUP_ID = UG.GROUP_ID) AND ((TI.TECH_USER_ID = UG.USER_ID)
    AND (UG.GROUP_ID = 'G5097')) AND (TI.TECH_USER_ID IS NOT NULL))
    LEFT OUTER JOIN WEBDESK.WD_GROUP G ON (TI.ROUTE_GROUP_ID = G.GROUP_ID)
    LEFT OUTER JOIN WEBDESK.WD_USER U ON (U.USER_ID = TI.TECH_USER_ID)
    LEFT OUTER JOIN WEBDESK.WD_REQUEST R ON (T.REQUEST_ID = R.REQUEST_ID)
    LEFT OUTER JOIN WEBDESK.WD_LOOK_UP L ON (T.REQUEST_CATEGORY_NO = L.LOOKUP_ID)
    LEFT OUTER JOIN WEBDESK.WD_LOOK_UP L1 ON (TI.STATUS_NO = L1.LOOKUP_ID)
    LEFT OUTER JOIN WEBDESK.WD_HELP_DESK H ON (T.HELP_DESK_ID = H.HELP_DESK_ID)
    --WHERE (TI.END_DATE = '31-DEC-9999')
    ORDER BY 2 DESC,3,4
    and when I run it I am getting
    ORA-01841: (full) year must be between -4713 and +9999, and not be 0
    01841. 00000 - "(full) year must be between -4713 and +9999, and not be 0"
    *Cause:    Illegal year entered
    *Action:   Input year in the specified range
    As far as I can tell the date IS in the proper range, but obviously I am missing something. Any help is greatly appreciated.

    AND ( tim.end_date = '31-DEC-9999' ) ))With Oracle characters between single quote marks are STRINGS!
    'This is a string, 2009-12-31, not a date'
    When a DATE datatype is desired, then use TO_DATE() function.
    SELECT DISTINCT ti.ticket_id,
                    ( queue_date - ( To_char(Trunc(SYSDATE), 'YYYYMMDD') ) ) AS
                    days_open,
                    ti.queue_date,
                    l1.long_desc                                             AS
                    item_status,
                    r.request_desc,
                    h.help_desk_name,
                    u.name_last
                    || ', '
                    || u.name_first                                          AS
                    assigned_tech
    FROM   webdesk.wd_ticket t
           inner join webdesk.wd_ticket_item ti
             ON ( ( ti.ticket_id = t.ticket_id )
                  AND ( ti.route_group_id = 'G5097' ) )
           left outer join webdesk.wd_group g
             ON ( ti.route_group_id = group_id )
           left outer join webdesk.wd_user u
             ON ( u.user_id = ti.tech_user_id )
           left outer join webdesk.wd_request r
             ON ( t.request_id = r.request_id )
           left outer join webdesk.wd_look_up l
             ON ( t.request_category_no = l.lookup_id )
           left outer join webdesk.wd_look_up l1
             ON ( ti.status_no = l1.lookup_id )
           left outer join webdesk.wd_help_desk h
             ON ( t.help_desk_id = h.help_desk_id )
           left outer join (SELECT ir.ticket_id,
                                   ir.ticket_item_id
                            FROM   webdesk.wd_ticket_iroute ir
                                   inner join webdesk.wd_ticket_item tim
                                     ON ( ( ir.ticket_id = tim.ticket_id )
                                          AND ( ir.item_id_dependency =
                                              tim.ticket_item_id )
                                          AND ( tim.end_date = '31-DEC-9999' ) ))
                           lir
             ON ( ( ti.ticket_id = lir.ticket_id )
                  AND ( ti.ticket_item_id = lir.ticket_item_id ) )
    WHERE  ( ( lir.ticket_id IS NULL )
             AND ( ti.tech_user_id IS NULL ) )
    --AND (TI.END_DATE = '31-DEC-9999')
    UNION
    SELECT DISTINCT ti.ticket_id,
                    ( ti.queue_date - ( To_char(Trunc(SYSDATE), 'YYYYMMDD') ) ) AS
                    days_open,
                    ti.queue_date,
                    l1.long_desc                                                AS
                    item_status,
                    r.request_desc,
                    h.help_desk_name,
                    u.name_last
                    || ', '
                    || u.name_first                                             AS
                    assigned_tech
    FROM   webdesk.wd_ticket t
           inner join webdesk.wd_ticket_item ti
             ON ( ti.ticket_id = t.ticket_id )
           inner join webdesk.wd_user_group ug
             ON ( ( ti.route_group_id = ug.group_id )
                  AND ( ( ti.tech_user_id = ug.user_id )
                        AND ( ug.group_id = 'G5097' ) )
                  AND ( ti.tech_user_id IS NOT NULL ) )
           left outer join webdesk.wd_group g
             ON ( ti.route_group_id = g.group_id )
           left outer join webdesk.wd_user u
             ON ( u.user_id = ti.tech_user_id )
           left outer join webdesk.wd_request r
             ON ( t.request_id = r.request_id )
           left outer join webdesk.wd_look_up l
             ON ( t.request_category_no = l.lookup_id )
           left outer join webdesk.wd_look_up l1
             ON ( ti.status_no = l1.lookup_id )
           left outer join webdesk.wd_help_desk h
             ON ( t.help_desk_id = h.help_desk_id )
    --WHERE (TI.END_DATE = '31-DEC-9999')
    ORDER  BY 2 DESC,
              3,
              4 

Maybe you are looking for

  • Different approach for capturing vhs to fcp

    yes i know an often asked question ... i happen to have a kona 3 card but it doesn't have any analog input capabilities. so the way i've captured vhs has been to simply to run it into my mini dv camera and record dv out from the camera through firewi

  • Why does labview forget the connections to the virtual channels in MAX?

    I create a bunch of virtual channels in MAX - some with double assignment to a single AI port. Context - a digital output controls a MUX to switch the inputs into the AI ports of my USB-6361(BNC) DAQmx; thereby allowing me to double up on the number

  • PDS change in cost data

    hi, What are the parameters to be passed into the mentioned BAPI. I have passed following parameters to change single level costs but not working. LOGICAL_SYSTEM BUSINESS_SYSTEM_GROUP SAVE_OPTIONS = 'X' PDS_HEAD PDS_NAME PDS_USAGE PLANNING_VERSION PR

  • Questions about Personalized MAIL Forms

    Hi all! I got some questions regarding the Personalized Mail Forms in CRM: 1. When you set up the subject part of the Form is it possible to "personalize it too"? What i mean is it possible to add attributes to the Subject for example the target's na

  • Error code VP07 when trying to watch BTSport on la...

    Completed registration and downloaded Silverlight. But, still not been able to watch BTSport on laptop as I get the error code VP07 being mentioned. Any ideas ?