Help again to write a query

hello
I hvae made the following query to see goods receipt - purchase order and goods return
It seems ok but I have several times the same records per line I wonder what triggers the number of record per let's say DocNum from OPDN. I would like to see only one record per DucNum OPDN.... Thank you if you find the problem
SELECT T2.[DocNum] as 'N° cde', T2.[DocDate], T2.[DocStatus], T1.[DocNum] as 'N° Entrée Mses', T1.[DocDate],T1.[DocStatus], (T1.DocTotal-T1.VATsum-T1.TotalExpns) AS 'Valeur Entrée Mses', T1.[Weight], T3.[DocNum] as 'N° Retour', T3.[DocDate], T3.[DocStatus], (T3.DocTotal-T3.VATsum-T3.TotalExpns) AS 'Valeur Retour Mses', T3.[Weight] FROM PDN1 T0 
INNER JOIN OPDN T1 ON T0.DocEntry = T1.DocEntry
inner join OPOR T2 ON T0.BaseRef=T2.[DocNum]
LEFT OUTER JOIN RPD1 T4 ON T4.BaseRef=T1.[DocNum]
LEFT OUTER JOIN ORPD T3 ON T4.DocEntry = T3.DocEntry
WHERE T1.DOCDATE>=[%0] AND T1.DOCDATE<=[%1]

Hi G Delanoe
This is happening based on the number of line items in the Purchase Order & Goods Receipt PO. You will need to use a group by on some of the fields, such as the document numbers, to display each document only once.
Try the following and see if it does the job.
SELECT T2.DocNum as 'N° cde', T2.DocDate, T2.DocStatus, T1.DocNum as 'N° Entrée Mses', T1.DocDate,T1.DocStatus, AVG(T1.DocTotal-T1.VATsum-T1.TotalExpns) AS 'Valeur Entrée Mses', AVG(T1.Weight), T3.DocNum as 'N° Retour', T3.DocDate, T3.DocStatus, AVG(T3.DocTotal-T3.VATsum-T3.TotalExpns) AS 'Valeur Retour Mses', AVG(T3.Weight )
FROM PDN1 T0 INNER JOIN OPDN T1 ON T0.DocEntry = T1.DocEntry inner join OPOR T2 ON T0.BaseRef=T2.DocNum LEFT OUTER JOIN RPD1 T4 ON T4.BaseRef=T1.DocNum LEFT OUTER JOIN ORPD T3 ON T4.DocEntry = T3.DocEntry
WHERE T1.DOCDATE>= '[%0]' AND T1.DOCDATE<= '[%1]'
GROUP BY T2.DocNum, T2.DocDate, T2.DocStatus, T1.DocNum, T1.DocDate, T1.DocStatus, T3.DocNum, T3.DocDate, T3.DocStatus
Kind regards
Peter Juby

