Please Resolve My Query

I have a seperate function on a column as follows:
LT Pick Date less Wkends
CASE WHEN (DAYNAME("Agreements - Sales Order Detail"."Date Order Transaction")) ='Monday' AND (CASE WHEN "Agreements - Sales Order Detail"."Date Scheduled Pick" >= '1/1/1900' THEN 0 ELSE TIMESTAMPDIFF(SQL_TSI_DAY,"Agreements - Sales Order Detail"."Date Order Transaction","Agreements - Sales Order Detail"."Date Scheduled Pick" ) END) <=6 THEN (CASE WHEN "Agreements - Sales Order Detail"."Date Scheduled Pick" >= '1/1/1900' THEN 0 ELSE TIMESTAMPDIFF(SQL_TSI_DAY,"Agreements - Sales Order Detail"."Date Order Transaction","Agreements - Sales Order Detail"."Date Scheduled Pick" ) END)
ELSE (CASE WHEN (DAYNAME("Agreements - Sales Order Detail"."Date Order Transaction"))='Tuesday' And (CASE WHEN "Agreements - Sales Order Detail"."Date Scheduled Pick" >= '1/1/1900' THEN 0 ELSE TIMESTAMPDIFF(SQL_TSI_DAY,"Agreements - Sales Order Detail"."Date Order Transaction","Agreements - Sales Order Detail"."Date Scheduled Pick" ) END) <=5 THEN (CASE WHEN "Agreements - Sales Order Detail"."Date Scheduled Pick" >= '1/1/1900' THEN 0 ELSE TIMESTAMPDIFF(SQL_TSI_DAY,"Agreements - Sales Order Detail"."Date Order Transaction","Agreements - Sales Order Detail"."Date Scheduled Pick" ) END) END
When i tried to reuse it in the following CASE WHEN,
CASE WHEN LT Pick Date less Wkends < "Item - Item Branch"."Leadtime MFG" Then 'LT Problem' Else 'Ok'
I am getting syntax error

Hi,
You missed a double quote in 'LT Pick Date less Wkends ';
--> CASE WHEN "LT Pick Date less Wkends < "."Leadtime MFG" Then 'LT Problem' Else 'Ok"
Cheers,
Daan Bakboord
http://obibb.wordpress.com

Similar Messages

  • Please resolve this query for me.

    i have table of supplier(supplier id,comp name,contractname,address, city, region, postal code, country, phone, fax),products(product id,sroduct name,supplier id,categoryid, qtyper unit, unitprice,unists in stock,units on order, reorder level,discontinued),order details(order id,productid, unitprice, qty, discount),orders(orderid,customerid,employee id,orderdate, requieredate, shipped date,shipvia, freight, shipname,shipaddress, shipcity,shipregion), customers(customerid,comp name contact name, contact title, address, city, region,postalcode, country, phone, fax), shippers(shipid,company name, phone), employee(allthe details of employee), categories(category id, catagoryname, description,picture)
    1. I want to contact all customers who have received over $1,000 in discounts on orders this year. Give me the name and phone number of the person to contact at the customers site. Also, list the orders where the total discount was greater than $100. Remember, discount is a percentage of the price.
    2. Give me a list of suppliers and products where we do not have the stock on hand to fill the orders to be shipped. List out the customer and order information for each of the products involved.
    3. Give me a list of all orders that were shipped after the required date for the week of Jan 7, 2001. I want to know the name of the employees that were responsible for the orders.
    4. We are having a golf tournament and I need some prizes. Give me a list of the top 5 suppliers by dollar amount in the last year.
    5. Some customers are taking us for a ride on shipping. Give me a list of customers and the orders involved where more than ½ of their orders are being shipped to a region other than their home region.

    Jakas,
    This forum is for the "SQL Developer" tool - you might get a better response on the "SQL and PL/SQL" forum (PL/SQL).
    theFurryOne

  • PLEASE RESOLVE THE MIRO ERROR

    Hi gurus,
    please resolve the problem urgent.
    while doing MIRO Iam getting the following error.
    after given PO number in MIRO I getting the following error. please how to resolve the problem.
    Error in account determination: table T030K key ASHA VST V0
    Message no. FF709
    Diagnosis
    In the chart of accounts to be posted to, no accounts are defined for the tax code you used.
    Procedure
    Contact your system administrator.
    Define the accounts to which a tax posting is to be made with the tax code entered in Customizing for taxes on sales/purchases.
    To do this, choose Maintain entries (F5).
    where i can determin the accounts, which accounts will be effect.
    Thank you & Regards
    Anil

    Dear Anil,
    Go to the transaction code FTXP. Give the country code and then the tax code.
    Now on the top, there would be a tab for the "Accounts". Click on it and for the chart of accounts, give the GL accounts.
    Alternatively, go to OB40, for the accounting key V0, give the GL accounts.
    Assign points if useful.
    Regards
    Venkatesh

  • MIRO error please resolve the problem

    Hi gurus,
    please resolve the problem urgent.
    while doing MIRO Iam getting the following error.
    after given PO number in MIRO I getting the following error. please how to resolve the problem.
    Error in account determination: table T030K key ASHA VST V0
    Message no. FF709
    Diagnosis
    In the chart of accounts to be posted to, no accounts are defined for the tax code you used.
    Procedure
    Contact your system administrator.
    Define the accounts to which a tax posting is to be made with the tax code entered in Customizing for taxes on sales/purchases.
    To do this, choose Maintain entries (F5).
    where i can determin the accounts, which accounts will be effect.
    Thank you & Regards
    Anil

    Hi,
    You have to maintain the GL accounts for the correct combination of country, tax code, chart of accounts etc in the tcode FTXP for the transaction VST.
    I hope either the transaction VST or the tax code V0 was newly added
    You can get the correct GL account by askign the person who has initiated this change. May be your FI team member also can help.
    T030K is the table which gets updated while you maintain teh entries through FTXP tcode.
    Hope this helps you

  • Please explain the query?

    hello all,
    please explain below query used in solution below, thanks in advance!
    ELECT MAX(P1.ET) AS ST, P2.ST AS ET
    FROM XYZ AS P1
    INNER JOIN XYZ AS P2 ON (P1.ST < P2.ST)
    GROUP BY P2.ST
    HAVING MAX(P1.ET) < P2.ST
    IF OBJECT_ID('XYZ') IS NOT NULL
    DROP TABLE XYZ
    GO
    CREATE TABLE XYZ
    id int identity(1,1),
    ST smalldatetime NOT NULL,
    ET smalldatetime NOT NULL
    GO
    INSERT INTO XYZ (ST, ET)
    VALUES ('2010-01-01 9:00AM', '2010-01-01 10:00AM')
    INSERT INTO XYZ (ST, ET)
    VALUES ('2010-01-01 9:00AM', '2010-01-01 12:00PM')
    INSERT INTO XYZ (ST, ET)
    VALUES ('2010-01-01 1:00PM', '2010-01-01 2:00PM')
    INSERT INTO XYZ (ST, ET)
    VALUES ('2010-01-01 3:00PM', '2010-01-01 5:00PM')
    INSERT INTO XYZ (ST, ET)
    VALUES ('2010-01-01 11:00AM', '2010-01-01 12:00PM')
    GO
    WITH Gaps(Gap) AS
    SELECT COALESCE(SUM(DATEDIFF(MINUTE,ST,ET)), 0)
    FROM (
    SELECT MAX(P1.ET) AS ST, P2.ST AS ET
    FROM XYZ AS P1
    INNER JOIN XYZ AS P2 ON (P1.ST < P2.ST)
    GROUP BY P2.ST
    HAVING MAX(P1.ET) < P2.ST
    ) gaps
    SELECT (
    COALESCE(DATEDIFF(MINUTE, MIN(ST), MAX(ET)), 0)
    - (SELECT Gap FROM Gaps)
    ) / 60.0 TotalHrs
    FROM XYZ

    SELECT MAX(P1.ET) AS ST, P2.ST AS ET
    FROM XYZ AS P1
    INNER JOIN XYZ AS P2 ON (P1.ST < P2.ST)
    GROUP BY P2.ST
    HAVING MAX(P1.ET) < P2.ST
    Finds all of the gaps (that is time that is not in any interval in your original data.  To see how it works, let's look at an example.  I'm going to use data that is a little different than your original data because your data has no gaps. 
    The query still works if you have no gaps, but it is easier to see what it is doing if the data has some gaps.  Also, I'm going to explicitly set the id column instead of making it an identity.  This will make it a little easier to identify each
    row in the following explanation. So the data I'm going to work with is
    CREATE TABLE XYZ
    id int,
    ST smalldatetime NOT NULL,
    ET smalldatetime NOT NULL
    GO
    INSERT INTO XYZ (id, ST, ET)
    VALUES (1, '2010-01-01 9:00AM', '2010-01-01 10:00AM')
    INSERT INTO XYZ (id, ST, ET)
    VALUES (2, '2010-01-01 9:00AM', '2010-01-01 12:00PM')
    INSERT INTO XYZ (id, ST, ET)
    VALUES (3, '2010-01-02 1:00PM', '2010-01-02 2:00PM')
    INSERT INTO XYZ (id, ST, ET)
    VALUES (4, '2010-01-03 3:00PM', '2010-01-03 5:00PM')
    INSERT INTO XYZ (id, ST, ET)
    VALUES (5, '2010-01-03 7:00PM', '2010-01-03 9:00PM')
    Notice that the gaps here are from row 2 to row 3 (12PM on the 1st to 1PM on the 2nd) and row 3 to row 4 (2PM on the 2nd to 3PM on the 3rd) and row 4 to row 5 (5PM on the 3rd to 7PM on the 3rd).  So that's what the above subquery should be finding for
    us.
    To see what a query you don't understand is doing, simplify it to the smallest part you can and see what it returns and then build it up to the final query.  So the simplest thing we can do is just the from clause.  That gives us
    SELECT *
    FROM XYZ AS P1
    INNER JOIN XYZ AS P2 ON (P1.ST < P2.ST)
    /* That gives us the result
    1 2010-01-01 09:00:00 2010-01-01 10:00:00 3 2010-01-02 13:00:00 2010-01-02 14:00:00
    2 2010-01-01 09:00:00 2010-01-01 12:00:00 3 2010-01-02 13:00:00 2010-01-02 14:00:00
    1 2010-01-01 09:00:00 2010-01-01 10:00:00 4 2010-01-03 15:00:00 2010-01-03 17:00:00
    2 2010-01-01 09:00:00 2010-01-01 12:00:00 4 2010-01-03 15:00:00 2010-01-03 17:00:00
    3 2010-01-02 13:00:00 2010-01-02 14:00:00 4 2010-01-03 15:00:00 2010-01-03 17:00:00
    1 2010-01-01 09:00:00 2010-01-01 10:00:00 5 2010-01-03 19:00:00 2010-01-03 21:00:00
    2 2010-01-01 09:00:00 2010-01-01 12:00:00 5 2010-01-03 19:00:00 2010-01-03 21:00:00
    3 2010-01-02 13:00:00 2010-01-02 14:00:00 5 2010-01-03 19:00:00 2010-01-03 21:00:00
    4 2010-01-03 15:00:00 2010-01-03 17:00:00 5 2010-01-03 19:00:00 2010-01-03 21:00:00
    Now we want to Group by P2.ST and get the MAX(P1.ET) and P2.ST, so that gives us
    SELECT MAX(P1.ET) AS ST, P2.ST AS ET
    FROM XYZ AS P1
    INNER JOIN XYZ AS P2 ON (P1.ST < P2.ST)
    GROUP BY P2.ST
    /* Result is
    2010-01-01 12:00:00 2010-01-02 13:00:00
    2010-01-02 14:00:00 2010-01-03 15:00:00
    2010-01-03 17:00:00 2010-01-03 19:00:00
    Now with this sample data there are no rows in the output with MAX(P1.ET) > P2.ST.  But if there was one, you would not want that row because it is not a real gap (obviously, a gap can't start today and end yesterday).  (If you want to see how
    you could get a case like that, add a row 6 to the sample data with a ST of 2010-01-01 7:00PM and an ET of 2010-01-03 9:00PM.)
    So we add a HAVING MAX(P1.ET) < P2.ST to remove those cases.
    That leaves us with all of the gaps.  So then with
    SELECT COALESCE(SUM(DATEDIFF(MINUTE,ST,ET)), 0)
    FROM (
    SELECT MAX(P1.ET) AS ST, P2.ST AS ET
    FROM XYZ AS P1
    INNER JOIN XYZ AS P2 ON (P1.ST < P2.ST)
    GROUP BY P2.ST
    HAVING MAX(P1.ET) < P2.ST
    ) gaps
    we get the total amount of time in all gaps.  Then the final result is just the time from the earliest ST to the latest ET minus the total time from the gap.
    Tom

  • TS3694 i have i phone 3g ios 4.2.1 so i wana to restore so now it is showing an error 1015 so please resolve my problem

    i have i phone 3g ios 4.2.1 so i wana to restore so now it is showing an error 1015 so please resolve my problem

    Shiv lal wrote:
    i have i phone 3g ios 4.2.1 ..  now it is showing an error 1015
    Unauthorized modification of iOS  >  http://support.apple.com/kb/HT3743
    ERROR 1015...
    This Error Code is indicative of the Device being jailbroken / Hacked...
    Sorry... But...
    The discussion of Jailbroken Devices is against the Terms of Use of this Forum.
    You will need to look elsewhere.

  • The query processor ran out of stack space during query optimization. Please simplify the query

    Can you suggest me that what should i do in this case.
    Actually i am having one table that is a MasterTable.I am referring this table in more than 300 tables that means i am having foreign key of this primary key in 300+ tables.
    due to this i am getting following error during deleting any row,
    doesn't matter that data is existing in reference table or not.
    Error that i am getting is 
    "The query processor ran out of stack space during query optimization. Please simplify the query"
    Can you suggest me that what should i do to avoid this error,because i am unable to delete this entry.
    Apart from it,i am getting performance problem too,so is it due to such huge FK on it.
    Please suggest me on following points
    1. Is it worst way to handle it,if yes then please suggest me solution for it.
    2. If it is a correct way then what should i do if getting error during deleting any record.
    3. Is it right to create Foreign key on each table where i am saving data of this master. if no then how to manage integrity.
    4. What people do in huge database when they wants to create foreign key for any primary key.
    5. Can you suggest me that how DBA's are handling this in big database,where they are having huge no. of tables.

    The most common reason of getting such error is having more than 253 foreign key constraints on a table. 
    The max limit is documented here:
    http://msdn.microsoft.com/en-us/library/ms143432(SQL.90).aspx 
    Although a table can contain an unlimited number of FOREIGN KEY constraints, the recommended maximum is 253. Depending on the hardware configuration hosting SQL Server, specifying additional foreign key constraints may be expensive for the query
    optimizer to process. If you are on 32 bit, then you might want to move to 64 bit to get little bigger stack space but eventually having 300 FK is not something which would work in long run.
    Balmukund Lakhani | Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
    This posting is provided "AS IS" with no warranties, and confers no rights.
    My Blog |
    Team Blog | @Twitter
    Author: SQL Server 2012 AlwaysOn -
    Paperback, Kindle

  • Please convert decode query to case expression

    Please convert decode query to case expression
    Decode(Sign(id), 1, id, null) positive, Decode(Sign(id), -1, id, null) negative from dual;

    this is a serious forums that help people, if you want that we do your "homeworks" i must told you that you should pay us for that.
    Edited by: Fran on 05-jun-2013 23:41
    1002966     
    Handle:      1002966 
    Status Level:      Newbie
    Registered:      Apr 28, 2013
    Total Posts:      20
    Total Questions:      12 (12 unresolved)

  • Please resolve ASAP

    Hello.
    I was billed twice for my SUBSCRIPTION to Creative Cloud Student. I am now paying for two subscription and I only want one on my current account (obviously). Please resolve this ASAP, before the first month is over and I am forced to pay this twice.
    Thank you. -Kyle

    I doubt posting here will resolve your issue, why don't you phone Adobe?
    https://helpx.adobe.com/creative-cloud/team/creative-cloud-teams.html

  • Please show this query, data not showing why?

    I created a report and write following query,it was working well since last 4 months but today automaticly not showing data I can undertstand why?
    Becuase I didnt make any changes in this query.
    Please help me
    Urgent
    SELECT ALL MERCH_ORDER.ORDERNO, MERCH_ORDER.ORDERDATE, MERCH_ORDER.SHIP_DATE, MERCH_ORDER.PONO,
    MERCH_ORDER.SUBPP, MERCH_ORDER.PJNO, BUYER.B_NAME, BUYER.B_AJENT,
    MERCH_ORDER.ITEM, MERCH_ORDER.FABRIC, MERCH_ORDER.QUALITY, MERCH_ORDER.COMPOSITION,
    MERCH_ORDER.P_SIZE, MERCH_ORDER.QUANTITY, MERCH_ORDER.Q_UNIT,
    MERCH_ORDER.NETWHT, MERCH_ORDER.WT_UNIT, MERCH_ORDER.TERM, MERCH_ORDER.COMM,
    MERCH_ORDER.PRICE, MERCH_ORDER.CUR_SYMB, MERCH_ORDER.STATUS, MERCH_ORDER.REMARKS,
    MERCH_ORDER.WONO, MERCH_ORDER.PRONO, MERCH_ORDER.PES_QUANTITY,
    MERCH_ORDER.PES_Q_UNIT, MERCH_ORDER.PES_PRICE, MERCH_ORDER.PES_CUR_SYMB
    FROM BUYER, MERCH_ORDER
    WHERE MERCH_ORDER.CANCEL IS NULL
    AND (MERCH_ORDER.B_CODE = BUYER.B_CODE)
    and merch_order.orderno not in
    (select export_order1.orderno from export_order1)
    ORDER BY MERCH_ORDER.ORDERNO
    there is no any error and msg header and footer print.

    Maybe , the query in "NOT IN" clause select export_order1.orderno from export_order1
    return the same rows as the following portion return....
    SELECT ALL MERCH_ORDER.ORDERNO, MERCH_ORDER.ORDERDATE, MERCH_ORDER.SHIP_DATE, MERCH_ORDER.PONO,
    MERCH_ORDER.SUBPP, MERCH_ORDER.PJNO, BUYER.B_NAME, BUYER.B_AJENT,
    MERCH_ORDER.ITEM, MERCH_ORDER.FABRIC, MERCH_ORDER.QUALITY, MERCH_ORDER.COMPOSITION,
    MERCH_ORDER.P_SIZE, MERCH_ORDER.QUANTITY, MERCH_ORDER.Q_UNIT,
    MERCH_ORDER.NETWHT, MERCH_ORDER.WT_UNIT, MERCH_ORDER.TERM, MERCH_ORDER.COMM,
    MERCH_ORDER.PRICE, MERCH_ORDER.CUR_SYMB, MERCH_ORDER.STATUS, MERCH_ORDER.REMARKS,
    MERCH_ORDER.WONO, MERCH_ORDER.PRONO, MERCH_ORDER.PES_QUANTITY,
    MERCH_ORDER.PES_Q_UNIT, MERCH_ORDER.PES_PRICE, MERCH_ORDER.PES_CUR_SYMB
    FROM BUYER, MERCH_ORDER
    WHERE MERCH_ORDER.CANCEL IS NULL
    AND (MERCH_ORDER.B_CODE = BUYER.B_CODE)
    OR
    there are no rows which conform to the joining condition between the two tables BUYER and MERCH_ORDER ....
    Regards,
    Simon

  • TS2446 Hi, please resolve my problem with my apple ID

    Hi
    I just redeem itunes gift card $100 and purchasing gems on game Clash of Clan, but i received alert can't purchased and contact support
    Please resond me soon
    Thank you so much for help

    Welcome to the Apple Community.
    Did you follow the instructions in the message and contact Apple, which you can do through iTunes Store Support. If Apple have asked you to contact them, then the issue can only be resolved by them.

  • Please tune this query.

    Hi Experts,
    My below query is taking long time.
    [code]SELECT FAX_LIST.*
        FROM (SELECT /*+ use_nl(fax, src)  */
                    ROW_NUMBER () OVER (ORDER BY fax.ID ASC NULLS FIRST) RN,
                     fax.ACCOUNT_TYPE,
                     fax.BU_FILE_LOCATION,
                     fax.COUNT_PAGES_RECEIVED,
                     NVL (fax.CUSTOMER_NAME, fax.SENDER_NAME) AS CUSTOMER_NAME,
                     fax.FAX_SOURCE,
                     fax.CUSTOMER_NUMBER,
                     fax.CUSTOMER_OMEGA_NUMBER,
                     fax.GENIFAX_RECIPIENT_ID,
                     fax.ID AS FAX_ID,
                     fax.DATE_RECEIVED AS FAX_RECEIVED,
                     fax.IS_LOCKED,
                     fax.LOCKED_BY,
                     fax.ORIGINATOR_CSI,
                     fax.MARGIN,
                     fax.PAYMENT_TYPE,
                     fax.PRIORITY_CODE,
                     fax.PRIORITY_VALUE,
                     FROM_TZ (fax.DATE_RECEIVED, 'UTC')
                        AT TIME ZONE fax.LOCAL_TZ_NAME
                        AS DATE_RECEIVED,
                     fax.SALES_PERSON,
                     fax.SENDER_EMAIL,
                     fax.SENDER_NAME,
                     fax.SUBJECT,
                     fax.WORKGROUP_ID,
                     fax.LOCKED_TIME,
                     src.DESCRIPTION,
                     src.FAX_NUMBER,
                     src.WORKFLOW_ID,
                     fax.CREATED_BY,
                     fax.CREATED_DATE,
                     fax.UPDATE_DATE,
                     fax.UPDATED_BY,
                     fax.RESERVED_BY,
                     fax.PRICE_TO_ORDER,
                     fax.SKU_COUNT_TO_ORDER,
                     NVL (ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).CHANGED_BY,
                        CASE
                           WHEN fax.LAST_TRANSITION_ID IS NULL
                                AND fax.GENIFAX_RECIPIENT_ID IS NOT NULL
                           THEN
                              'ORL FEEDER'
                           ELSE
                              'Admin'
                        END)
                        AS LAST_USER,
                     FROM_TZ (
                        NVL (ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).CHANGED_DATE,
                             fax.DATE_RECEIVED),
                        'UTC')
                        AT TIME ZONE fax.LOCAL_TZ_NAME
                        AS MOVE_DATETIME,
                     NVL (ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).REASON,
                          'Reroute')
                        AS MOVE_REASON,
                     NVL (
                        (SELECT fw.DESCRIPTION
                           FROM ORL.WORKGROUP_TRANSITION wt, ORL.WORKGROUP fw
                          WHERE wt.ID =
                                   ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).WORKGROUP_TRANSITION_ID
                                AND fw.ID = wt.CURRENT_WORKGROUP_ID),
                        CASE
                           WHEN fax.LAST_TRANSITION_ID IS NULL
                                AND fax.GENIFAX_RECIPIENT_ID IS NOT NULL
                           THEN
                              'ORL FEEDER STAGING'
                           ELSE
                              'N/A'
                        END)
                        AS OLD_STATUS,
                     (SELECT MAX (PURCHASE_ORDER_NUMBER)
                        FROM ORL.FAX_OFFER_DETAIL
                       WHERE FAX_ID = fax.ID)
                        AS MAXPO,
                     (SELECT CASE COUNT (PURCHASE_ORDER_NUMBER)
                                WHEN 0 THEN NULL
                                ELSE COUNT (PURCHASE_ORDER_NUMBER)
                             END
                                AS POCOUNT
                        FROM ORL.FAX_OFFER_DETAIL
                       WHERE FAX_ID = fax.ID)
                        AS POCOUNT,
                     (SELECT SUM (VALUE) AS ORDER_VALUE
                        FROM ORL.FAX_OFFER_DETAIL
                       WHERE FAX_ID = fax.ID)
                        AS ORDER_VALUE,
                     fax.SALESPERSON_NAME,
                     fax.GROUP_NAME,
                     fax.ROLE_NAME,
                     fax.EMAIL,
                     fax.SALES_CHANNEL
                FROM (SELECT fax.*,
                             map.ORACLE_TZ_NAME AS LOCAL_TZ_NAME,
                             ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).SALESPERSON_NAME
                                AS SALESPERSON_NAME,
                             ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).GROUP_NAME
                                AS GROUP_NAME,
                             ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).ROLE_NAME
                                AS ROLE_NAME,
                             ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).EMAIL
                                AS EMAIL,
                             ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).SALES_CHANNEL
                                AS SALES_CHANNEL
                        FROM APPS_GLOBAL.GLOBAL_BU_MAPPING map,
                             (SELECT *
                                FROM ORL.FAX_HEADER FH
                               WHERE FH.WORKGROUP_ID = 262) fax
                       WHERE map.GEDIS = 'Y'
                             AND map.BU_ID = (SELECT BUID
                                                FROM ORL.WORKGROUP
                                               WHERE ID = fax.WORKGROUP_ID)
                             AND ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).SALESPERSON_NAME =
                                    'A_CARRICK') fax,
                     (SELECT *
                        FROM ORL.FAX_SOURCE FS
                       WHERE FS.WORKFLOW_ID IN
                                (SELECT  /*+ CARDINALITY(t, 1) */
                                       TO_NUMBER (
                                           COLUMN_VALUE)
                                           AS COLUMN_VALUE
                                   FROM TABLE (
                                           SplitClob ('1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
            16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
            32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
            48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
            64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
            80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
            96, 97, 98', ',')) t)) src
               WHERE src.ID(+) = fax.FAX_SOURCE) FAX_LIST
       WHERE RN BETWEEN 1 AND 100
    ORDER BY RN ASC;[/code]
    In my query the following three functions are using.
    ORL.GET_FTH_WTR
    ORL.GET_SALESPERSON
    APPS_GLOBAL.SplitClob
    [code]CREATE OR REPLACE FUNCTION ORL.GET_FTH_WTR(p_LAST_TRANSITION_ID NUMBER)
            RETURN ORL.FTH_WTR_T
            DETERMINISTIC
        IS
            v_REASON nvarchar2(1024 char);
            v_WORKGROUP_TRANSITION_ID number;
            v_CHANGED_BY nvarchar2(64 char);
            v_CHANGED_DATE timestamp(6);
        BEGIN
        SELECT
            WTR.REASON,
            wtr.WORKGROUP_TRANSITION_ID,
            fth.CHANGED_BY,
            fth.CHANGED_DATE
        INTO
            v_REASON,
            v_WORKGROUP_TRANSITION_ID,
            v_CHANGED_BY,
            v_CHANGED_DATE
        FROM
            ORL.FAX_TRANSITION_HISTORY fth,
            ORL.WORKGROUP_TRANSITION_REASON wtr
        WHERE
             fth.ID = p_LAST_TRANSITION_ID
            AND wtr.ID(+) = fth.TRANSITION_REASON_ID;
        RETURN ORL.FTH_WTR_T(v_CHANGED_BY, v_CHANGED_DATE, v_REASON, v_WORKGROUP_TRANSITION_ID);
        EXCEPTION
            WHEN NO_DATA_FOUND THEN
            RETURN ORL.FTH_WTR_T(NULL, NULL, NULL, NULL);
        END GET_FTH_WTR;
    CREATE OR REPLACE function ORL.GET_SALESPERSON(ORG_ID number, SALESPERSON_ID number)
        return ORL.SALESPERSON_T
        deterministic
    is
        apps_bu             varchar2(30);
        salesperson_name    varchar2(150 char);
        email               varchar2(70 char);
        sales_channel       varchar2(60 char);
        role_name           varchar2(60 char);
        group_name          varchar2(60 char);
    begin
        select OWNER_NAME into apps_bu
          from APPS_GLOBAL.GLOBAL_BU_MAPPING
         where ORG_ID = GET_SALESPERSON.ORG_ID;
        execute immediate
        replace('
            select SALESPERSON_NAME
                 , EMAIL
                 , SALES_CHANNEL
                 , ROLE_NAME
                 , GROUP_NAME
              from APPS_**.ORL_ACTIVE_SALESPERSON
             where SALESPERSON_ID = :1
      and  rownum = 1
                ', 'APPS_**', apps_bu)
        into salesperson_name, email, sales_channel, role_name, group_name
        using SALESPERSON_ID;
        return SALESPERSON_T(ORG_ID, SALESPERSON_ID, salesperson_name, email, sales_channel, role_name, group_name);
    exception
        when no_data_found then
            return SALESPERSON_T(null, null, null, null, null, null, null);
    end GET_SALESPERSON;
    CREATE OR REPLACE function APPS_GLOBAL.SplitClob
        p_clob          clob
      , p_delimiter     varchar2 := ','
    return StringTable
    deterministic
    pipelined
    as
        v_current_pos   pls_integer := 1;
        v_delimiter_pos pls_integer;
    begin
        if (p_clob is not NULL) and (p_delimiter is not NULL) then
            while v_current_pos <= length(p_clob) loop
                v_delimiter_pos := instr(p_clob, p_delimiter, v_current_pos);
                if  v_delimiter_pos < 1  then   -- no more delimiters
                    v_delimiter_pos := length(p_clob) + 1;
                end if;
                pipe row( to_char( substr(p_clob,
                                          v_current_pos,
                                          v_delimiter_pos - v_current_pos) ) );
                v_current_pos := v_delimiter_pos + length(p_delimiter);
            end loop;
        end if;
    end SplitClob;
    CREATE OR REPLACE TYPE SALESPERSON_T  AS OBJECT
      SALESPERSON_ID        number(15)
    , SALESPERSON_NUMBER    varchar2(150 char)
    , FIRST_NAME            varchar2(20 char)
    , LAST_NAME             varchar2(40 char)
    , SALES_CHANNEL         varchar2(60 char)
    , ORG_ID                number(15)
    , USER_NAME             varchar2(61 char)
    , EFFECTIVE_START_DATE  date
    , EFFECTIVE_END_DATE    date
    , STATUS_FLAG           varchar2(1 char)
    , EMAIL                 varchar2(70 char)
    , WORK_TELEPHONE        varchar2(60 char)
    , MANAGERS_NAME         varchar2(50 char)
    , FAX_NO                varchar2(60 char)
    , SALESPERSON_NAME      varchar2(30 char)
    , TERRITORY             varchar2(40 char)
    , FO_LOGON              varchar2(150 char)
    , BO_LOGON              varchar2(150 char)
    , SUB_CHANNEL           varchar2(25 char)
    , BUSINESS_SEGMENT      varchar2(3 char)
    , DISCOUNT_NAME         varchar2(30 char)
    , RESPONSIBILITY_ID     number
    , RESPONSIBILITY_KEY    varchar2(30 char)
    CREATE OR REPLACE TYPE STRINGTABLE as table of varchar2(4000);[/code]
    The total number of records in each table.
    [code]SELECT COUNT(*) FROM ORL.FAX_HEADER FH --4397829
    SELECT COUNT(*) FROM APPS_GLOBAL.GLOBAL_BU_MAPPING map --31
    SELECT COUNT(*) FROM ORL.WORKGROUP_TRANSITION  --6735
    SELECT COUNT(*) FROM ORL.WORKGROUP fw --1495
    SELECT COUNT(*) FROM ORL.FAX_OFFER_DETAIL --5904039
    SELECT COUNT(*) FROM  ORL.FAX_SOURCE--2368 --2457[/code]
    Indexes on the columns.
    [code]ORL.WORKGROUP_TRANSITION(ID),
    ORL.WORKGROUP(ID),
    ORL.FAX_OFFER_DETAIL(FAX_ID),
    ORL.FAX_HEADER(WORKGROUP_ID).[/code]
    Please help me to tune this query.
    Thanks in advance.

    Hi,
    Thanks for your reply.
    For split the numbers into different rows , we are sending randomly.
    It's not constant numbers.We have used bind variables.
    For testing purpose I have hard coded the values.
    How to do this split without using user defined function.
    I am sending the actual query along with execution plan without any hints.
    [code]SELECT   FAX_LIST.*
      FROM   (SELECT   ROW_NUMBER () OVER (ORDER BY fax.ID ASC) RN,
                       fax.ACCOUNT_TYPE,
                       fax.BU_FILE_LOCATION,
                       fax.COUNT_PAGES_RECEIVED,
                       NVL (fax.CUSTOMER_NAME, fax.SENDER_NAME) AS CUSTOMER_NAME,
                       fax.FAX_SOURCE,
                       fax.CUSTOMER_NUMBER,
                       fax.CUSTOMER_OMEGA_NUMBER,
                       fax.GENIFAX_RECIPIENT_ID,
                       fax.ID AS FAX_ID,
                       fax.DATE_RECEIVED AS FAX_RECEIVED,
                       fax.IS_LOCKED,
                       fax.LOCKED_BY,
                       fax.ORIGINATOR_CSI,
                       fax.MARGIN,
                       fax.PAYMENT_TYPE,
                       fax.PRIORITY_CODE,
                       fax.PRIORITY_VALUE,
                       FROM_TZ (fax.DATE_RECEIVED, 'UTC') AT TIME ZONE map.ORACLE_TZ_NAME AS DATE_RECEIVED,
                       fax.SALES_PERSON,
                       fax.SENDER_EMAIL,
                       fax.SENDER_NAME,
                       fax.SUBJECT,
                       fax.WORKGROUP_ID,
                       fax.LOCKED_TIME,
                       src.DESCRIPTION,
                       src.FAX_NUMBER,
                       src.WORKFLOW_ID,
                       fax.CREATED_BY,
                       fax.CREATED_DATE,
                       fax.UPDATE_DATE,
                       fax.UPDATED_BY,
                       fax.RESERVED_BY,
                       fax.PRICE_TO_ORDER,
                       fax.SKU_COUNT_TO_ORDER,
                       NVL (
                          ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).CHANGED_BY,
                          CASE
                             WHEN fax.LAST_TRANSITION_ID IS NULL
                                  AND fax.GENIFAX_RECIPIENT_ID IS NOT NULL
                             THEN
                                'ORL FEEDER'
                             ELSE
                                'Admin'
                          END
                          AS LAST_USER,
                       FROM_TZ (NVL (ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).CHANGED_DATE,fax.DATE_RECEIVED),'UTC')
                       AT TIME ZONE map.ORACLE_TZ_NAME AS MOVE_DATETIME,
                       NVL (ORL.GET_FTH_WTR (fax.LAST_TRANSITION_ID).REASON,'Reroute')AS MOVE_REASON,
                       NVL ((SELECT   fw.DESCRIPTION
                             FROM   ORL.WORKGROUP_TRANSITION wt, ORL.WORKGROUP fw
                            WHERE   wt.ID =
                                       ORL.GET_FTH_WTR (
                                          fax.LAST_TRANSITION_ID
                                       ).WORKGROUP_TRANSITION_ID
                                    AND fw.ID = wt.CURRENT_WORKGROUP_ID),
                          CASE
                             WHEN fax.LAST_TRANSITION_ID IS NULL
                                  AND fax.GENIFAX_RECIPIENT_ID IS NOT NULL
                             THEN
                                'ORL FEEDER STAGING'
                             ELSE
                                'N/A'
                          END
                          AS OLD_STATUS,
                       (SELECT   MAX (PURCHASE_ORDER_NUMBER)
                          FROM   ORL.FAX_OFFER_DETAIL
                         WHERE   FAX_ID = fax.ID)
                          AS MAXPO,
                       (SELECT   CASE COUNT (PURCHASE_ORDER_NUMBER)
                                    WHEN 0 THEN NULL
                                    ELSE COUNT (PURCHASE_ORDER_NUMBER)
                                 END
                                    AS POCOUNT
                          FROM   ORL.FAX_OFFER_DETAIL
                         WHERE   FAX_ID = fax.ID)
                          AS POCOUNT,
                       (SELECT   SUM (VALUE) AS ORDER_VALUE
                          FROM   ORL.FAX_OFFER_DETAIL
                         WHERE   FAX_ID = fax.ID) AS ORDER_VALUE,
                       ORL.GET_SALESPERSON (map.ORG_ID,fax.SALES_PERSON).SALESPERSON_NAME AS SALESPERSON_NAME,
                       ORL.GET_SALESPERSON (map.ORG_ID,fax.SALES_PERSON).GROUP_NAME AS GROUP_NAME,
                       ORL.GET_SALESPERSON (map.ORG_ID,fax.SALES_PERSON).ROLE_NAME AS ROLE_NAME,
                       ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).EMAIL AS EMAIL,
                       ORL.GET_SALESPERSON (map.ORG_ID, fax.SALES_PERSON).SALES_CHANNEL AS SALES_CHANNEL
                FROM   ORL.FAX_HEADER fax,
                       APPS_GLOBAL.GLOBAL_BU_MAPPING map,
                       ORL.FAX_SOURCE src
               WHERE       fax.WORKGROUP_ID = :WORKGROUP_ID
                       AND map.GEDIS = 'Y'
                       AND map.BU_ID = (SELECT   BUID
                                          FROM   ORL.WORKGROUP
                                         WHERE   ID = fax.WORKGROUP_ID)
                       AND ORL.GET_SALESPERSON (map.ORG_ID,fax.SALES_PERSON).GROUP_NAME =:SALES_TEAM
                       AND ORL.GET_SALESPERSON (map.ORG_ID,fax.SALES_PERSON
                          ).SALESPERSON_NAME = :SALESPERSON_NAME
                       AND src.WORKFLOW_ID IN
                                (SELECT TO_NUMBER (COLUMN_VALUE) AS COLUMN_VALUE
                                   FROM table(SplitClob (:WORKFLOW_ID,:WORKFLOW_ID_delim)) t)
                       AND src.ID(+) = fax.FAX_SOURCE) FAX_LIST
    WHERE   RN BETWEEN 1 AND 100;
    Execution Plan
    | Id  | Operation                               | Name                    | Rows  | Bytes | Cost (%CPU)|
    |   0 | SELECT STATEMENT                        |                         |     2 | 12092 | 88923   (1)|
    |   1 |  NESTED LOOPS                           |                         |     1 |    38 |     3   (0)|
    |   2 |   TABLE ACCESS BY INDEX ROWID           | WORKGROUP_TRANSITION    |     1 |     9 |     2   (0)|
    |   3 |    INDEX UNIQUE SCAN                    | PK_WORKGROUP_TRANSITION |     1 |       |     1   (0)|
    |   4 |   TABLE ACCESS BY INDEX ROWID           | WORKGROUP               |  1482 | 42978 |     1   (0)|
    |   5 |    INDEX UNIQUE SCAN                    | PK_WORKGROUP            |     1 |       |     0   (0)|
    |   6 |  SORT AGGREGATE                         |                         |     1 |    16 |            |
    |   7 |   TABLE ACCESS BY INDEX ROWID           | FAX_OFFER_DETAIL        |     1 |    16 |     5   (0)|
    |   8 |    INDEX RANGE SCAN                     | FAX_OFFER_DETAIL_IDX1   |     1 |       |     3   (0)|
    |   9 |  SORT AGGREGATE                         |                         |     1 |    16 |            |
    |  10 |   TABLE ACCESS BY INDEX ROWID           | FAX_OFFER_DETAIL        |     1 |    16 |     5   (0)|
    |  11 |    INDEX RANGE SCAN                     | FAX_OFFER_DETAIL_IDX1   |     1 |       |     3   (0)|
    |  12 |  SORT AGGREGATE                         |                         |     1 |    11 |            |
    |  13 |   TABLE ACCESS BY INDEX ROWID           | FAX_OFFER_DETAIL        |     1 |    11 |     5   (0)|
    |  14 |    INDEX RANGE SCAN                     | FAX_OFFER_DETAIL_IDX1   |     1 |       |     3   (0)|
    |  15 |  VIEW                                   |                         |     2 | 12092 | 88923   (1)|
    |  16 |   WINDOW SORT PUSHED RANK               |                         |     2 |  1378 | 88923   (1)|
    |  17 |    NESTED LOOPS                         |                         |       |       |            |
    |  18 |     NESTED LOOPS                        |                         |     2 |  1378 | 88920   (1)|
    |  19 |      NESTED LOOPS                       |                         | 18420 |    11M| 70490   (1)|
    |  20 |       HASH JOIN RIGHT SEMI              |                         |    69 |  3657 |    49   (3)|
    |  21 |        COLLECTION ITERATOR PICKLER FETCH| SPLITCLOB               |  8168 | 16336 |    29   (0)|
    |  22 |        TABLE ACCESS FULL                | FAX_SOURCE              |  2409 |   119K|    19   (0)|
    |  23 |       TABLE ACCESS BY INDEX ROWID       | FAX_HEADER              |   268 |   160K|  2854   (1)|
    |  24 |        INDEX RANGE SCAN                 | FAX_HEADER_FS_IDX       |  4345 |       |     7   (0)|
    |  25 |      INDEX RANGE SCAN                   | GLOBAL_BU_MAPPING_BUID  |     1 |       |     0   (0)|
    |  26 |       TABLE ACCESS BY INDEX ROWID       | WORKGROUP               |     1 |     8 |     2   (0)|
    |  27 |        INDEX UNIQUE SCAN                | PK_WORKGROUP            |     1 |       |     1   (0)|
    |  28 |     TABLE ACCESS BY INDEX ROWID         | GLOBAL_BU_MAPPING       |     1 |    24 |     1   (0)|
    Note
       - 'PLAN_TABLE' is old version
    Statistics
         894400  recursive calls
             60  db block gets
        7402741  consistent gets
              0  physical reads
              0  redo size
          46309  bytes sent via SQL*Net to client
            430  bytes received via SQL*Net from client
              8  SQL*Net roundtrips to/from client
         848304  sorts (memory)
              0  sorts (disk)
            100  rows processed[/code]
    Please help me.
    Thanks in advance.

  • Please see this query, not showing data Why?

    I created a report and write following query,it was working well since last 4 months but today automaticly not showing data I can undertstand why?
    Becuase I didnt make any changes in this query.
    Please help me
    Urgent
    SELECT ALL MERCH_ORDER.ORDERNO, MERCH_ORDER.ORDERDATE, MERCH_ORDER.SHIP_DATE, MERCH_ORDER.PONO,
    MERCH_ORDER.SUBPP, MERCH_ORDER.PJNO, BUYER.B_NAME, BUYER.B_AJENT,
    MERCH_ORDER.ITEM, MERCH_ORDER.FABRIC, MERCH_ORDER.QUALITY, MERCH_ORDER.COMPOSITION,
    MERCH_ORDER.P_SIZE, MERCH_ORDER.QUANTITY, MERCH_ORDER.Q_UNIT,
    MERCH_ORDER.NETWHT, MERCH_ORDER.WT_UNIT, MERCH_ORDER.TERM, MERCH_ORDER.COMM,
    MERCH_ORDER.PRICE, MERCH_ORDER.CUR_SYMB, MERCH_ORDER.STATUS, MERCH_ORDER.REMARKS,
    MERCH_ORDER.WONO, MERCH_ORDER.PRONO, MERCH_ORDER.PES_QUANTITY,
    MERCH_ORDER.PES_Q_UNIT, MERCH_ORDER.PES_PRICE, MERCH_ORDER.PES_CUR_SYMB
    FROM BUYER, MERCH_ORDER
    WHERE MERCH_ORDER.CANCEL IS NULL
    AND (MERCH_ORDER.B_CODE = BUYER.B_CODE)
    and merch_order.orderno not in
    (select export_order1.orderno from export_order1)
    ORDER BY MERCH_ORDER.ORDERNO

    Where "first table" is merch_order and "second table" is export_order1?
    How many distinct orders are in each table?
    Are there any NULL order numbers in either table?
    I'd put money on the fact that if commenting out a clause causes a number of rows to be returned, that clause is filtering out all the rows. You'll need to go through your data to figure out why the NOT IN clause is filtering out all your rows.
    Justin

  • Please give the query to find out primary key in table in Sql plus

    Dear friends,
    Please give me the query to find out the primary key in Sql plus.

    hi
    SQL> DESC user_constraints
    Name                                                                                                  
    OWNER                                                                                                 
    CONSTRAINT_NAME                                                                                       
    CONSTRAINT_TYPE                                                                                       
    TABLE_NAME                                                                                            
    SEARCH_CONDITION                                                                                      
    R_OWNER                                                                                               
    R_CONSTRAINT_NAME                                                                                     
    DELETE_RULE                                                                                           
    STATUS                                                                                                
    DEFERRABLE                                                                                            
    DEFERRED                                                                                              
    VALIDATED                                                                                             
    GENERATED                                                                                             
    BAD                                                                                                   
    RELY                                                                                                  
    LAST_CHANGE                                                                                           
    INDEX_OWNER                                                                                           
    INDEX_NAME                                                                                            
    INVALID                                                                                               
    VIEW_RELATED                                                                                          
    SQL> SELECT constraint_name,table_name,r_constraint_name,status
      2  FROM user_constraints WHERE constraint_type='P';
    CONSTRAINT_NAME                TABLE_NAME                     R_CONSTRAINT_NAME              STATUS
    SYS_C003141                    CUSTOMERS                                                     ENABLED
    PK_DEPT                        DEPT                                                          ENABLED
    SYS_C003139                    SALESREPS                                                     ENABLEDKhurram

  • Shall u please build a query for the below requirement

    Existing:
    policy  endmt     type         CR      CCR
    101      0         CEED          10      4       
    101      1         CEED           2       6        
    101      2         NONCEED     1      14        
    101      3         CEED          11      3        
    101      4         CEED           6       1
    101      5         NONCEED     7       2
    101      6         NONCEED    10      3
    101      7         CEED           9       4Required Output:
    policy  endmt     type         CR      CCR  OUTPUT
    101      0         CEED          10      4         10
    101      1         CEED           2       6          6
    101      2         NONCEED     1      14         -2
    101      3         CEED          11      3          11
    101      4         CEED           6       1           1
    101      5         NONCEED     7       2          -6
    101      7         CEED           9       4           9Explanation:
    CASE1:For a policy if starting endorsement is 'CEED' then 'CR' Value should consider.
    CASE2:IF for the policy previous endorsement is 'CEED' and curent endorsement is 'CEED' then 'CCR' value
    CASE3: :IF for the policy previous endordement is 'CEED' and curent endorment is 'NONCEED' then
    Previous endorsement 'CR' *-1  value.
    CASE4::IF for the policy previous endordement is 'NONCEED' and curent endorment is 'NONCEED' then
    eliminate the current endorsement row
    CASE5:IF for the policy previous endorsement is 'NONCEED' and curent endorsement is 'CEED' then 'CR'
    Shall u please help in resolving it?
    Thanks
    Edited by: BluShadow on 15-Oct-2012 14:21
    added {noformat}{noformat} tags to help readability (though there's no helping poorly pasted data).  Please read {message:id=9360002} and learn to paste your code/data correctly.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    886380 wrote:
    Thanks ,but
    endorsement number may not be in sequence ,same endorsement number can repeat for a policy.
    Then that scenario it wont work.On what basis you will ORDE, if you have same endorsement number ?
    Something like this can be done (If you can find out some unique combination to ORDER. Here I am using "order by policy,endmt,type"
    SQL> WITH t AS
      2  (SELECT 101 policy, 0 endmt, 'CEED' type, 10 cr, 4 ccr FROM dual UNION
      3   SELECT 101 policy, 1 endmt, 'CEED' type, 2 cr, 6 ccr FROM dual UNION
      4   SELECT 101 policy, 2 endmt, 'NONCEED' type, 1 cr, 14 ccr FROM dual UNION
      5   SELECT 101 policy, 3 endmt, 'CEED' type, 11 cr, 3 ccr FROM dual UNION
      6   SELECT 101 policy, 4 endmt, 'CEED' type, 6 cr, 1 ccr FROM dual UNION
      7   SELECT 101 policy, 4 endmt, 'NONCEED' type, 7 cr, 2 ccr FROM dual UNION
      8   SELECT 101 policy, 6 endmt, 'NONCEED' type, 10 cr, 3 ccr FROM dual UNION
      9   SELECT 101 policy, 7 endmt, 'CEED' type, 9 cr, 4 ccr FROM dual)
    10  SELECT   t1.policy,
    11           t1.endmt,
    12           t1.cr,
    13           t1.ccr,
    14           CASE
    15           WHEN t1.type = 'CEED' THEN
    16             CASE
    17             WHEN t2.type = 'CEED' THEN t1.ccr
    18             ELSE t1.cr
    19             END
    20           WHEN t1.type = 'NONCEED' THEN
    21             CASE
    22             WHEN t2.type = 'CEED' THEN -t2.cr
    23             ELSE NULL
    24             END
    25           END output
    26  FROM     (select t.*,rownum rn
    27       from (select * from t order by   policy,endmt,type) t
    28           )
    29           t1 LEFT OUTER JOIN
    30     (select t.*,rownum rn
    31       from (select * from t order by   policy,endmt,type) t
    32           ) t2
    33           ON (t1.policy = t2.policy
    34               AND t1.rn -1 = t2.rn)
    35  WHERE   (t1.type = 'CEED'
    36  OR      (t1.type = 'NONCEED'
    37  AND      t2.type = 'CEED'))
    38  ORDER BY t1.policy,
    39           t1.endmt,t1.type;
        POLICY      ENDMT         CR        CCR     OUTPUT
           101          0         10          4         10
           101          1          2          6          6
           101          2          1         14         -2
           101          3         11          3         11
           101          4          6          1          1
           101          4          7          2         -6
           101          7          9          4          9
    7 rows selected.Edited by: jeneesh on Oct 17, 2012 12:10 PM

Maybe you are looking for