Need output of below query

WITH rcv_transactions
     AS (SELECT 1001      TRANSACTIONS_ID,
                -1        PARENT_TRANSACTION_ID,
                'RECEIVE' TRANSACTION_TYPE,
                NULL      LPN_ID,
                'LPN01'   TRANSFER_LPN_ID
         FROM   dual
         UNION ALL
         SELECT 1002,
                1001,
                'DELIVER',
                'LPN01',
                'LPN01'
         FROM   dual
         UNION ALL
         SELECT 1003,
                -1,
                'RECEIVE',
                NULL,
                'LPN01'
         FROM   dual)
SELECT *
FROM   rcv_transactions;
My output for the above query is
transactions_id   parent_transaction_id   transaction_type     lpn_id     transfer_lpn_id
1001                        -1                          receive                                 lpn01
1002                    1001                           deliver                  lpn01      lpn01
1003                         -1                         receive                                lpn01
I want the output as below:
TRANSACTIONS_ID,   PARENT_TRANSACTION_ID,  TRANSACTION_TYPE,     LPN_ID,     TRANSFER_LPN_ID,
1003                                    -1                                   RECEIVE                                   LPN01
i.e., I want the transactions id which are only 'receive' and not 'deliver' for the particular transfer_lpn_id
in the above scenario 1001 is received and delivered in 1002.
But 1003 is only received. so  I need the output of 1003

WITH rcv_transactions AS (SELECT 1001 TRANSACTIONS_ID,
                                 -1 PARENT_TRANSACTION_ID,
                                 'RECEIVE' TRANSACTION_TYPE,
                                 NULL LPN_ID,
                                 'LPN01' TRANSFER_LPN_ID
                            FROM DUAL
                          UNION ALL
                          SELECT 1002,
                                 1001,
                                 'DELIVER',
                                 'LPN01',
                                 'LPN01'
                            FROM DUAL
                          UNION ALL
                          SELECT 1003,
                                 -1,
                                 'RECEIVE',
                                 NULL,
                                 'LPN01'
                            FROM DUAL)
SELECT *
  FROM rcv_transactions rcv_out
WHERE rcv_out.transaction_type = 'RECEIVE'
       AND NOT EXISTS
              (SELECT 1
                 FROM rcv_transactions
                WHERE parent_transaction_id = rcv_out.TRANSACTIONS_ID)