Similar Messages

  • Please help to re-write this query using exists or with

    Hi please help to re-write this query using exists or with, i need to write same code for 45 day , 90 days and so on but sub query condition is same for all
    SELECT SUM (DECODE (t_one_mon_c_paid_us, 0, 0, 1)) t_two_y_m_mul_ca_
    FROM (SELECT SUM (one_mon_c_paid_us) t_one_mon_c_paid_us
    FROM (
    SELECT a.individual_id individual_id,
    CASE
    WHEN NVL
    (b.ship_dt,
    TO_DATE ('05-MAY-1955')
    ) >= SYSDATE - 45
    AND a.country_cd = 'US'
    AND b.individual_id in (
    SELECT UNIQUE c.individual_id
    FROM order c
    WHERE c.prod_cd = 'A'
    AND NVL (c.last_payment_dt,
    TO_DATE ('05-MAY-1955')
    ) >= SYSDATE - 745)
    THEN 1
    ELSE 0
    END AS one_mon_c_paid_us
    FROM items b, addr a, product d
    WHERE b.prod_id = d.prod_id
    AND d.affinity_1_cd = 'ADH'
    AND b.individual_id = a.individual_id)
    GROUP BY individual_id)
    Edited by: user4522368 on Aug 23, 2010 9:11 AM

    Please try and place \ before and after you code \Could you not remove the inline column select with the following?
    SELECT a.individual_id individual_id
         ,CASE
            when b.Ship_dt is null then
              3
            WHEN b.ship_dt >= SYSDATE - 90
              3
            WHEN b.ship_dt >= SYSDATE - 45
              2
            WHEN b.ship_dt >= SYSDATE - 30
              1
          END AS one_mon_c_paid_us
    FROM  items           b
         ,addr            a
         ,product         d
         ,order           o
    WHERE b.prod_id       = d.prod_id
    AND   d.affinity_1_cd = 'ADH'
    AND   b.individual_id = a.individual_id
    AND   b.Individual_ID = o.Individual_ID
    and   o.Prod_CD       = 'A'             
    and   NVL (o.last_payment_dt,TO_DATE ('05-MAY-1955') ) >= SYSDATE - 745
    and   a.Country_CD    = 'US'

  • Help: How to write a query formula?

    Problem. There are 4 tables of a database of a store:
    Table 1: Tarifs
    Columns: Tarif_number, Price
    Primary key: Tarif_number
    Table 2: Products
    Columns: Product_number, Product_name, Quantity;
    Primary key: Product_number
    Data: Tarif 1 - 100%, Tarif 2 - 75 %, Tarif 3 - 50 %, Tarif 4 - 25 % (discounts)
    Table 3: Customers
    Columns: Customer_Number, Customer_Name, Address, Phone;
    Primary Key: Client_Number
    Table 4: Sales
    Columns: Date, Product_Number, Tarif_Number, Quantity_Sold, Customer_Name
    Primary key: Date, Product_Number, Customer_Number, Tarif_Number
    The problem is to write SQL PLUS queries, which would bring the following results:
    i. Data off all customers (name, address, phone), who within the period from 3/4/2009 to 4/4/2009 purchased products containing the string "abc" in its name. The data should be arranged alphabetically by product name. The report should have a footnote including name of its author.
    ii which product was purchased the most frequently and the total purchase value. The footnote should include the title and the date of the printout.
    iii Catalogue product number, product name, quantity on store, quantity sold for any product in the database. The product name should be introduced from keyboard in order to make possible to enter this or similar query later.

    Queries not tested*
    I) SELECT cs.customer_name, cs.address, cs.phone
    FROM customers cs, sales sl, products prd
    WHERE cs.customer_number = sl.customer_number
    AND prd.product_number = sl.product_number
    AND sl.DATE BETWEEN TO_DATE ('3/4/2009', 'DD/MM/YYYY')
    AND TO_DATE ('4/4/2009', 'DD/MM/YYYY')
    AND LOWER (prd.product_name) LIKE '%abc%'
    II) SELECT prd.product_name, SUM (sl.quantity_sold * tf.price)
    FROM sales sl, product prd, tarifs tf
    WHERE sl.product_number = prd.product_number
    AND tf.tarif_number = sl.tarif_number
    GROUP BY sl.product_number
    HAVING COUNT (1) = (SELECT MAX (COUNT (1))
    FROM sales sl1
    GROUP BY sl1.product_number)
    III) SELECT prd.product_name, prd.quantity, SUM (sl.quantity_sold)
    FROM sales sl, product prd, tarifs tf
    WHERE sl.product_number = prd.product_number
    AND tf.tarif_number = sl.tarif_number
    GROUP BY sl.product_number

  • Please Help Me to write a query

    I have a table called CLAIMs and I want to find out the rows which have the same claim and member id
    having cert ='MEMBER' and Insert date is greater than Other rows within the same claim and memberid.
    Eg:
    Insert date             claim                    member Id        cert     
    7/17/2009               2009106                  1345678         1345678 
    8/8/2009                2009106                  1345678         MEMBER 
    12/11/2009              2009107            1345679         MEMBER
    12/14/2009          2009107                   1345679         1345679
    Expected row:
    7/17/2009               2009106                  1345678         1345678 
    8/8/2009                2009106                  1345678         MEMBER  Please help.

    hoek wrote:
    Edited by: hoek on Dec 14, 2009 6:14 PMYour solution will show MEMBER even if there are no non-members for the claim, while OP stated:
    Insert date is greater than Other+* rows within the same claim and memberid
    SQL> with claims as (
      2                  select to_date('8/8/2009','mm/dd/yyyy') insert_date,2009106 claim,1345678 member_id,'MEMBER' cert from dual
      3                 )
      4  select *
      5  from   claims a
      6  where exists ( select null
      7                 from ( select b.insert_date
      8                        ,      b.claim
      9                        ,      b.member_id
    10                        ,      b.cert
    11                        ,      row_number() over (partition by b.claim, b.member_id order by b.insert_date desc) rn
    12                        from   claims b
    13                      ) b
    14                 where b.claim = a.claim
    15                 and   b.member_id = a.member_id
    16                 and   b.cert = 'MEMBER'
    17                 and   b.rn = 1
    18               );
    INSERT_DA      CLAIM  MEMBER_ID CERT
    08-AUG-09    2009106    1345678 MEMBER
    SQL> with claims as (
      2                  select to_date('8/8/2009','mm/dd/yyyy') insert_dt,2009106 claim,1345678 member_id,'MEMBER' cert from dual
      3                 )
      4  select  insert_dt,
      5          claim,
      6          member_id,
      7          cert
      8    from  (
      9           select  c.*,
    10                   max(case cert when 'MEMBER' then null else insert_dt end) over(partition by claim,member_id) max_non_member_dt,
    11                   max(case cert when 'MEMBER' then insert_dt end) over(partition by claim,member_id) max_member_dt
    12             from  claims c
    13          )
    14  where max_non_member_dt < max_member_dt
    15  /
    no rows selected
    SQL> SY.

  • Please help me re write this query

    select case
    when MONTHS>=6 then YRS++
         else YRS
    end,
    from some_tab
    Edited by: user9027633 on Feb 25, 2010 1:44 AM

    Hi,
    It sounds like you want to round the length of service to the nearest year; the rounded figure may as much as 6 months more or less than the actual length of service.
    How to do that depends on your data.
    If you have 2 separate columns, months (a number, where 0 <= months < 12) and yrs (an integer) then you could say
    select  case
                when MONTHS >= 6 then YRS+1
                                 else YRS
            end     AS rounded_service_years
    from    some_tab
    ;but you might find it easier to say:
    select  ROUND (yrs + (months / 12))     AS rounded_service_years
    from    some_tab
    ;

  • Urgent help in multiple table update query

    TABLE1
    NAME
    RATE
    TABLE2
    NAME RATE1 DATE1
    NAME RATE2 DATE2
    NAME RATE3 DATE3
    Can anyone help me to write a query which can update RATE in TABLE1 based on
    following criteria ?
    We have same column 'NAME' in both the tables.
    First Find maximum Date from the TABLE2 and then take correspoinding rate from TABLE2 and update with the RATE column in TABLE1.

    Works as expected for me:
    SQL> CREATE TABLE TABLE1 (
      2   NAME VARCHAR2(10),
      3   RATE NUMBER
      4  );
    Tabelle wurde erstellt.
    SQL> CREATE TABLE TABLE2 (
      2   NAME VARCHAR2(10),
      3   RATE NUMBER,
      4   DT DATE
      5  );
    Tabelle wurde erstellt.
    SQL> INSERT INTO TABLE1 VALUES ('NAME1', NULL);
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE1 VALUES ('NAME2', NULL);
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE1 VALUES ('NAME3', NULL);
    1 Zeile wurde erstellt.
    SQL>
    SQL> INSERT INTO TABLE2 VALUES ('NAME1', 1, SYSDATE);
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE2 VALUES ('NAME1', 2, SYSDATE-1);
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE2 VALUES ('NAME1', 3, SYSDATE-2);
    1 Zeile wurde erstellt.
    SQL>
    SQL> INSERT INTO TABLE2 VALUES ('NAME2', 1, SYSDATE+1/1440);
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE2 VALUES ('NAME2', 2, SYSDATE+2/1440);
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE2 VALUES ('NAME2', 3, SYSDATE+3/1440);
    1 Zeile wurde erstellt.
    SQL> update table1 t1 set
      2    rate = (select t2.rate
      3              from table2 t2
      4             where t2.dt  = (select max(t3.dt)
      5                                from table2 t3
      6                               where t3.name=t2.name
      7                             )
      8               and t2.name=t1.name
      9           );
    3 Zeilen wurden aktualisiert.
    SQL> select name, rate from table1;
    NAME             RATE                                                          
    NAME1               1                                                          
    NAME2               3                                                          
    NAME3                                                                          
    SQL> INSERT INTO TABLE2 VALUES ('NAME3', 1, TRUNC(SYSDATE));
    1 Zeile wurde erstellt.
    SQL> INSERT INTO TABLE2 VALUES ('NAME3', 2, TRUNC(SYSDATE));
    1 Zeile wurde erstellt.
    SQL> update table1 t1 set
      2    rate = (select t2.rate
      3              from table2 t2
      4             where t2.dt  = (select max(t3.dt)
      5                                from table2 t3
      6                               where t3.name=t2.name
      7                             )
      8               and t2.name=t1.name
      9           )
    10  ;
      rate = (select t2.rate
    FEHLER in Zeile 2:
    ORA-01427: Unterabfrage für eine Zeile liefert mehr als eine Zeile

  • Guide me to write a query

    Hi All,
    I want to retrieve some 7 fields from 3 tables
    5 from prd_common_info
    1 from vendor
    1 from srv_act
    common column :
    prd_common_info and srv_act - prd_code
    srv_act and vendor - vendor_id
    vendor and prd_common_info - LAST_MOD_USER
    condition should be PRD_CODE = 'M-DATA' and INV_STATUS_FLAG='A'
    I have written a query beloe.but the problem here all the data is getting duplicated(too many records for one entry).
    SELECT P.PRD_CODE,P.CIRCUIT_ID,P.NAME,P.EMP_ID,P.END_USER_EMAIL,V.VENDOR_NAME,P.COMMENTS
    FROM
    PRD_COMMON_INFO P,SRV_ACT S,VENDOR V
    WHERE P.PRD_CODE = 'M-DATA' and INV_STATUS_FLAG='A'
    AND P.PRD_CODE=S.PRD_CODE and S.VENDOR_ID=V.VENDOR_ID AND V.LAST_MOD_USER=P.LAST_MOD_USER
    Please help me to write the query.
    Regards,
    Saji

    Hi,
    If the problem in duplicate records only, use DISTINCT
    SELECT DISTINCT P.PRD_CODE,
                    P.CIRCUIT_ID,
                    P.NAME,
                    P.EMP_ID,
                    P.END_USER_EMAIL,
                    V.VENDOR_NAME,
                    P.COMMENTS
      FROM PRD_COMMON_INFO P, SRV_ACT S, VENDOR V
    WHERE     P.PRD_CODE = 'M-DATA'
           AND INV_STATUS_FLAG = 'A'
           AND P.PRD_CODE = S.PRD_CODE
           AND S.VENDOR_ID = V.VENDOR_ID
           AND V.LAST_MOD_USER = P.LAST_MOD_USERSaad,

  • How to write a query for this situation

    Hi ,
    Can any one help me to write a query.I have a table with three columns like char_id_1,char_id_2,char_id_3,these data have to insert into some other table but in the target table the data should be comma seperated like (10,8,2),here blank space indicates null.In source table the columns are numbers,but in target table it is varchar2.
    source table:
    CHAR_ID_1 CHAR_ID_2 CHAR_ID_3
    10 8 2
    7 1 5
    4 11
    6 1
    2 8
    6 12 7
    Target table:
    CHAR_IDS
    10,8,2
    7,1,5
    4,11
    6,1
    2,8
    6,12,7

    I started doing it to_char way, and thought of some different version, and ended up with an ugly solution which works only if you have three columns. Thought of not posting, but posting it anyway:
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP and Data Mining options
    SQL> with t as(
      2      select 10 c1,8 c2,2 c3 from dual union all
      3      select 7,1,5 from dual union all
      4      select 4,null,11 from dual union all
      5      select null,6,1 from dual union all
      6      select 2,8,null from dual union all
      7      select null,8,null from dual union all
      8      select null,null,10 from dual union all
      9      select 2,null,null from dual union all
    10      select null,null,null from dual union all
    11      select 6,12,7 from dual )
    12  --
    13  select nvl2(c1,
    14              c1 || nvl2(c2,',' || c2 || nvl2(c3,',' || c3,null),nvl2(c3,',' || c3,null)),
    15              nvl2(c2, c2 || nvl2(c3,','|| c3,null),nvl2(c3,c3,null))) col_concat
    16  from t;
    COL_CONCAT
    10,8,2
    7,1,5
    4,11
    6,1
    2,8
    8
    10
    2
    6,12,7
    10 rows selected.
    SQL>

  • SQL query....need help again

    Actor (Aname: varchar(40), Ano: varchar(6)) Ano is pk
    Movie (Mname: varchar(40),mno: varchar(8)) Mno is pk
    PlayIn (Ano, Mno, Pay: Integer) Ano and Mno are fk referencing Acotr and Movie respectively.
    Actor(Aname, Ano)
    A Bingo, A1
    B Castro, A2
    C Katie, A3
    S Hommy, A4
    J Tammy, A5
    K loren, A6
    Movie(Mname, Mno)
    Gladiator, M1
    Cast, M2
    Dog, M3
    Jilters, M4
    PlayIn(Ano,Mno,Pay)
    A1 M1 800
    A1 M2 1500
    A2 M2 78
    A2 M3 1750
    A2 M4 2301
    A3 M2 904
    A3 M3 629
    A4 M2 565
    A4 M3 5695
    A4 M4 1255
    A5 M1 989
    A5 M4 238
    A6 M2 137
    A6 M3 236
    A6 M4 545
    write a query for this QUESTION:
    for each movie, list the movie number, the average pay and the total number of actors in the movie.....

    hi - didn't you read my response to your other nearly identical thread?
    SQL query...pls help ASAP
    may I suggest you get a basic book on oracle SQL and immerse yourself for a few hours before comming to this forum for answers to your homework questions?

  • Need a help to write the query

    Hi,
    I need a small help to write the query.
    I have a table contains couponid,coupon,createdate,expirationdate,assigndate from couponday table
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.
    Thanks in advance

    Hi,
    user586 wrote:
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.If you mean expirationdate (datatype: DATE) is not within 30 days (past or future) of run time, then:
    SELECT  coupon          -- or whatever columns you want
    FROM    table_x
    WHERE   expirationdate  NOT BETWEEN  SYSDATE - 30
                                AND      SYSDATE + 30
    ;

  • Calling SQL experts to help write a query

    Trying to find some help from some sql experts.
    I was asked to help write a query showing each Dialed_Number, the associated Call_Type, and Script(s) in an ICM configuration.
    All the data I’m looking for seems to be in the following tables:
    Dialed_Number (DialedNumberString, EnterpriseName, DialedNumberID)
    Call_Type (CallTypeID, EnterpriseName)
    Dialed_Number_Map (DialedNumberID, CallTypeID)
    Call_Type_Map (CallTypeID, MasterScriptID)
    Master_Script (MasterScriptID, EnterpriseName)
    Thus far, my query looks like this:
    SELECT     distinct Dialed_Number.DialedNumberString, Dialed_Number.EnterpriseName AS DN_Name, Call_Type.EnterpriseName AS Call_Type_Name,
                          Master_Script.EnterpriseName AS ScriptName
    FROM         Call_Type_Map CROSS JOIN
                          Call_Type CROSS JOIN
                          Dialed_Number CROSS JOIN
                          Dialed_Number_Map CROSS JOIN
                          Master_Script
    However, it’s returning every possible combination (it was 10x worse before adding the word ‘distinct’)
    Ideally, I’d like the results to just show one row for each Dialed_Number (assumes 1:1 ratio of DN to CallType).
    e.g.:
    DN_Name           DialedNumberString      CallType_Name                ScriptName
    Can anyone offer any guidance or suggestion?

    Thanks Geoff,
    I'd put this out on a couple different channels, and while yours is pretty good (I did remove the RoutingClientID from the where clause), I got another version that works just as well, and also includes DNs without CallTypes, and those which may have a CallType, but no script.
    SELECT dn.DialedNumberString, dn.EnterpriseName AS Dialed_Number_Name, ct.EnterpriseName AS Call_Type_Name, ms.EnterpriseName AS Script_Name
    FROM Dialed_Number dn
    LEFT OUTER JOIN Dialed_Number_Map dnm ON dn.DialedNumberID = dnm.DialedNumberID
    LEFT OUTER JOIN Call_Type ct ON dnm.CallTypeID = ct.CallTypeID
    LEFT OUTER JOIN Call_Type_Map ctm ON ct.CallTypeID = ctm.CallTypeID
    LEFT OUTER JOIN Master_Script ms ON ctm.MasterScriptID = ms.MasterScriptID
    I believe the "joins" are supposedly more efficient, too.
    Thanks for your contribution!

  • I need a help to write the query

    Id
    Child
    Parent
    Is Sub Parent
    Is Parent
    1
    chld1
    y
    2
    chld2
    chld1
    y
    3
    chld3
    chld1
    y
    4
    chld4
    chld1
    5
    chld5
    chld2
    y
    6
    chld6
    chld2
    7
    chld7
    chld5
    8
    chld8
    Q: please check the above table, id no 1-7 are linked with parent and id no 8 is independent child, i need to write a query to display the below result based on the below scenarios
    Scenario 1: if user passes the child 1 the result should be as showed below table all 7 rows need to show.
    Scenario 2: if user passes the child in between child 1 - child 7 then also the result should be as showed below table all 7 rows.
    RESULT
    Id
    Child
    Parent
    1
    chld1
    2
    chld2
    chld1
    3
    chld3
    chld1
    4
    chld4
    chld1
    5
    chld5
    chld2
    6
    chld6
    chld2
    7
    chld7
    chld5
    Thanks in advance

    Hi,
    next time please post sample data as CREATE TABLE and INSERT statements or with clause like this:
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    Here a couple of test with chld7 and chld1
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    , find_root as
        select chld
          from mychildren
         where prnt is null
         start with chld='chld1'  --- this is your input
       connect by prior prnt=chld
    select id, chld, prnt
       from mychildren     
      start with chld=(select chld from find_root)
    connect by prior chld=prnt
            ID CHLD  PRNT
             1 chld1     
             2 chld2 chld1
             5 chld5 chld2
             7 chld7 chld5
             6 chld6 chld2
             3 chld3 chld1
             4 chld4 chld1
    Changing it to chld7 does not change the output.
    Output is now sorted according hierarchical query. If you need a different sort order you can specify what you want.
    Regards.
    Al

  • Help to write a query for cursor

    Hi
    I have a table trans_table which has records for all Credit (C) and Debit(D) entries for customers with the trans_date.
    Now there is a column adj_amt which contains 0 for all D entries. But for C entries it contains current trans_amt - Prev trans_amt. If there is no prev record with C type then it is 0. i.e. If this the first C entry for a particular account then adj_amt will contain 0.
    Below is the data.
    create table trans_table (cust_id number, tran_date date, tran_amt number, tran_type char(1), adj_amt number);
    1 01/01/2007 100     D 0
    1 01/01/2007 100     D 0
    1 01/02/2007 80     C 0
    1 05/02/2007 200     C 120
    1 08/02/2007 300     C 100
    2 01/01/2007 100     D 0
    2 05/01/2007 100     D 0
    2 08/01/2007 100     D 0
    2 09/01/2007 100     D 0
    2 01/02/2007 90     C 0
    2 05/02/2007 200     C 110
    3 01/01/2007 100     D 0
    3 05/01/2007 100     D 0
    3 08/01/2007 100     D 0
    4 01/01/2007 100     D 0
    4 05/01/2007 100     D 0
    4 06/01/2007 100     D 0
    4 08/01/2007 100     D 0
    4 10/01/2007 100     D 0
    4 01/02/2007 100     C 0                    
    Now hv to write a query whioch gives the cust_id , max(tran_amt) with D entry, max(trans_amt) wth C entry and if no C entry record then this column will be 0.
    The output wud be klike this..
    1 100 300
    2 100 200
    3 100 0
    4 100 100
    Since cust_id 3 has no C entry so 0 value in 3rd column.
    Now i wrote a query for cust_id and 2nd column, but am not able to get the 3rd column.
    Is it possible to get the above output..
    I wud be using this query in a cursor which wud fetch from 20 million transactions.
    Thanks in advance
    Piks
    (I am pasting the insert script if required)
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('01-02-2007', 'dd-mm-yyyy'), 100, 'C', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('10-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('06-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (3, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (3, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (3, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('05-02-2007', 'dd-mm-yyyy'), 200, 'C', 110);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('01-02-2007', 'dd-mm-yyyy'), 90, 'C', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('09-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('08-02-2007', 'dd-mm-yyyy'), 300, 'C', 100);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('05-02-2007', 'dd-mm-yyyy'), 200, 'C', 120);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('01-02-2007', 'dd-mm-yyyy'), 80, 'C', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);

    Hitherto an attempt:
    test@ORA92>
    test@ORA92> select * from trans_table;
       CUST_ID TRAN_DATE             TRAN_AMT T    ADJ_AMT
             4 02/01/2007 00:00:00        100 C          0
             4 01/05/2007 00:00:00        100 D          0
             4 01/01/2007 00:00:00        100 D          0
             4 01/10/2007 00:00:00        100 D          0
             4 01/08/2007 00:00:00        100 D          0
             4 01/06/2007 00:00:00        100 D          0
             3 01/08/2007 00:00:00        100 D          0
             3 01/05/2007 00:00:00        100 D          0
             3 01/01/2007 00:00:00        100 D          0
             2 02/05/2007 00:00:00        200 C        110
             2 02/01/2007 00:00:00         90 C          0
             2 01/05/2007 00:00:00        100 D          0
             2 01/01/2007 00:00:00        100 D          0
             2 01/09/2007 00:00:00        100 D          0
             2 01/08/2007 00:00:00        100 D          0
             1 02/08/2007 00:00:00        300 C        100
             1 02/05/2007 00:00:00        200 C        120
             1 02/01/2007 00:00:00         80 C          0
             1 01/01/2007 00:00:00        100 D          0
             1 01/01/2007 00:00:00        100 D          0
    20 rows selected.
    test@ORA92>
    test@ORA92> select
      2    cust_id,
      3    max(case when tran_type = 'D' then tran_amt else 0 end) as max_c,
      4    max(case when tran_type = 'C' then tran_amt else 0 end) as max_d
      5  from trans_table
      6  group by cust_id;
       CUST_ID      MAX_C      MAX_D
             1        100        300
             2        100        200
             3        100          0
             4        100        100
    4 rows selected.
    test@ORA92>
    test@ORA92>Cheers
    pratz

  • Need help th tuning query or re write the query--

    Hi,
    Need help to tune the below query or rewrite th query for reducing the execution time Please find the query and explain plan.
    QUERY
    explain plan FOR SELECT consumer_key,product_key,days_in_product,20100201 period_key FROM
    (SELECT consumer_key,
      product_key,
      days_in_product,
      row_number() over ( Partition BY consumer_key order by Days_in_product DESC) row_num
    FROM
      (SELECT consumer_key,
        product_key,
        SUM(no_ofdays) days_in_product
      FROM
        (SELECT pcv.consumer_key,
          pcv.product_key,
          pcv.product_consumer_valid_from,
          pcv.product_consumer_valid_to,
          DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from) period_start,
          DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959) period_end,
          CASE
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
            AND activation_date > to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
            THEN 0
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
            AND activation_date BETWEEN to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') AND to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
            THEN
              --to_char(activation_date,'MON-YYYY')='PERIOD_ACTIVE'  and activation_date >= to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') then
              (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_date(TO_CHAR(activation_date,'YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') )
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) < 20100201000000
            THEN (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) > 20100228235959
            THEN 0
            ELSE
              --unusual situation
              (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
          END No_ofDays
        FROM cimtran.product_consumer_validity pcv,
          consumer_dimension cd
        WHERE pcv.consumer_key           =cd.consumer_key
        AND product_consumer_valid_to   >= 20100201000000
        AND product_consumer_valid_from <= 20100228235959
          --and product_consumer_valid_from > '20090801000000'
        ORDER BY consumer_key,
          product_key,
          product_consumer_valid_from
        ) a
      GROUP BY consumer_key,
        product_key
      ORDER BY consumer_key,
        product_key
    ) WHERE row_num=1 ;EXPLAIN PLAN
    "PLAN_TABLE_OUTPUT"
    "Plan hash value: 3823907703"
    "| Id  | Operation                | Name                      | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |"
    "|   0 | SELECT STATEMENT         |                           |  4665K|   231M|       |   133K  (1)| 00:31:08 |"
    "|*  1 |  VIEW                    |                           |  4665K|   231M|       |   133K  (1)| 00:31:08 |"
    "|*  2 |   WINDOW SORT PUSHED RANK|                           |  4665K|   173M|   232M|   133K  (1)| 00:31:08 |"
    "|   3 |    VIEW                  |                           |  4665K|   173M|       |   104K  (1)| 00:24:18 |"
    "|   4 |     SORT GROUP BY        |                           |  4665K|   182M|   729M|   104K  (1)| 00:24:18 |"
    "|*  5 |      HASH JOIN           |                           |    13M|   533M|    65M| 44241   (1)| 00:10:20 |"
    "|   6 |       TABLE ACCESS FULL  | CONSUMER_DIMENSION        |  2657K|    35M|       |  4337   (1)| 00:01:01 |"
    "|*  7 |       TABLE ACCESS FULL  | PRODUCT_CONSUMER_VALIDITY |    13M|   351M|       | 15340   (2)| 00:03:35 |"
    "Predicate Information (identified by operation id):"
    "   1 - filter(""ROW_NUM""=1)"
    "   2 - filter(ROW_NUMBER() OVER ( PARTITION BY ""CONSUMER_KEY"" ORDER BY "
    "              INTERNAL_FUNCTION(""DAYS_IN_PRODUCT"") DESC )<=1)"
    "   5 - access(""PCV"".""CONSUMER_KEY""=""CD"".""CONSUMER_KEY"")"
    "   7 - filter(""PRODUCT_CONSUMER_VALID_FROM""<=20100228235959 AND "
    "              ""PRODUCT_CONSUMER_VALID_TO"">=20100201000000)"

    I doubt that this query can be tuned without using indexes. There is a lot of unnecessary work specified in your query, like unnecessary intermediate sorting and selecting unused columns. The cost based optimizer recognized it and skips some of that unnecessary work, it seems. For clarity's sake, I would rewrite your query like below. Note that the query is untested:
    select consumer_key
         , max(product_key) keep (dense_rank last order by days_in_product) product_key
         , max(days_in_product) days_in_product
         , 20100201 period_key
      from ( select pcv.consumer_key
                  , pcv.product_key
                  , sum
                    ( case
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) between 20100201000000 and 20100228235959
                      then
                        case
                        when cd.activation_date > to_date(pcv.product_consumer_valid_to,'yyyymmddhh24miss')
                        then
                          0
                        when cd.activation_date between to_date(pcv.product_consumer_valid_from,'yyyymmddhh24miss') and to_date(product_consumer_valid_to,'yyyymmddhh24miss')
                        then
                          to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
                          - to_date(to_char(activation_date,'yyyymmddhh24miss'),'yyyymmddhh24miss')
                        end
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) < 20100201000000
                      then
                        to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
                        - to_date(to_char(pcv.product_consumer_valid_from),'yyyymmddhh24miss'))
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) > 20100228235959
                      then
                        0
                      end
                    ) days_in_product
               from cimtran.product_consumer_validity pcv
                  , consumer_dimension cd
              where pcv.consumer_key             = cd.consumer_key
                and product_consumer_valid_to   >= 20100201000000
                and product_consumer_valid_from <= 20100228235959
              group by consumer_key
                  , product_key
    group by consumer_keyRegards,
    Rob.

  • How to write named query if we want to use IN syntax in our sql statement?

    I cannot find a suitable category about named query, so please move to appropriate place if there is any.
    When we write named query, below statement is fine.
    Query q2 = em.createQuery("SELECT o FROM Table1 as o WHERE field1 = :input1");             q2.setParameter("input1", "value1");
    Now, my question is, how can I write this type of query when we want to use the IN sql syntax? As below statement CANNOT return correct results. Even I tried to put a pair of single quote [ ':input2' ], it won't help also.
    Query q2 = em.createQuery("SELECT o FROM Table1 as o WHERE field2 IN (:input2)");             q2.setParameter("input1", "3633, 3644");
    Can anyone suggest? Thanks.

    roamer wrote:
    Now, my question is, how can I write this type of query when we want to use the IN sql syntax? As below statement CANNOT return correct results. Even I tried to put a pair of single quote [ ':input2' ], it won't help also.
    Query q2 = em.createQuery("SELECT o FROM Table1 as o WHERE field2 IN (:input2)");
    q2.setParameter("input1", "3633, 3644");
    Can anyone suggest?The above is in your code right? Not in some configuration file?
    Then you do it the same way as with regular jdbc/sql.
    1. You start with a collection of values - call it collection A.
    1. Create a for loop that dynamically creates the string using 'bind' variables (whatever you want to call the 'colon' entity in the above).
    2. Call the createQuery method using the string that was created
    3. Create a second loop that iterates over A and populates with setParameter.
    Pseudo code
            Object[] A = ...
            String sql = "SELECT o FROM Table1 as o WHERE field2 IN (";
            for (int i=1; i <= A.length; i++)
                  if (i == 1)
                     sql += ":input" + i;
                 else
                     sql += ",:input" + i;
            sql += ")";
            Query q2 = em.createQuery(sql);
            for (int i=1; i <= A.length; i++
                  q2.setParameter("input" + i, A[i-1]);
                  }By the way there is a jdbc forum.

Maybe you are looking for

  • ATI Radeeon X800XT Mac Edition - Upgrade experience

    Though it's very expensive, for almost HK$6000, and it took me a month to get it, I was happy to be able to at last do the upgrade from the GeForce FX5200 coming along with my G5 when purchased. But the happiness faded very quickly when I was using i

  • Error PI 7.31 RFC-SOAP Certificate Rejected

    Hi Experts, I'm facing an error last days. The scenario is, an interface was working fine in DEV, but in QAS stopped. DEV and QAS has the same configuration, same endpoint, user, etc.... In QAS the error in PI 7.31 was: com.sap.engine.interfaces.mess

  • JMaskField mask input : delete and backspace do not work

    I am using this package for formatted input. Delete and backspace do not work if the field is created with a default text as in the code snippet I attach. Did somebody managed this to work? How? delete and backspace work badly in everycase anyway. be

  • The Playhead Function in the iOS7 Music Player

    I am having troubles with the playhead function with the new music app for the ios7. If I am playing a track and I want to use the playhead function whilst playing a track it at times lags and often bumps to different part of the track.  The playhead

  • How to verify adhoc for tester

    I can verify that an ad hoc is signed correctly for me by: - deleting existing profile/app from iPhone - dropping provisioning file into iTunes with .app - syncing phone to iTunes However, my tester may still get a verification failure, even though h