Similar Messages

  • Need Help on below Query.

    Hi All,
    Need Help on below Query.
    Consider,
    "test9" Table Data in COLUMN "Name" AS
    Name
    =====
    'a'
    'b'
    'c'
    'd'
    'e'
    I am writing a query as :
    SELECT * FROM test9 WHERE Name IN ('a','b','c','d','e','f','g')
    I want result set as , It should show data as -
    'f'
    'g'
    i.e. data which does not exists in the table and which is give in in clause
    Is it possible in a single query.

    You can put the data that is to be checked for into a table instead or an inline view, for example:
    with t as
    (select 'a' as c1 from dual
    union all
    select 'b' from dual
    union all
    select 'c' from dual
    union all
    select 'd' from dual
    union all
    select 'e' from dual)
    select c1 from (select 'a' as c1 from dual
    union all
    select 'b' from dual
    union all
    select 'c' from dual
    union all
    select 'd' from dual
    union all
    select 'e' from dual
    union all
    select 'f' from dual
    union all
    select 'g' from dual)
    minus
    select c1 from t
    C
    f
    g
    2 rows selected.

  • Need output like below mentioned format

    Hi,
    By using below query , iam getting output in the below formate as metioned
    select
    m.year "Year",
    --decode(m.year,like '200%','a') "DG",
    --decode(instr(m.year,'200'),0,Null,'Shallow Water <1000 Feet') "DG",
    --decode(substr('Shallow Water <1000 Feet',1,1),'S','Shallow Water <1000 Feet') "DG1",
    'Shallow Water <1000 Feet' as "Depth Group",
    'Deep Water 1000-5000 Feet' as "Depth Group",
    'Ultra Deep Water > 5000 Feet' as "Depth Group",
    sum(decode(M.FLUID,'O',nvl(M.YEAR_TO_DATE,0)))  "Ytd Oil" ,
    from pden_monthly_prod m,pi_pden_well w,pden p
    ---           Make sure all necessary Table joins are done
    where P.ENTITY=M.ENTITY
    and p.entity = w.entity
    ----        define limitations by column values
    and m.year between 2000 and 2012
    and ((p.entity_type in ('LEASE','WELL') and p.province_state not in ('17','42') and p.pi_situation_cd <> 'F')
    or (p.entity_type = 'ALLOCATED'      and p.province_state in ('17','42')     and p.pi_situation_cd <> 'F')
    or (p.entity_type = 'WELL'              and p.province_state = '42'             and p.pi_situation_cd <> 'F')
    or(p.pi_situation_cd = 'F'))
    and m.fluid in ('O','CN','G','CG','OW','GW','LW','FW') 
    and  nvl(w.water_depth,'0') <1000
    --or w.water_depth between 1000 and 5000
    --or w.water_depth >5000
    --for Federal state only (FOS)
    and P.PI_SITUATION_CD IN ('F','S')
    and p.province_state in ('17','42','60','01')
    group by m.year
                ,'Shallow Water <1000 Feet'
              ,'Deep Water 1000-5000 Feet'
               ,'Ultra Deep Water > 5000 Feet'
    order by m.year
    i am getting output is
    Year    Depth Group       Depth Group      Depth Group Ytd Oil
    2000           a                          b                           c               200
    2001           a                          b                           c               150
    2002           a                          b                           c               110
    2003           a                          b                           c               100
    but i need output like the below format
    Year          Depth Group       Ytd Oil
    2000              a                      200
    2000              b                      200
    2000              c                       200
    2000Total                              600
    2001             a                      150
    2001             b                      150
    2001             c                       150
    2001Total                               450
    2002             a                      110
    2002             b                      110
    2002             c                      110
    2002Total                           330
    2003             a                      100
    2003             b                      100
    2003             c                       100
    2003Total                             300
    could you please help on this ?

    UNPIVOT is equal to:
    with t1 as (select 'a' f1, 1 f2, 2 f3, 3 f4 from dual union all
                select 'b', 4, 5, 6 from dual union all
                select 'c', 7, 8, 9 from dual union all
                select 'd', 10, 11, 12 from dual)
    select a.f1, b.f,
           case b.f
             when 'F2' then a.f2
             when 'F3' then a.f3
             when 'F4' then a.f4
           end res
    from t1 a, (select 'F2' f from dual union all
                select 'F3' f from dual union all
                select 'F4' f from dual) b
    order by 1, 2

  • Need output as below

    WITH T1
         AS (SELECT 7532 pkey,2 events,TO_DATE ('12/11/2012', 'mm/dd/yyyy') dt,'Dept' code FROM DUAL
             UNION ALL
             SELECT 8101,3,TO_DATE ('12/9/2013', 'mm/dd/yyyy'),'Emp' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL)
    SELECT pkey, events, dt, code
      FROM t1;
    Output:
    pkey  events  dt            code
    7532    2    12/11/2012     Dept
    8101    3    12/9/2013      Emp
    8102    4    12/16/2013     Dept
    value column should be derived as below:_
    Starting_letter_of_code+year_of_dt+week_of_year_in_dt+loop_sequence_of_event
    for pkey 7532 value is derived as below
    Starting letter of code is D
    year of dt is 12
    week of dt in year is 50
    no of events are 2
    so I should get two records for pkey 7532
    i.e., D125001 and D125002
    Expected output:
    pkey    events    dt        code    value
    7532       2    12/11/2012  Dept     D125001
    7532       2    12/11/2012  Dept     D125002
    8101       3    12/9/2013   Emp      E134901
    8101       3    12/9/2013   Emp      E134902
    8101       3    12/9/2013   Emp      E134903
    8102       4    12/16/2013  Dept     D135001
    8102       4    12/16/2013  Dept     D135002
    8102       4    12/16/2013  Dept     D135003
    8102       4    12/16/2013  Dept     D135004
    am currently getting output as
    present output:                           
    pkey  events  dt            code     value
    7532    2    12/11/2012     Dept     D1250
    8101    3    12/9/2013      Emp      E1349
    8102    4    12/16/2013     Dept     D1350
    Query Used:
    WITH T1
         AS (SELECT 7532 pkey,2 events,TO_DATE ('12/11/2012', 'mm/dd/yyyy') dt,'Dept' code FROM DUAL
             UNION ALL
             SELECT 8101,3,TO_DATE ('12/9/2013', 'mm/dd/yyyy'),'Emp' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL)
    SELECT pkey, events, dt, code,
           NVL (REGEXP_SUBSTR (code,'(D)(ept)',1,1,'i',1),
                REGEXP_SUBSTR (code,'(E)(mp)',1,1,'i',1)
           || TO_CHAR (dt, 'YY')
           || TO_CHAR (dt, 'WW')
              val
      FROM t1;Edited by: NSK2KSN on Dec 21, 2012 12:05 PM

    Used Analytic Function 'ROW_NUMBER()' to get the consecutive row numbers inside a group.
    please check this...
    WITH T1
         AS (SELECT 7532 pkey,2 events,TO_DATE ('12/11/2012', 'mm/dd/yyyy') dt,'Dept' code FROM DUAL
             UNION ALL
             SELECT 8101,3,TO_DATE ('12/9/2013', 'mm/dd/yyyy'),'Emp' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL
             UNION ALL
             SELECT 8101,3,TO_DATE ('12/9/2013', 'mm/dd/yyyy'),'Emp' FROM DUAL
             UNION ALL
             SELECT 8101,3,TO_DATE ('12/9/2013', 'mm/dd/yyyy'),'Emp' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL
             UNION ALL
             SELECT 8102,4,TO_DATE ('12/16/2013', 'mm/dd/yyyy'),'Dept' FROM DUAL)
    SELECT pkey, events, dt, code,
                  substr(code,1,1)||
                  to_char(dt,'yy')||
                  to_char(dt,'ww')||
                  lpad(row_number() over (partition by pkey order by pkey),2,'0')
      FROM t1;gives7532     2     12-11-2012     Dept     D125001
    8101     3     12-9-2013     Emp     E134901
    8101     3     12-9-2013     Emp     E134902
    8101     3     12-9-2013     Emp     E134903
    8102     4     12-16-2013     Dept     D135001
    8102     4     12-16-2013     Dept     D135002
    8102     4     12-16-2013     Dept     D135003
    8102     4     12-16-2013     Dept     D135004
    8102     4     12-16-2013     Dept     D135005

  • Need output of a query in given xml format.

    I have a table as
    create table t_cases (case_id number, pros_seq number, case_lname varchar2(100),
    day_phone number, night_phone number, intl_phone number);
    where case_id and pros_seq is the compound primary key.
    The insert script to insert the data in the table is
    insert into t_cases values(1, 1, 'test', 12, 23, 34);
    insert into t_cases values(1, 2, 'test', 56, 67, 78);
    commit;
    Now from this table I need to query and fetch the data in the following xml format.
    <case REPEATINGTYPE="PageList">
         <rowdata REPEATINGINDEX="1">
              <caseid>1</caseid>
              <prosseq>1</prosseq>
              <PhoneNumbers REPEATINGTYPE="PageList">
              <rowdata REPEATINGINDEX="1">
                   <CountryCode>12</CountryCode>
                   <Type>Day</Type>
              </rowdata>
              <rowdata REPEATINGINDEX="2">
                   <CountryCode>23</CountryCode>
                   <Type>Night</Type>
              </rowdata>
              <rowdata REPEATINGINDEX="3">
                   <CountryCode>34</CountryCode>
                   <Type>International</Type>
              </rowdata>
              </PhoneNumbers>
         </rowdata>
         <rowdata REPEATINGINDEX="2">
              <caseid>1</caseid>
              <prosseq>2</prosseq>
              <PhoneNumbers REPEATINGTYPE="PageList">
              <rowdata REPEATINGINDEX="1">
                   <CountryCode>56</CountryCode>
                   <Type>Day</Type>
              </rowdata>
              <rowdata REPEATINGINDEX="2">
                   <CountryCode>67</CountryCode>
                   <Type>Night</Type>
              </rowdata>
              <rowdata REPEATINGINDEX="3">
                   <CountryCode>78</CountryCode>
                   <Type>International</Type>
              </rowdata>
              </PhoneNumbers>
         </rowdata>
    </case>
    Please let me know how to do this.

    Like this...
    select XMLELEMENT( "case", XMLATTRIBUTES ('PageList' as "RepeatingType")
                     ,xmlagg(
                              xmlelement("rowdata", xmlattributes(pros_seq as "RepeatingIndex")
                                        ,xmlelement("caseid", case_id)
                                        ,xmlelement("prosseq", pros_seq)
                                        ,xmlelement("PhoneNumbers", xmlattributes('PageList' as "RepeatingType")
                                                   ,xmlelement("rowdata", xmlattributes(1 as "RepeatingIndex")
                                                              ,xmlelement("CountryCode", day_phone)
                                                              ,xmlelement("Type", 'Day')
                                                   ,xmlelement("rowdata", xmlattributes(2 as "RepeatingIndex")
                                                              ,xmlelement("CountryCode", night_phone)
                                                              ,xmlelement("Type", 'Night')
                                                   ,xmlelement("rowdata", xmlattributes(3 as "RepeatingIndex")
                                                              ,xmlelement("CountryCode", intl_phone)
                                                              ,xmlelement("Type", 'International')
            as x
    from t_cases

  • Run BW query from R/3, need output file saved on application server

    Hello all,
    We are currently working on BI 7.0. Is there a way where we can run a BW query from R/3 by some program or tcode? We need to run the BW query and use the output of that query as an input to some other custom program in R/3.
    If we can save the output file on application server than the R/3 program will pick up that file from there.
    Is there any standard delivered functionality that will allow us to do that or how can we achieve this.
    Can some help help with some suggestions or links?
    Thanks in advance.

    Hi,
       Refer the following threads:
    [Calling BW Query from R/3;
    [Saving Bex Report / query in BW App server;
    Regards.

  • How to execute the output of the below query automatically

    Hi All,
    I want to execute the output of the below query automatically, instead of manually copying it and execute.
    select 'alter database ['+name+'] set recovery simple' from master.sys.databases where database_id > 4 and state_desc = 'online'
    Please provide me a script to do this.
    ThanK
    Kate

    EXEC sp_MSforeachdb N'ALTER DATABASE [?] SET recovery simple';--- This will set the recovery model for all the system database.The query provided by Vikash16, meets my requirement. Thank you.

  • Query needed for the below requirement

    Hi,
    I have three tables structures like below.
    tableA - LINE_ID, VENDOR_ID,DEAL_SITE_ID
    tableB - LINE_ID, DEAL_SITE_ID
    tableC - DEAL_ID,VERSION, DEAL_SITE_ID
    Now the requirement is I have to fetch all distinct VENDOR_ID from tableA with some given DEAL_ID (100001) and VERSION (1), only Problem is in tableA the rows which have LINE_ID will not have DEAL_SITE_ID and vice versa. I've used the below query for this purpose but wanted to know if there is any better solution.
    select distinct VENDOR_ID from tableA where deal_site_id in
    (select deal_site_id from tableC where deal_id = 100001 and snapshot_ver = 1)
    UNION
    select distinct VENDOR_ID from tableA where line_id in
    (select line_id from tableB where deal_site_id in
    (select deal_site_id from tableC where deal_id = 100001 and snapshot_ver = 1))
    Regards,
    Subhadeep
    Edited by: sumajumd on Jul 17, 2012 2:33 AM

    Try regular joins (assuming column version should be snapshot_ver).
    Like this (untested):
    select tablea.vendor_id
    from   tablea
      inner join tablec on (tablea.deal_site_id = tablec.deal_site_id)
    where  tablec.deal_id      = 100001
    and    tablec.snapshot_ver = 1
    union
    select tablea.vendor_id
    from   tablea
      inner join tableb on (tableb.line_id      = tablea.line_id)
      inner join tablec on (tableb.deal_site_id = tablec.deal_site_id)
    where  tablec.deal_id      = 100001
    and    tablec.snapshot_ver = 1

  • Need help in MDX query

    Hi All 
    I am new to MDX language and need a MDX functions/query on the cube to get the required output, Given below is the scenario with the data. 
    I am maintaining the data in a table in dataMart with given structure. We have the data at day and weekly in a single table with granularity indicator and count is the measure group column. While loading the data in to mart table we are populaiting the week
    Key from week table and Month key from month table and joining in the cube.
    we need to calculate the inventory for a particular month. If a user selects a particular month the output would be count = 30 as  a measure called Closed and count = 16 as a measure value called Open.
    Need a MDX query to get output.
    Granularity  Count WeekKey MonthKey
    Weekly 16
    W1 M1
    Weekly 17
    W1 M1
    Weekly 18
    w1 M1
    Weekly 19
    W1 M1
    Weekly 20
    W1 M1
    Weekly 21
    W1 M1
    Weekly 22
    W1 M1
    Weekly 23
    w2 M1
    Weekly 24
    w2 M1
    Weekly 25
    w2 M1
    Weekly 26
    w2 M1
    Weekly 27
    w2 M1
    Weekly 28
    w2 M1
    Weekly 29
    w2 M1
    Weekly 30
    w2 M1
    Weekly 16
    w3 M1
    Weekly 17
    w3 M1
    Weekly 18
    w3 M1
    Weekly 19
    w3 M1
    Weekly 20
    w3 M1
    Weekly 21
    w3 M1
    Weekly 22
    w3 M1
    Weekly 23
    w4 M1
    Weekly 24
    w4 M1
    Weekly 25
    w4 M1
    Weekly 26
    w4 M1
    Weekly 27
    w4 M1
    Weekly 28
    w4 M1
    Weekly 29
    w4 M1
    Weekly 30
    w4 M1
    Thanks in advance

    Hi Venkatesh,
    According to your description, you need to count the members with conditions in a particular month, right?
    In MDX, we can achieve the requirement by using Count and Filter function, I have tested it on AdventureWorks cube, the sample query below is for you reference.
    with member [ConditionalCount]
    as
    count(filter([Date].[Calendar].[Month].&[2008]&[2].children,[Measures].[Internet Order Count]>50))
    select {[Measures].[Internet Order Count],[ConditionalCount]} on 0,
    [Date].[Calendar].[Date].members on 1
    from
    (select [Date].[Calendar].[Month].&[2008]&[2] on 0 from
    [Adventure Works]
    Reference
    http://msdn.microsoft.com/en-us/library/ms144823.aspx
    http://msdn.microsoft.com/en-us/library/ms146037.aspx
    If this is not what you want, please elaborate your requirement, such as the detail structure of your cube, so that we can make further analysis.
    Regards,
    Charlie Liao
    TechNet Community Support

  • I need to pass a query in form of string to DBMS_XMLQUERY.GETXML package...the parameters to the query are date and varchar ..please help me..

    I need to pass a query in form of string to DBMS_XMLQUERY.GETXML package...the parameters to the query are date and varchar ..please help me build the string .Below is the query and the out put. ( the string is building fine except the parameters are with out quotes)
    here is the procedure
    create or replace
    procedure temp(
        P_MTR_ID VARCHAR2,
        P_FROM_DATE    IN DATE ,
        P_THROUGH_DATE IN DATE ) AS
        L_XML CLOB;
        l_query VARCHAR2(2000);
    BEGIN
    l_query:=  'SELECT
        a.s_datetime DATETIME,
        a.downdate Ending_date,
        a.downtime Ending_time,
        TO_CHAR(ROUND(a.downusage,3),''9999999.000'') kWh_Usage,
        TO_CHAR(ROUND(a.downcost,2),''$9,999,999.00'') kWh_cost,
        TO_CHAR(ROUND(B.DOWNUSAGE,3),''9999999.000'') KVARH
      FROM
        (SELECT s_datetime + .000011574 s_datetime,
          TO_CHAR(S_DATETIME ,''mm/dd/yyyy'') DOWNDATE,
          DECODE(TO_CHAR(s_datetime+.000011574 ,''hh24:'
          ||'mi''), ''00:'
          ||'00'',''24:'
          ||'00'', TO_CHAR(s_datetime+.000011574,''hh24:'
          ||'mi'')) downtime,
          s_usage downusage,
          s_cost downcost
        FROM summary_qtrhour
        WHERE s_mtrid = '
        ||P_MTR_ID||
       ' AND s_mtrch   = ''1''
        AND s_datetime BETWEEN TO_DATE('
        ||P_FROM_DATE||
        ',''DD-MON-YY'') AND (TO_DATE('
        ||P_THROUGH_DATE||
        ',''DD-MON-YY'') + 1)
        ) a,
        (SELECT s_datetime + .000011574 s_datetime,
          s_usage downusage
        FROM summary_qtrhour
        WHERE s_mtrid = '
        ||P_MTR_ID||
        ' AND s_mtrch   = ''2''
        AND s_datetime BETWEEN TO_DATE('
        ||P_FROM_DATE||
        ',''DD-MON-YY'') AND (TO_DATE('
        ||P_THROUGH_DATE||
        ','' DD-MON-YY'') + 1)
        ) B
      where a.S_DATETIME = B.S_DATETIME(+)';
    SELECT DBMS_XMLQUERY.GETXML('L_QUERY') INTO L_XML   FROM DUAL;
    INSERT INTO NK VALUES (L_XML);
    DBMS_OUTPUT.PUT_LINE('L_QUERY IS :'||L_QUERY);
    END;
    OUTPUT parameters are in bold (the issue is they are coming without single quotes otherwise th equery is fine
    L_QUERY IS :SELECT
        a.s_datetime DATETIME,
        a.downdate Ending_date,
        a.downtime Ending_time,
        TO_CHAR(ROUND(a.downusage,3),'9999999.000') kWh_Usage,
        TO_CHAR(ROUND(a.downcost,2),'$9,999,999.00') kWh_cost,
        TO_CHAR(ROUND(B.DOWNUSAGE,3),'9999999.000') KVARH
      FROM
        (SELECT s_datetime + .000011574 s_datetime,
          TO_CHAR(S_DATETIME ,'mm/dd/yyyy') DOWNDATE,
          DECODE(TO_CHAR(s_datetime+.000011574 ,'hh24:mi'), '00:00','24:00', TO_CHAR(s_datetime+.000011574,'hh24:mi')) downtime,
          s_usage downusage,
          s_cost downcost
        FROM summary_qtrhour
        WHERE s_mtrid = N3165 AND s_mtrch   = '1'
        AND s_datetime BETWEEN TO_DATE(01-JAN-13,'DD-MON-YY') AND (TO_DATE(31-JAN-13,'DD-MON-YY') + 1)
        ) a,
        (SELECT s_datetime + .000011574 s_datetime,
          s_usage downusage
        FROM summary_qtrhour
        WHERE s_mtrid = N3165 AND s_mtrch   = '2'
        AND s_datetime BETWEEN TO_DATE(01-JAN-13,'DD-MON-YY') AND (TO_DATE(31-JAN-13,' DD-MON-YY') + 1)
        ) B
      where a.S_DATETIME = B.S_DATETIME(+)

    The correct way to handle this is to use bind variables.
    And use DBMS_XMLGEN instead of DBMS_XMLQUERY :
    create or replace procedure temp (
      p_mtr_id       in varchar2
    , p_from_date    in date
    , p_through_date in date
    is
      l_xml   CLOB;
      l_query VARCHAR2(2000);
      l_ctx   dbms_xmlgen.ctxHandle;
    begin
      l_query:=  'SELECT
        a.s_datetime DATETIME,
        a.downdate Ending_date,
        a.downtime Ending_time,
        TO_CHAR(ROUND(a.downusage,3),''9999999.000'') kWh_Usage,
        TO_CHAR(ROUND(a.downcost,2),''$9,999,999.00'') kWh_cost,
        TO_CHAR(ROUND(B.DOWNUSAGE,3),''9999999.000'') KVARH
      FROM
        (SELECT s_datetime + .000011574 s_datetime,
          TO_CHAR(S_DATETIME ,''mm/dd/yyyy'') DOWNDATE,
          DECODE(TO_CHAR(s_datetime+.000011574 ,''hh24:'
          ||'mi''), ''00:'
          ||'00'',''24:'
          ||'00'', TO_CHAR(s_datetime+.000011574,''hh24:'
          ||'mi'')) downtime,
          s_usage downusage,
          s_cost downcost
        FROM summary_qtrhour
        WHERE s_mtrid = :P_MTR_ID
        AND s_mtrch   = ''1''
        AND s_datetime BETWEEN TO_DATE(:P_FROM_DATE,''DD-MON-YY'')
                           AND (TO_DATE(:P_THROUGH_DATE,''DD-MON-YY'') + 1)
        ) a,
        (SELECT s_datetime + .000011574 s_datetime,
          s_usage downusage
        FROM summary_qtrhour
        WHERE s_mtrid = :P_MTR_ID
        AND s_mtrch   = ''2''
        AND s_datetime BETWEEN TO_DATE(:P_FROM_DATE,''DD-MON-YY'')
                           AND (TO_DATE(:P_THROUGH_DATE,'' DD-MON-YY'') + 1)
        ) B
      where a.S_DATETIME = B.S_DATETIME(+)';
      l_ctx := dbms_xmlgen.newContext(l_query);
      dbms_xmlgen.setBindValue(l_ctx, 'P_MTR_ID', p_mtr_id);
      dbms_xmlgen.setBindValue(l_ctx, 'P_FROM_DATE', to_char(p_from_date, 'DD-MON-YY'));
      dbms_xmlgen.setBindValue(l_ctx, 'P_THROUGH_DATE', to_char(p_through_date, 'DD-MON-YY'));
      l_xml := dbms_xmlgen.getXML(l_ctx);
      dbms_xmlgen.closeContext(l_ctx);
      insert into nk values (l_xml);
    end;

  • Need suggestion on a query

    with t1 as
    select 1406 cid, 21 nid, 121 loc, 'Page' attrib from dual
    union all
    select 1406, 21, 122, 'Page' from dual
    union all
    select 1406, 21, 123, 'Page' from dual
    union all
    select 1406, 21, 124, 'Page' from dual
    union all
    select 1406, 21, 125, 'Page' from dual
    union all
    select 1406, 24, 121, 'Page' from dual
    union all
    select 1406, 24, 121, 'Sizzler' from dual
    union all
    select 1407 cid, 25 nid, 121 loc, 'Page' attrib from dual
    union all
    select 1407, 25, 122, 'Page' from dual
    union all
    select 1407, 25, 123, 'Page' from dual
    union all
    select 1407, 25, 124, 'Page' from dual
    union all
    select 1407, 25, 125, 'Page' from dual
    union all
    select 1407, 20, 121, 'Page' from dual
    union all
    select 1407, 20, 121, 'Sizzler' from dual
    Select * from t1;
    Expected output :
    1406     125     21     Page     1
    1406     124     21     Page     1
    1406     123     21     Page     1
    1406     122     21     Page     1
    1406     121     24     Page     1
    1406     121     24     Sizzler 1
    1407     121     20     Page     1
    1407     121     20     Sizzler     1
    1407     122     25     Page     1
    1407     123     25     Page     1
    1407     125     25     Page     1
    1407     124     25     Page     1
    I have written below query
    For this quuery when order by is asc, am getting correct data set for 1407 and for 1406 expected result set is not comming
    select cid,loc, nid,  attrib, dense_rank() over (partition by cid, loc order by nid) from t1 order by cid, nid, attrib;
    For this quuery when order by is asc, am getting correct data set for 1406 and for 1407 expected result set is not comming
    select cid,loc, nid,  attrib, dense_rank() over (partition by cid, loc order by nid desc) from t1 order by cid, nid, attrib;
    Requirement
    For the same cid, loc and attrib I want to show the record which is having different nodekey id
    for example, for the same 1406, 121, Page I have two records with different nid 21 and 24 (as there are more number of rows with 21, I need in the output of a record with 24 instead of 21 and remaining should be same.
    adv thx for your help
    version : 11gr2

    Like this?
    SQL> with ranked as
      2  (
      3    select cid,loc,nid,attrib,
      4           row_number()
      5            over(partition by cid,loc,attrib
      6             order by cnt ) rn
      7    from
      8    (
      9      select cid,nid,loc,attrib,
    10             count(*) over(partition by nid) cnt
    11      from t1
    12    )
    13  )
    14  select cid,loc,nid,attrib,rn
    15  from ranked
    16  where rn= 1
    17  order by cid,nid,loc desc;
           CID        LOC        NID ATTRIB          RN
          1406        125         21 Page             1
          1406        124         21 Page             1
          1406        123         21 Page             1
          1406        122         21 Page             1
          1406        121         24 Sizzler          1
          1406        121         24 Page             1
          1407        121         20 Page             1
          1407        121         20 Sizzler          1
          1407        125         25 Page             1
          1407        124         25 Page             1
          1407        123         25 Page             1
          1407        122         25 Page             1
    12 rows selected.Edited by: jeneesh on Mar 14, 2013 1:16 PM
    ORDER BY added

  • How i can add document nu in below query

    i want to add one field in below query
    field is bkpf-xblnr
    i want document number after d_text.
    here d_text = 'Payment for'
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'D_TEXT'.
        tab_out-value = d_text.
        MODIFY tab_out INDEX sy-tabix.
    please help
    thanks in advanced.

    hi help yar i m totaly new for this
    i m pasting whole code here just see yar and where u changing please hight light that things
    thanks yar
    PROGRAM zfisp0001.
    *& Subroutine pool   ZFISP0001                                         *
    *& Author: Rakesh Rao on 01.04.05                                      *
    *& Modified by: Rakesh Rao on 15.07.2005                               *
    *& Description: This subroutine pool is called from Check and Payment  *
    *&              Advice SAPScripts                                      *
    TABLES: itcsy.
    TYPES: BEGIN OF t_itcsy.
            INCLUDE STRUCTURE itcsy.
    TYPES: END OF t_itcsy.
    DATA: tab_in  TYPE STANDARD TABLE OF t_itcsy WITH NON-UNIQUE
           DEFAULT KEY INITIAL SIZE 0.
    DATA: tab_out TYPE STANDARD TABLE OF t_itcsy WITH NON-UNIQUE
           DEFAULT KEY INITIAL SIZE 0.
    DATA: d_count(3) VALUE 0,
          d_netamt_tot TYPE p DECIMALS 2,
          d_tdsamt_tot TYPE p DECIMALS 2,
          d_grossamt_tot TYPE p DECIMALS 2,
          d_tdsamt TYPE p DECIMALS 2.
    DATA flag.
    DATA: bschl LIKE bseg-bschl.
    DATA: shkzg LIKE bseg-shkzg.
    clear: d_count,d_netamt_tot.
    *&      Form  ZPAYMENT
          text
    -->  p1        text
    <--  p2        text
    FORM zpayment TABLES tab_in STRUCTURE itcsy
                         tab_out STRUCTURE itcsy.
      tables : bsak.
      TYPES: imis_type_c20(20) TYPE c,
             imis_type_c10(10) TYPE c.
      DATA: l_intern TYPE imis_type_c20.
      DATA: e_intern TYPE imis_type_c10.
      DATA: i_extern TYPE imis_type_c10.
      DATA: d_belnr(10),
           D_VBLNR(10),
            d_bukrs TYPE bseg-bukrs,
            d_gjahr TYPE bseg-gjahr,
            d_gjahr1 TYPE bseg-gjahr, " 15.07.2005 Rakesh
            d_buzei TYPE bseg-buzei,
            d_augbl TYPE bseg-augbl,
            d_umskz TYPE bseg-umskz,
            d_bschl TYPE bseg-bschl,
            d_bktxt TYPE bkpf-bktxt,
            d_budat(10),
            d_rebzg TYPE bseg-rebzg,
            D_SHKZG TYPE REGUP-SHKZG,
            d_swnes TYPE regud-swnes,
            d_netamt TYPE p DECIMALS 2,
           d_tdsamt TYPE p DECIMALS 2,
            d_discamt TYPE regud-wskto,
            d_gross  TYPE regud-wrbtr.
      DATA: BEGIN OF t_bkpf,
             bukrs TYPE bkpf-bukrs,
             belnr TYPE bkpf-belnr,
             gjahr TYPE bkpf-gjahr,
             budat TYPE bkpf-budat,
             bktxt TYPE bkpf-bktxt,
             xblnr TYPE bkpf-xblnr,
            END OF t_bkpf.
      DATA: BEGIN OF t_bseg,
             bukrs TYPE bseg-bukrs,
             belnr TYPE bseg-belnr,
             gjahr TYPE bseg-gjahr,
             buzei TYPE bseg-buzei,
             umskz TYPE bseg-umskz,
             bschl TYPE bseg-bschl,
             qbshb TYPE bseg-qbshb,
             augbl TYPE bseg-augbl,
             rebzg TYPE bseg-rebzg,
             rebzj TYPE bseg-rebzj,
             sgtxt TYPE bseg-sgtxt,
             xref1 TYPE bseg-xref1,
             ebeln TYPE bseg-ebeln,
             shkzg type bseg-shkzg,
            END OF t_bseg.
      DATA: num VALUE ' ',
            d_result VALUE ' '.
      DATA: d_symbol VALUE '/',
            d_text(11) VALUE 'Payment for'.
      DATA: d_belnr1 LIKE bseg-belnr.
      DATA: wa_payrq TYPE payrq. " 15.07.2005 Rakesh
    18.05.2006
      data : wa_belnr like bseg-belnr,wa_augbl like bseg-augbl.
    18.05.2006
    06.09.2006
      data : d_other type p decimals 2.
    Read incoming data
    *break-point.
    *zpayment.
      CLEAR: d_belnr,d_bukrs,d_gjahr,d_result.
      clear d_tdsamt.
    Company code
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BUKRS'.
      d_bukrs = tab_in-value.
    Document number
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BELNR'.
      wa_belnr = d_belnr(10) = tab_in-value(10).
    Posting Key
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BSCHL'.
      d_bschl = tab_in-value.
    Special G/L Indicator
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-UMSKZ'.
      d_umskz = tab_in-value.
    Fiscal year
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-GJAHR'.
      d_gjahr = tab_in-value.
    <-- 15.07.2005 Rakesh
    Fiscal year1 - For Payment request case
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-GJAHR'.
      d_gjahr1 = tab_in-value.
    15.07.2005 Rakesh -->
    Document item
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BUZEI'.
      d_buzei = tab_in-value.
    Discount amount, if any
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-WSKTO'.
      REPLACE ALL OCCURRENCES OF ',' IN tab_in-value WITH ' '.
      d_discamt = tab_in-value.
    Gross Amount
    CLEAR tab_in-value.
    READ TABLE tab_in WITH KEY 'REGUD-WRBTR'.
    REPLACE ALL OCCURRENCES OF ',' IN tab_in-value WITH ' '.
    d_gross = tab_in-value.
    Gross Amount
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-WNETT'.
      REPLACE ALL OCCURRENCES OF ',' IN tab_in-value WITH ' '.
      d_netamt = tab_in-value.
    Clearing document 18.05.2006
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
      wa_augbl = tab_in-value.
    DEBIT/CREDIT INDICATOR
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-SHKZG'.
      d_SHKZG = tab_in-value(1).
    IF  d_netamt < 0 AND D_SHKZG = 'S'.
         D_NETAMT = D_NETAMT * '-1'.
    ENDIF.
    Check document no.  is clearing document than reset wnett = 0.
    18.05.2006
      select single * from bsak where augbl = wa_augbl
                       and bukrs = d_bukrs and gjahr = d_gjahr1.
      if sy-subrc = 0 and wa_belnr = wa_augbl.
         d_netamt = 0.
      endif.
    18.05.2006
    Check if Down payment request
      IF d_bschl = '39'.
    This line item is not considered
        d_result = 'X'.
      ELSE.
    Check if Down payment
        IF d_bschl = '29'
            and wa_augbl = wa_belnr.  "18.05.2006
          IF d_umskz EQ 'A'
          OR d_umskz EQ 'B'
          OR d_umskz EQ 'I'
          OR d_umskz EQ 'M'
          OR d_umskz EQ 'D'.
    Put Document Number of the Payment Document value to 'd_belnr'
            CLEAR tab_in-value.
            READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
            d_belnr = tab_in-value.
          ENDIF.
        ENDIF.
    <--15.07.2005 Rakesh
    Check if Payment request exists in 'payrq'
        CLEAR wa_payrq.
    Convert 'd_belnr' to 10 digits,if required
        CLEAR: l_intern,e_intern.
        CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                           ID 'OUTPUT' FIELD l_intern.
        e_intern = l_intern+10.
        d_belnr1 = d_belnr.
        CLEAR d_belnr.
        d_belnr  = e_intern.
        SELECT SINGLE keyno augbl FROM payrq
         INTO CORRESPONDING FIELDS OF wa_payrq
          WHERE keyno = d_belnr.
        IF sy-subrc = 0. " Yes
    Take clearing document which is the required accounting document
            d_belnr = wa_payrq-augbl.
    Convert 'd_belnr' to 10 digits,if required
          CLEAR: l_intern,e_intern.
          CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                             ID 'OUTPUT' FIELD l_intern.
          e_intern = l_intern+10.
          CLEAR d_belnr.
          d_belnr  = e_intern.
    Assign 'd_gjahr1' to 'd_gjahr' as this is relevant here.
          d_gjahr = d_gjahr1. " 15.07.2005 Rakesh
    Get BSEG data
          SELECT SINGLE bukrs belnr gjahr buzei augbl rebzg rebzj
                        sgtxt qbshb umskz bschl xref1 ebeln
           FROM bseg INTO CORRESPONDING FIELDS OF t_bseg
            WHERE bukrs EQ d_bukrs
              AND belnr EQ d_belnr
              AND gjahr EQ d_gjahr
           AND buzei EQ d_buzei      " not relevant
           AND bschl EQ '25'           " Posting key - Outgoing payment
              AND koart EQ 'S'           " G/L accounts
              AND shkzg EQ 'S'.          " Debit
        ELSE. " No
    For all other cases, we use this final logic, even though we have
    taken ‘d_belnr’ value from accounting doc no., in some earlier cases.
    Put document number of the payment document(vblnr) value to 'd_belnr',
        IF d_bschl = '29'
            and wa_augbl <> wa_belnr.  "18.05.2006
          IF d_umskz EQ 'A'
            OR d_umskz EQ 'B'
            OR d_umskz EQ 'I'
            OR d_umskz EQ 'M'
            OR d_umskz EQ 'D'.
         else.
          CLEAR tab_in-value.
          READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
          d_belnr = tab_in-value.
         endif.
       else.
          CLEAR tab_in-value.
          READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
          d_belnr = tab_in-value.
       endif.
    -->15.07.2005
    Convert 'd_belnr' to 10 digits,if required
          CLEAR: l_intern,e_intern.
          CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                             ID 'OUTPUT' FIELD l_intern.
          e_intern = l_intern+10.
          CLEAR d_belnr.
          d_belnr  = e_intern.
    Get BSEG data
          SELECT SINGLE bukrs belnr gjahr buzei augbl rebzg rebzj
                        sgtxt qbshb umskz bschl xref1 ebeln shkzg
           FROM bseg INTO CORRESPONDING FIELDS OF t_bseg
            WHERE bukrs EQ d_bukrs
              AND belnr EQ d_belnr
              AND gjahr EQ d_gjahr1
    "changed by Bharat 15.04.2006 - d_gjahr1 contains payment doc fiscal yea
    "r and d_gjahr contains invoice fiscal year
         AND buzei EQ d_buzei      "amisha 10-10-2005
         AND bschl EQ '25'           " Posting key - Outgoing payment
              AND shkzg EQ 'S'       "added by Bharat 17.04.2006
              AND ( koart EQ 'K' OR koart EQ 'D').
    <--15.07.2005 Rakesh
    18.05.2006
    in clearing document payment advice not consider TDS amount of
    payment document
         if wa_belnr <> wa_augbl and d_shkzg = 'S'.
            t_bseg-qbshb = t_bseg-qbshb * '-1'.
            d_netamt = d_netamt + t_bseg-qbshb.
            t_bseg-qbshb = 0.
         endif.
    18.05.2006
        ENDIF.
    -->15.07.2005
    Get corresponding BKPF data
    If Down payment
        IF d_bschl = '29'.
          IF d_umskz EQ 'A'
          OR d_umskz EQ 'B'
          OR d_umskz EQ 'I'
          OR d_umskz EQ 'M'
          OR d_umskz EQ 'D'.
            SELECT SINGLE bukrs belnr gjahr budat bktxt xblnr
             FROM bkpf INTO CORRESPONDING FIELDS OF t_bkpf
              WHERE bukrs EQ d_bukrs
                AND belnr EQ d_belnr
                AND gjahr EQ d_gjahr1.
    "changed by Bharat 15.04.2006 - d_gjahr1 contains payment doc fiscal yea
    "r and d_gjahr contains invoice fiscal year
          ENDIF.
        ELSE.
    If Partial payment/On-account payment
          IF t_bseg-augbl IS INITIAL.
    On-Account case
         IF t_bseg-augbl IS INITIAL.
            IF t_bseg-rebzg IS INITIAL.
              t_bseg-rebzg = t_bseg-belnr.   " Payment Document Number
              t_bseg-rebzj = t_bseg-gjahr.   " Fiscal year
            ENDIF.
         ENDIF.
            SELECT SINGLE bukrs belnr gjahr budat bktxt xblnr
             FROM bkpf INTO CORRESPONDING FIELDS OF t_bkpf
              WHERE bukrs EQ t_bseg-bukrs
                AND belnr EQ t_bseg-rebzg
                AND gjahr EQ t_bseg-rebzj.
          ELSE.
    FULL PAYMENT with Discount
           clear d_tdsamt.
    Following code added for GSPL by bharat06092006
    if d_bukrs eq 'GSPL'.
    DATA : V_TEXT(20) TYPE C.
        V_TEXT = 'W%'.
        SELECT SINGLE wrbtr FROM bseg
               INTO d_tdsamt
               WHERE bukrs EQ d_bukrs  AND
                     belnr EQ d_belnr1 AND
                     gjahr EQ d_gjahr  AND         " Added by bharat on 10-04-2006 fiscal year
                     bschl EQ '50'     AND
                     KTOSL EQ 'WIT'    AND
                     QSSKZ NOT LIKE V_TEXT.
        SELECT single wrbtr FROM bseg
           INTO d_other
           WHERE bukrs EQ d_bukrs  AND
                 belnr EQ d_belnr1 AND
                 gjahr EQ d_gjahr  AND         " Added by bharat on 10-04-2006 fiscal year
                 bschl EQ '50'     AND
                 KTOSL EQ 'WIT'    AND
                 QSSKZ LIKE V_TEXT.
    ELSE.
    Added by bharat 06.09.2006
            SELECT SINGLE wrbtr
             FROM bseg INTO d_tdsamt
              WHERE bukrs EQ d_bukrs
                AND belnr EQ d_belnr1
                AND gjahr EQ d_gjahr
                " Added by bharat on 10-04-2006 fiscal year
                AND bschl EQ '50' AND KTOSL EQ 'WIT'.
    ENDIF.
            SELECT SINGLE bukrs belnr gjahr budat bktxt xblnr
             FROM bkpf INTO CORRESPONDING FIELDS OF t_bkpf
              WHERE bukrs EQ d_bukrs
                AND belnr EQ d_belnr
                AND gjahr EQ d_gjahr1.
    "changed by Bharat 15.04.2006 - d_gjahr1 contains payment doc fiscal yea
    "r and d_gjahr contains invoice fiscal year
           tab_in-value
    *d_tdsamt_tot
          ENDIF.
        ENDIF.
        IF sy-subrc = 0.
    Sr. no. for Line items
          d_count = d_count + 1.
        ELSE.
    Else, this line item is not considered
          d_result = 'X'.
        ENDIF.
      ENDIF. " if d_bschl = '39'
    Pass the output to the outgoing table 'tab_out', if 'd_result' is not
    *set.
      IF d_result = ' '.
    Line count
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'COUNT'.
        tab_out-value = d_count.
        MODIFY tab_out INDEX sy-tabix.
    Result flag
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'RESULT1'.
        tab_out-value = d_result.
        MODIFY tab_out INDEX sy-tabix.
    Clearing document
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'AUGBL'.
        tab_out-value = t_bseg-augbl.
        MODIFY tab_out INDEX sy-tabix.
        IF d_bschl <> '29'.
    On-Account case
          IF t_bseg-augbl IS INITIAL.
            IF t_bseg-rebzg IS INITIAL.
              t_bseg-rebzg = t_bseg-belnr.   " Payment Document Number
            ENDIF.
          ENDIF.
        ELSE.
    Payment document request
          IF NOT t_bseg-ebeln IS INITIAL. " First preference
            t_bseg-rebzg = t_bseg-ebeln.
          ELSE.
            t_bseg-rebzg = d_belnr.
          ENDIF.
        ENDIF.
    Number of the Invoice the Transaction Belongs to
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'REBZG'.
        SHIFT t_bseg-rebzg LEFT DELETING LEADING num.
        tab_out-value = t_bseg-rebzg.
        MODIFY tab_out INDEX sy-tabix.
    Document number - check use??
       CLEAR: tab_out-value,tab_out-name.
       READ TABLE tab_out WITH KEY 'BELNR'.
       tab_out-value = d_belnr.                   " check!!
       MODIFY tab_out INDEX sy-tabix.
    Net amount
       d_netamt = d_gross - d_discamt - t_bseg-qbshb.
    Gross amount
        d_gross = d_netamt + d_tdsamt + t_bseg-qbshb.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'NETAMT'.
        tab_out-value = d_netamt.
        SHIFT tab_out-value LEFT DELETING LEADING space.
        MODIFY tab_out INDEX sy-tabix.
    TDS Amount
        IF t_bseg-qbshb IS INITIAL.
          CLEAR: tab_out-value,tab_out-name.
          READ TABLE tab_out WITH KEY 'TDSAMT'.
          tab_out-value = d_tdsamt.
          SHIFT tab_out-value LEFT DELETING LEADING space.
          MODIFY tab_out INDEX sy-tabix.
    Added by bharat 06.09.2006
         CLEAR: tab_out-value,tab_out-name.
         READ TABLE tab_out WITH KEY 'D_OTHER'.
         tab_out-value = d_other.
         SHIFT tab_out-value LEFT DELETING LEADING space.
         MODIFY tab_out INDEX sy-tabix.
    Added by bharat 06.09.2006
        ELSE.
          CLEAR: tab_out-value,tab_out-name.
          READ TABLE tab_out WITH KEY 'TDSAMT'.
          tab_out-value = t_bseg-qbshb.
          SHIFT tab_out-value LEFT DELETING LEADING space.
          MODIFY tab_out INDEX sy-tabix.
        ENDIF.
    *Calculate Total Net amount & TDS amount which is stored in Global
    *memory and accessed later
    from 'FORM Z_TOTAL_AMT'
        d_netamt_tot = d_netamt_tot + d_netamt.
        d_grossamt_tot = d_grossamt_tot + d_gross.
        d_tdsamt_tot = d_tdsamt_tot + t_bseg-qbshb + d_tdsamt + d_other.
    CLEAR: tab_OUT-value,tab_out-name.
    READ TABLE tab_OUT WITH KEY 'NETAMT_TOT'.
    tab_out-value = d_netamt_tot.
    shift tab_out-value left deleting leading space.
    MODIFY tab_out INDEX sy-tabix.
    Document Header text
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'BKTXT'.
        IF d_bschl = '29'.
          tab_out-value = t_bseg-sgtxt.    " for down payment request
        ELSE.
          tab_out-value = t_bkpf-bktxt.
        ENDIF.
        MODIFY tab_out INDEX sy-tabix.
    Reference Document Number - Header
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'XBLNR'.
        IF d_bschl = '29'.
          tab_out-value = t_bseg-xref1.   " for down payment request
        ELSE.
          tab_out-value = t_bkpf-xblnr.
        ENDIF.
        MODIFY tab_out INDEX sy-tabix.
    Symbol '/'
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'D_SYMBOL'.
        tab_out-value = d_symbol.
        MODIFY tab_out INDEX sy-tabix.
    Text 'Payment for'
       CLEAR: tab_out-value,tab_out-name.
       READ TABLE tab_out WITH KEY 'D_TEXT'.
       tab_out-value = d_text.
       MODIFY tab_out INDEX sy-tabix.
      CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'D_TEXT'.
        tab_out-value = d_text.
        tab_out-xblnr = t_bkpf-xblnr.
        MODIFY tab_out by bkpf-xblnr INDEX sy-tabix.
    Posting date
        CLEAR d_budat.
        CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
          EXPORTING
            date_internal            = t_bkpf-budat
          IMPORTING
            date_external            = d_budat
          EXCEPTIONS
            date_internal_is_invalid = 1
            OTHERS                   = 2.
        IF sy-subrc <> 0.
         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'BUDAT'.
        tab_out-value = d_budat.
        MODIFY tab_out INDEX sy-tabix.
      ELSE.
    Result flag
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'RESULT1'.
        tab_out-value = d_result.
        MODIFY tab_out INDEX sy-tabix.
      ENDIF.
    d_gross = d_netamt + d_tdsamt + t_bseg-qbshb.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'GROSSAMT'.
      tab_out-value = d_gross.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
    ENDFORM.                    " ZPAYMENT
    *&      Form  zamtwords
          text - If only 'Net Amount' is required
    -->  p1        text
    <--  p2        text
    FORM zamtwords TABLES tab_in  STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      DATA: d_swnet  TYPE regud-swnes,  " take character format type
            d_swnet1 TYPE bseg-dmbtr.
           d_words(160),
           d_words1(80),
           d_words2(80).
    <-- 21.05.2005
      DATA: d_words(160),
            d_words1(80) TYPE c,
            d_words2(80) TYPE c.
      DATA: len TYPE i.
    <-- 21.05.2005
    *zamtwords
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-SWNET'.
      d_swnet = tab_in-value.
    REPLACE ALL OCCURRENCES OF '*' IN d_swnes WITH ' '.
      REPLACE ALL OCCURRENCES OF ',' IN d_swnet WITH ' '.
      d_swnet1 = d_swnet.
      CLEAR d_swnet.
      PERFORM z_conv_amt USING d_swnet1 CHANGING d_swnet.
    Return 'SWNET1' without commas
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'SWNET1'.
    d_swnet = d_swnet1.
      tab_out-value = d_swnet.
      MODIFY tab_out INDEX sy-tabix.
    Amount in Indian currency format
      CALL FUNCTION 'Z_SPELL_AMOUNT_INR'
        EXPORTING
          amount     = d_swnet1
        IMPORTING
          rupees_str = d_words.
    Adjust 'd_words' left aligned
      SHIFT d_words LEFT DELETING LEADING space.
    <-- 21.05.2005
    Split 'd_words' into 'd_words1' and 'd_words2'
    since 'DEFINE' in SAPScript displays maximum 80 characters
    d_words1 = d_words(80).
    d_words2 = d_words+80(80).
    *CALL FUNCTION 'C147_STRING_SPLIT_AT_POSITION'
    EXPORTING
       i_string         = d_words
       i_position       = 80
    IMPORTING
       E_HEAD           = d_words1
       E_TAIL           = d_words2.
      len = STRLEN( d_words ).
    Only 80 characters can be passed to one SAPScript symbol
    Split 'd_words' into 'd_words1' and 'd_words2'
    since 'DEFINE' in SAPScript displays maximum 80 characters
    Check if string split is required
      IF len > 80.
        d_words1 = d_words(80).                                 " First 80
        d_words2 = d_words+80(80).                              " Next 80
    Introduce an initial space in 'd_words2' if 80th character in
    'd_words1' is space, as this space is not considered when 'd_words1'
    value is passed to the SAPScript, since we need proper spacing
    between 'd_words1' and 'd_words2' in the Cheque layout.
        IF d_words1+79(1) = ' '.
          SHIFT d_words2 RIGHT BY 1 PLACES.
        ENDIF.
      ELSE.
        d_words1 = d_words.
        d_words2 = space.
      ENDIF.
    21.05.2005 -->
    Amount in words1
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'WORDS'.
      tab_out-value = d_words1.
      MODIFY tab_out INDEX sy-tabix.
    Amount in words2
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'WORDS1'.
      tab_out-value = d_words2.
      MODIFY tab_out INDEX sy-tabix.
    ENDFORM.                    " zamtwords
    *&      Form  z_address
          text
    -->  p1        text
    <--  p2        text
    FORM z_address TABLES tab_in  STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      DATA d_bukrs TYPE bseg-bukrs.
      DATA: t_t001 TYPE t001,
            t_adrc TYPE adrc.
      CLEAR: tab_in-value,t_t001,t_adrc.
      READ TABLE tab_in WITH KEY 'REGUP-BUKRS'.
      d_bukrs = tab_in-value.
      CALL FUNCTION 'K_READ_T001'
        EXPORTING
          i_bukrs   = d_bukrs
        IMPORTING
          e_t001    = t_t001
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ELSE.
        CALL FUNCTION 'RTP_US_DB_ADRC_READ'
          EXPORTING
            i_address_number       = t_t001-adrnr
      I_NATION               = ' '
      I_READ_DB              =
         IMPORTING
            e_adrc                 = t_adrc
         EXCEPTIONS
           not_found              = 1
           OTHERS                 = 2
        IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-NAME1'.
        tab_out-value = t_adrc-name1.
        MODIFY tab_out INDEX sy-tabix.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-STREET'.
        tab_out-value = t_adrc-street.
        MODIFY tab_out INDEX sy-tabix.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-CITY1'.
        tab_out-value = t_adrc-city1.
        MODIFY tab_out INDEX sy-tabix.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-POST_CODE1'.
        tab_out-value = t_adrc-post_code1.
        MODIFY tab_out INDEX sy-tabix.
      ENDIF.
    ENDFORM.                    " z_address
    *&      Form  z_total_amt
          text
    -->  p1        text
    <--  p2        text
    FORM z_total_amt TABLES tab_in STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'NETAMT_TOT'.
      tab_out-value = d_netamt_tot.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'TDSAMT_TOT'.
      tab_out-value = d_tdsamt_tot.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'GROSSAMT_TOT'.
      tab_out-value = d_grossamt_tot.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
    ENDFORM.                    " z_total_amt
    *&      Form  Z_NET_AMT
          text
         -->TAB_IN     text
         -->TAB_OUT    text
    FORM z_net_amt TABLES tab_in  STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      TYPES: imis_type_c20(20) TYPE c,
             imis_type_c10(10) TYPE c.
      DATA: l_intern TYPE imis_type_c20.
      DATA: e_intern TYPE imis_type_c10.
      DATA: i_extern TYPE imis_type_c10.
      DATA: d_belnr(10),
            d_bukrs TYPE bseg-bukrs,
            d_gjahr TYPE bseg-gjahr,
            d_umskz TYPE bseg-umskz,
            d_bschl TYPE bseg-bschl,
            d_dmbtr1 TYPE bseg-dmbtr,
            d_dmbtr(17). "prb16052006
           d_dmbtr1 like regud-swnes. "prb16052006
      DATA d_amt TYPE char18.
      DATA: BEGIN OF t_bseg OCCURS 10,
             bukrs TYPE bseg-bukrs,
             belnr TYPE bseg-belnr,
             gjahr TYPE bseg-gjahr,
             buzei TYPE bseg-buzei,
             dmbtr TYPE bseg-dmbtr,
             wrbtr TYPE bseg-wrbtr,
            umskz TYPE bseg-umskz,
             bschl TYPE bseg-bschl,
             qbshb TYPE bseg-qbshb,
             NEbtR TYPE BSEG-nebtr, "prb13052006
            END OF t_bseg.
    <-- 21.05.2005
      DATA: d_words(160),
            d_words1(80) TYPE c,
            d_words2(80) TYPE c.
      DATA: len TYPE i.
    <-- 21.05.2005
      DATA wa_payrq TYPE payrq. " 15.07.2005 Rakesh
    Read incoming data
      CLEAR: d_belnr,d_bukrs,d_gjahr.
    Company code
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BUKRS'.
      d_bukrs = tab_in-value.
    Document number
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BELNR'.
      d_belnr(10) = tab_in-value(10).
    Posting Key
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BSCHL'.
      d_bschl = tab_in-value.
    Special G/L Indicator
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-UMSKZ'.
      d_umskz = tab_in-value.
    Fiscal Year
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-GJAHR'.  " Note
      CHECK sy-subrc = 0.
      d_gjahr = tab_in-value.
    Check if Down payment
      IF d_bschl = '29'.
        IF d_umskz EQ 'A'
        OR d_umskz EQ 'B'
        OR d_umskz EQ 'I'
        OR d_umskz EQ 'M'
        OR d_umskz EQ 'D'.
    Put Document Number of the Payment Document value to 'd_belnr'
          CLEAR tab_in-value.
          READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
          d_belnr = tab_in-value.
        ENDIF.
      ENDIF.
    Check if Down payment request
      IF d_bschl = '39'.
    Put Document Number of the Payment Document value to 'd_belnr'
        CLEAR tab_in-value.
        READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
        d_belnr = tab_in-value.
      ENDIF.
    <--15.07.2005 Rakesh
    Check if Payment request exists in 'payrq'
      CLEAR wa_payrq.
    Convert 'd_belnr' to 10 digits,if required
      CLEAR: l_intern,e_intern.
      CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                         ID 'OUTPUT' FIELD l_intern.
      e_intern = l_intern+10.
      CLEAR d_belnr.
      d_belnr  = e_intern.
      SELECT SINGLE keyno augbl FROM payrq
       INTO CORRESPONDING FIELDS OF wa_payrq
        WHERE keyno = d_belnr.
      IF sy-subrc = 0. " Yes
    Take clearing document which is the required accounting document
        d_belnr = wa_payrq-augbl.
    Convert 'd_belnr' to 10 digits,if required
        CLEAR: l_intern,e_intern.
        CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                           ID 'OUTPUT' FIELD l_intern.
        e_intern = l_intern+10.
        CLEAR d_belnr.
        d_belnr  = e_intern.
    Get BSEG data
        SELECT bukrs belnr gjahr buzei augbl rebzg rebzj sgtxt dmbtr wrbtr
        qbshb nebtr
         FROM bseg INTO CORRESPONDING FIELDS OF TABLE t_bseg
          WHERE bukrs EQ d_bukrs
            AND belnr EQ d_belnr
            AND gjahr EQ d_gjahr
         AND buzei EQ d_buzei
         AND bschl EQ '25'
              AND koart EQ 'S'           " G/L accounts
              AND shkzg EQ 'S'.          " Debit
      ELSE. " No
    For all other cases, we use this final logic, even though we have
    taken ‘d_belnr’ value from accounting doc no., in some earlier cases.
    Put document number of the payment document(vblnr) value to 'd_belnr',
        CLEAR tab_in-value.
        READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
        d_belnr = tab_in-value.
    -->15.07.2005
    Convert 'd_belnr' to 10 digits.
        CLEAR: l_intern,e_intern.
        CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                           ID 'OUTPUT' FIELD l_intern.
        e_intern = l_intern+10.
        CLEAR d_belnr.
        d_belnr  = e_intern.
    *BSEG - PARTIAL PAYMT/FULL PAYMT WT DISC - Get Total Amount without Bank
    *charges
        SELECT bukrs belnr gjahr buzei augbl rebzg rebzj sgtxt dmbtr wrbtr
        qbshb bschl NEBTR
         FROM bseg INTO CORRESPONDING FIELDS OF TABLE t_bseg
          WHERE bukrs EQ d_bukrs
            AND belnr EQ d_belnr
            AND gjahr EQ d_gjahr
         AND buzei EQ d_buzei
         AND bschl EQ '25'
            AND ( koart EQ 'K' OR koart EQ 'D' ).
    <--15.07.2005 Rakesh
      ENDIF.
    -->20.10.2005 By Yogesh.
      CLEAR d_dmbtr.
    IF sy-subrc = 0.
      LOOP AT t_bseg.
       d_dmbtr = d_dmbtr + t_bseg-dmbtr - t_bseg-qbshb.
        SELECT SINGLE shkzg INTO (shkzg) FROM tbsl
        WHERE bschl = t_bseg-bschl.
    *BREAK-POINT.
    *******prb13052
       IF shkzg = 'S'.
         d_dmbtr = d_dmbtr + t_bseg-wrbtr - t_bseg-qbshb.
       ELSEIF shkzg = 'H'.
         d_dmbtr = d_dmbtr - t_bseg-wrbtr - t_bseg-qbshb.
       ENDIF.
       IF shkzg = 'S'.
         d_dmbtr = d_dmbtr + t_bseg-nebtr." - t_bseg-qbshb.
       ELSEIF shkzg = 'H'.
         if t_bseg-nebtr <> ''.
         d_dmbtr = d_dmbtr - t_bseg-nebtr." - t_bseg-qbshb.
         else.
         d_dmbtr = d_dmbtr - t_bseg-wrbtr." - t_bseg-qbshb.
         endif.
       ENDIF.
    *tables: reguh, payr.
      select single * from reguh where zbukr = d_bukrs
                           and vblnr = d_belnr
                           and LAUFD+6(4) = d_gjahr.
    *select single * from payr where zbukr = d_bukrs
    *and vblnr = d_belnr
    *and hbkid = reguh-hbkid
    *and hktid = reguh-hktid.
    *d_dmbtr = payr-rwbtr.
    *if d_dmbtr < 0.
    *d_dmbtr = d_dmbtr * ( -1 ).
    *endif.
    CLEAR tab_in-value.
        READ TABLE tab_in WITH KEY 'REGUD-SWNES'.
        d_DMBTR  =  tab_in-value.
    translate d_dmbtr using '* '.
    translate d_dmbtr using ', '.
    condense d_dmbtr no-gaps.
    d_dmbtr1 = d_dmbtr.
    *******prb13052
      ENDLOOP.
      PERFORM z_conv_amt USING d_dmbtr CHANGING d_amt.
    *Pass the Net Amount output to the outgoing table tab_out-name =
    *'NETAMT_TOT'.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'NETAMT_TOT'.
      tab_out-value = d_amt.
      " For inserting '*' ahead of value
    SHIFT tab_out-value LEFT DELETING LEADING space.  """"????IMP
      MODIFY tab_out INDEX sy-tabix.
    Get the Amount in words for Indian currency
    Can also use the FM 'HR_IN_CHG_INR_WRDS'
      CALL FUNCTION 'Z_SPELL_AMOUNT_INR'
        EXPORTING
          amount     = d_dmbtr1
        IMPORTING
          rupees_str = d_words.
    Shift left since above Function Module introduces an initial space
      SHIFT d_words LEFT DELETING LEADING space.
    <-- 21.05.2005
    *CALL FUNCTION 'C147_STRING_SPLIT_AT_POSITION'
    EXPORTING
       i_string         = d_words
       i_position       = 80
    IMPORTING
       E_HEAD           = d_words1
       E_TAIL           = d_words2.
      len = STRLEN( d_words ).
    Only 80 characters can be passed to one SAPScript symbol
    Split 'd_words' into 'd_words1' and 'd_words2'
    since 'DEFINE' in SAPScript displays maximum 80 charact

  • Need help. Making query faster.

    Hello SQL Oracle Expert.
    Need Help of below SQL.
    The Query Takes 60second to process.
    What do i need to do to make it faster.
    Thanks.
    Indexing on a and b. total Record of 20milion. Using Oracle 10g.
    Testing query using SQLtool.
    Hope there is someone who can help. :D
    SELECT
    FROM
         (     SELECT
                   xxx.*, rownum rnum
              FROM
                   (     SELECT
                             a,b,c
                        FROM
                             (     SELECT
                                       a,b,c
                                  FROM
                                       table_a
                                       LEFT JOIN table_b
                                       ON table_a.a = table_b.x
                                  UNION
    SELECT
    a,b,c
    FROM
    table_c
    WHERE
                                       table_c.c NOT IN ('19','20','52','53','10')
    UNION
    SELECT
    a,b,c
    FROM
    table_d
                                  WHERE
                                       table_d.c NOT IN ('19','20','52','53','10')
                             LEFT JOIN table_e
                             ON a = table_e.a AND c = table_e.c AND SUBSTR(b,23,8) = to_char(table_e.b,'YYYYMMDD')
                        WHERE
                             b BETWEEN To_Date('01/05/2009 000000','DD/MM/YYYY HH24MISS') AND To_Date(
                             '01/06/2009 235959', 'DD/MM/YYYY HH24MISS') AND (a LIKE '%W%')
                        ORDER BY
                             2 ASC
                   xxx
              WHERE
                   rownum <= 20
    WHERE
         rnum >= 1

    Hello,
    How about you generate an execution plan and post its output here, beside oracle version and OS?
    sqlplus username/password
    sql>set autotrace traceonly;
    sql>set lines 400;
    sql>set timi on;
    sql>@myquery.sql
    #myquery.sql
    SELECT   *
      FROM   (SELECT   xxx.*, ROWNUM rnum
                FROM   (  SELECT   a, b, c
                            FROM      (SELECT   a, b, c
                                         FROM      table_a
                                                LEFT JOIN
                                                   table_b
                                                ON table_a.a = table_b.x
                                       UNION
                                       SELECT   a, b, c
                                         FROM   table_c
                                        WHERE   table_c.c NOT IN
                                                      ('19', '20', '52', '53', '10')
                                       UNION
                                       SELECT   a, b, c
                                         FROM   table_d
                                        WHERE   table_d.c NOT IN
                                                      ('19', '20', '52', '53', '10'))
                                   LEFT JOIN
                                      table_e
                                   ON a = table_e.a AND c = table_e.c
                                      AND SUBSTR (b, 23, 8) =
                                            TO_CHAR (table_e.b, 'YYYYMMDD')
                           WHERE   b BETWEEN TO_DATE ('01/05/2009 000000',
                                                      'DD/MM/YYYY HH24MISS')
                                         AND  TO_DATE ('01/06/2009 235959',
                                                       'DD/MM/YYYY HH24MISS')
                                   AND (a LIKE '%W%')
                        ORDER BY   2 ASC) xxx
               WHERE   ROWNUM <= 20)
    WHERE   rnum >= 1h3. Also post your code or output between \ tags to preserve formatting
      your code or output goes here
    \Regards
    OrionNet
    Regards                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Procedure to save the output of a query into excel file or flat file

    Procedure to save the output of a query into excel file or flat file
    I want to store the output of my query into a file and then export it from sql server management studio to a desired location using stored procedure.
    I have run the query --
    DECLARE @cmd VARCHAR(255)
    SET @cmd = 'bcp "select * from dbo.test1" queryout "D:\testing2.xlsx;" -U "user-PC\user" -P "" -c '
    Exec xp_cmdshell @cmd
    error message--
    SQLState = 28000, NativeError = 18456
    Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'user-PC\user'.
    NULL
    Goel.Aman

    Hello,
    -T:
    Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. The security credentials of the network user,
    login_id, and password are not required. If
    –T is not specified, you need to specify
    –U and –P to successfully log in.
    -U:
    Specifies the login ID used to connect to SQL Server.
    Note: When the bcp utility is connecting to SQL Server with a trusted connection using integrated security, use the
    -T option (trusted connection) instead of the
    user name and password combination
    I would suggest you take a look at the following article:
    bcp Utility: http://technet.microsoft.com/en-us/library/ms162802.aspx
    A similar thread regarding this issue:
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/b450937f-0ef5-427a-ae3b-115335c0d83c/bcp-connection-error-sqlstate-28000-nativeerror-18456?forum=sqldataaccess
    Regards,
    Elvis Long
    TechNet Community Support

  • Help needed in writing a query

    Hi all,
    Following is the structure of my table.
    Data
    Key Number
    Id Number
    Value varchar2(100)
    activity_name varchar2(100)
    Creation_Date Date
    Eval_Point varchar2(100)
    In the above table Id is the primary key.
    The column eval_point holds only two types of entries 'activation' or 'completion'
    The activity_name column holds the name of the activity.
    The sample entries in the table are as follows
    Key Value activity_name Creation_Date Id Eval_Point
    260002 XXX assign_1 2007-09-21 16:58:41.920000 951 activation
    260002     XXX assign_1 2007-09-21 16:58:43.392000 953     completion
    260002     XXX assign_2 2007-09-21 16:59:03.732000 956     activation
    260002     XXX assign_2 2007-09-21 16:59:04.112000 954     completion
    260002     XXX assign_3 2007-09-21 16:59:24.331000     958     activation
    260002     XXX assign_3 2007-09-21 16:59:24.421000     957     completion
    i need to write a query which gives me data in the following format
    value id start_date end_date
    XXX YYY 2007-09-21 16:58:41.920000 2007-09-21 16:58:43.392000
    where start_date is the creation date of the 'activation' and end_date is the creation_date of 'completion'.
    Can somebody help?
    -thanks
    lavanya

    hello all,
    I would like to re frame my question.
    this is the output of the base query
    select id,instance_key,sensor_target,activity_sensor,creation_date,eval_point from bpel_variable_sensor_values where instance_key=260002;
    953     260002     Assign_1     952     2007-09-21 16:58:43.392000     completion
    951     260002     Assign_1     952     2007-09-21 16:58:41.920000     activation
    956     260002     Assign_2     955     2007-09-21 16:59:03.732000     activation
    954     260002     Assign_2     955     2007-09-21 16:59:04.112000     completion
    958     260002     Assign_3     959     2007-09-21 16:59:24.331000     activation
    957     260002     Assign_3     959     2007-09-21 16:59:24.421000     completion
    962     260002     Assign_4     960     2007-09-21 16:59:44.741000     completion
    961     260002     Assign_4     960     2007-09-21 16:59:44.640000     activation
    964     260002     Assign_5     965     2007-09-21 17:00:05.290000     completion
    963     260002     Assign_5     965     2007-09-21 17:00:04.950000     activation
    I am trying out this query
    select a.instance_key,a.creation_date,b.creation_date,a.id
    from bpel_variable_sensor_values a, bpel_variable_sensor_values b
    where a.instance_key=b.instance_key
    and a.instance_key=260002
    and a.eval_point='activation'
    and b.eval_point='completion'
    and i am getting 25 entries i.e a cartesian product of a.creation_date
    260002     2007-09-21 16:58:41.920000     2007-09-21 16:58:43.392000     951
    260002     2007-09-21 16:58:41.920000     2007-09-21 16:59:04.112000     951
    260002     2007-09-21 16:58:41.920000     2007-09-21 16:59:24.421000     951
    260002     2007-09-21 16:58:41.920000     2007-09-21 16:59:44.741000     951
    260002     2007-09-21 16:58:41.920000     2007-09-21 17:00:05.290000     951
    260002     2007-09-21 16:59:03.732000     2007-09-21 16:58:43.392000     956
    260002     2007-09-21 16:59:03.732000     2007-09-21 16:59:04.112000     956
    260002     2007-09-21 16:59:03.732000     2007-09-21 16:59:24.421000     956
    260002     2007-09-21 16:59:03.732000     2007-09-21 16:59:44.741000     956
    260002     2007-09-21 16:59:03.732000     2007-09-21 17:00:05.290000     956
    260002     2007-09-21 16:59:24.331000     2007-09-21 16:58:43.392000     958
    260002     2007-09-21 16:59:24.331000     2007-09-21 16:59:04.112000     958
    260002     2007-09-21 16:59:24.331000     2007-09-21 16:59:24.421000     958
    260002     2007-09-21 16:59:24.331000     2007-09-21 16:59:44.741000     958
    260002     2007-09-21 16:59:24.331000     2007-09-21 17:00:05.290000     958
    260002     2007-09-21 16:59:44.640000     2007-09-21 16:58:43.392000     961
    260002     2007-09-21 16:59:44.640000     2007-09-21 16:59:04.112000     961
    260002     2007-09-21 16:59:44.640000     2007-09-21 16:59:24.421000     961
    260002     2007-09-21 16:59:44.640000     2007-09-21 16:59:44.741000     961
    260002     2007-09-21 16:59:44.640000     2007-09-21 17:00:05.290000     961
    260002     2007-09-21 17:00:04.950000     2007-09-21 16:58:43.392000     963
    260002     2007-09-21 17:00:04.950000     2007-09-21 16:59:04.112000     963
    260002     2007-09-21 17:00:04.950000     2007-09-21 16:59:24.421000     963
    260002     2007-09-21 17:00:04.950000     2007-09-21 16:59:44.741000     963
    260002     2007-09-21 17:00:04.950000     2007-09-21 17:00:05.290000     963
    can soembody help me to reduce these to 5 rows.

Maybe you are looking for

  • How can I print web pages with HP psc 2175 printer?

    Hi, I'm having difficulty printing web pages with my HP psc 2175 printer. The printer spits out many pages before the actual page I want printed and then at times it doesn't print all of the section desired. In fact, when I copied one page the other

  • Release of Payment inspite of material lying in QI Stock

    Dear guru I have a query, pLease give me some suggestion After receipt of material in the stores, the challan is entered in SAP with the transaction code: MIGO and the material stock indicate in QI (QUALITY INSPECTION) Stock. After inspection of mate

  • User-exit  for displaying message in LT15

    Hi, I am looking for a user-exit (or another way?) for displaying a warning message when the user tries to cancel a TO via LT15 (under certain conditions). But the only user-exit that I found that would do the thing here is ZXLTOU02, but it's perform

  • Accessing xml forms for anonymous user

    Hi all, Is it possible in any way to use the "news" made with XML-Forms on an External Facing Portal? I have created one page in XML form builder and assigned that page for anonymous user. Initially that page is visible to anonymous user. If i click

  • I cannot connect to apps such as Pandora and App store. The internet does work though.

    I cannot connect to Pandora and some other apps like app store. My internet is working and I can get on the internet from my ipod. What could be wrong?