Splitting the huge SELECT statement

Hi Gurus,
I need help in order to split the huge SELECT statement into individual select for each table (AFKO, AFVC)...I don't have to much experience into that, could someone guide me into or provide finished code?
Thanks
OPEN CURSOR WITH HOLD s_cursor FOR
SELECT
t1~mandt AS mandt
t1~aufnr AS aufnr
t1~aufpl AS aufpl
t2~aplzl AS aplzl
t2~banfn AS banfn
t2~bnfpo AS bnfpo
t3~ebeln AS ebeln
t3~ebelp AS ebelp
t4~vgabe AS vgabe
t4~belnr AS belnr
t4~buzei AS buzei
t4~gjahr AS gjahr
t4~zekkn AS zekkn
t4~srvpos AS srvpos
t6~menge AS menge "D02K924592
t3~dmbtr AS dmbtr "D02K924378
t4~cpudt AS cpudt
t4~shkzg AS shkzg
t4~budat AS budat
t3~lifnr AS lifnr
t2~vornr AS vornr_sactv
t7~vornr AS vornr_actv
t2~sumnr AS sumnr
t2~prctr AS prctr
t6~meins AS meins "D02K924592
t3~waers AS waers "D02K924378
t3~sakto AS kstar "D02K924456
t6~packno AS packno "D02K924592
t6~introw AS introw "D02K924592
FROM afko AS t1
INNER JOIN afvc AS t2 ON t2mandt = t1mandt
AND t2aufpl = t1aufpl
INNER JOIN mseg AS t3 ON t3mandt = t2mandt "D02K924373
AND t3aufpl = t2aufpl "D02K924373
AND t3aplzl = t2aplzl "D02K924373
INNER JOIN ekbe AS t4 ON t4mandt = t3mandt
AND t4ebeln = t3ebeln
AND t4ebelp = t3ebelp
AND t4zekkn = t3zekkn "D02K924373
AND t4~vgabe = 1 "D02K924373
AND t4gjahr = t3mjahr "D02K924373
AND t4belnr = t3mblnr "D02K924373
AND t4buzei = t3zeile "D02K924373
INNER JOIN t430 AS t5 ON t5mandt = t2mandt "D02K924489
AND t5~plnaw = '*' "D02K924489
AND t5steus = t2steus "D02K924489
AND t5~service = 'X' "D02K924489
LEFT OUTER JOIN esll AS t6 ON t6mandt = t4mandt "D02K924592
AND t6packno = t4packno "D02K924592
AND t6introw = t4introw "D02K924592
INNER JOIN afvc AS t7 ON t2mandt = t7mandt
AND t2aufpl = t7aufpl
AND t2sumnr = t7aplzl
WHERE t1~mandt = sy-mandt
AND t1~aufnr LIKE '00006%'
AND t1~aufnr IN l_r_aufnr
AND t2~prctr = '0000005080'
AND t2~sumnr '00000000'
AND t4~vgabe = 1
AND t4~cpudt IN l_r_cpudt
AND t2~steus IN l_r_steus "D02K924627
AND t5~steus IN l_r_steus. "D02K924627.
Regards

you can use for all entries option like this...
" first select data from first table
data: it_table1 type table of afko .
SELECT
t1~mandt
t1~aufnr
t1~aufpl
FROM afko  as t1
WHERE t1~mandt = sy-mandt
AND t1~aufnr LIKE '00006%'
AND t1~aufnr IN l_r_aufnr into table it_table1 .
" then we ll select the data from second table using the entries selected from the first table and field conditions for second tabel itself.
data: it_table2 type table of afvc .
select
t2~aplzl
t2~banfn
t2~bnfpo
t2~vornr
t2~sumnr
t2~prctr
from afvc as t2 into table it_table2
for all entries in it_table1
where it_table1-mandt = t2~mandt
and   it_table1-aufpl = t2~aufpl
and t2~prctr = '0000005080'
AND t2~sumnr '00000000'
AND t2~steus IN l_r_steus "D02K924627 .
" then we ll select the data from third table using the entries selected from the first and second table and field conditions for second tabel itself.
data: it_table3 type table of mseg .
select
t3~ebeln AS ebeln
t3~ebelp AS ebelp
t3~dmbtr AS dmbtr "D02K924378
t3~lifnr AS lifnr
t3~waers AS waers "D02K924378
t3~sakto AS kstar "D02K924456
from mseg AS t3
for all entries in it_table2
where it_table2-mandt = t3~mandt
and it_table2-aufpl = t3~aufpl
and it_table2-aplzl = t3~aplzl .
like wise u can do for all the tables you have...
Edited by: prashanti korlepara on Jun 16, 2011 3:41 PM

Similar Messages

  • Increase performance of the following SELECT statement.

    Hi All,
    I have the following select statement which I would want to fine tune.
      CHECK NOT LT_MARC IS INITIAL.
      SELECT RSNUM
             RSPOS
             RSART
             MATNR
             WERKS
             BDTER
             BDMNG FROM RESB
                          INTO TABLE GT_RESB 
                          FOR ALL ENTRIES IN LT_MARC
                          WHERE XLOEK EQ ' '
                          AND MATNR EQ LT_MARC-MATNR
                          AND WERKS EQ P_WERKS
                          AND BDTER IN S_PERIOD.
    The following query is being run for a period of 1 year where the number of records returned will be approx 3 million. When the program is run in background the execution time is around 76 hours. When I run the same program dividing the selection period into smaller parts I am able to execute the same in about an hour.
    After a previous posting I had changed the select statement to
      CHECK NOT LT_MARC IS INITIAL.
      SELECT RSNUM
             RSPOS
             RSART
             MATNR
             WERKS
             BDTER
             BDMNG FROM RESB
                          APPENDING TABLE GT_RESB  PACKAGE SIZE LV_SIZE
                          FOR ALL ENTRIES IN LT_MARC
                          WHERE XLOEK EQ ' '
                          AND MATNR EQ LT_MARC-MATNR
                          AND WERKS EQ P_WERKS
                          AND BDTER IN S_PERIOD.
      ENDSELECT.
    But the performance improvement is very negligible.
    Please suggest.
    Regards,
    Karthik

    Hi Karthik,
    <b>Do not use the appending statement</b>
    Also you said if you reduce period then you get it quickly.
    Why not try dividing your internal table LT_MARC into small internal tables having max 1000 entries.
    You can read from index 1 - 1000 for first table. Use that in the select query and append the results
    Then you can refresh that table and read table LT_MARC from 1001-2000 into the same table and then again execute the same query.
    I know this sounds strange but you can bargain for better performance by increasing data base hits in this case.
    Try this and let me know.
    Regards
    Nishant
    > I have the following select statement which I would
    > want to fine tune.
    >
    >   CHECK NOT LT_MARC IS INITIAL.
    > SELECT RSNUM
    >          RSPOS
    > RSART
    >          MATNR
    > WERKS
    >          BDTER
    > BDMNG FROM RESB
    >                       INTO TABLE GT_RESB 
    > FOR ALL ENTRIES IN LT_MARC
    >                       WHERE XLOEK EQ ' '
    > AND MATNR EQ LT_MARC-MATNR
    >                       AND WERKS EQ P_WERKS
    > AND BDTER IN S_PERIOD.
    >  
    > e following query is being run for a period of 1 year
    > where the number of records returned will be approx 3
    > million. When the program is run in background the
    > execution time is around 76 hours. When I run the
    > same program dividing the selection period into
    > smaller parts I am able to execute the same in about
    > an hour.
    >
    > After a previous posting I had changed the select
    > statement to
    >
    >   CHECK NOT LT_MARC IS INITIAL.
    > SELECT RSNUM
    >          RSPOS
    > RSART
    >          MATNR
    > WERKS
    >          BDTER
    > BDMNG FROM RESB
    > APPENDING TABLE GT_RESB
    >   PACKAGE SIZE LV_SIZE
    >                     FOR ALL ENTRIES IN LT_MARC
    >   WHERE XLOEK EQ ' '
    >                     AND MATNR EQ LT_MARC-MATNR
    >   AND WERKS EQ P_WERKS
    >                     AND BDTER IN S_PERIOD.
    > the performance improvement is very negligible.
    > Please suggest.
    >
    > Regards,
    > Karthik
    Hi Karthik,

  • Change the default select statement in the execute_query function

    I want to list all records where c1 like '%ABD%'. So I set the following in the 'when_button_pressed' tigger:
    set_block_property ('block_name',DEFAULT_WHERE,'upper(c1) like '''||'%'||:block_name.blk_col_name||'%'||'''');
    execute_query;
    When I click on the button the :system.last_query holds the following select statement:
    select c1, c2, .. cn from t_n where upper(c1) like '%ABD%' and (upper(c1) = 'ABD' and (c1 like 'ab%' or c1 like 'aB%' or c1 like 'Ab%' or c1 like 'AB%')) order by c1
    Since the condition upper(c1) = 'ABD' is not satisfied oracle does not return any rows.
    My question is how to overcome this or is it possible to change the built-in select statement of execute_query.

    Have you tried this :
    'upper(c1) like upper('''||'%'||:block_name.blk_col_name||'%'||''''));

  • Delete adjacent duplicates how to use in the below select statement

    hi i have a problem
    i am suing the below select statement
    SELECT    a~extno
              a~guid_lclic       " for next select
              e~ctsim
              e~ctsex
    *revised spec 3rd
              f~guid_pobj
              f~amnt_flt
              f~amcur
              f~guid_mobj
              e~srvll     "pk
              e~ctsty     "PK
              e~lgreg  "PK
      INTO TABLE gt_sagmeld
      FROM /SAPSLL/LCLIC  as a
      INNER JOIN /sapsll/tlegsv as e on elgreg = algreg
      inner join /sapsll/legcon as f on fguid_lclic = aguid_lclic   " for ccngn1 selection
      inner join /sapsll/corcts as g on gguid_pobj = fguid_pobj
                               where   a~extno in s_extno.
      sort gt_sagmeld by guid_lclic lgreg ctsty srvll GUID_POBJ GUID_MOBJ.
      delete adjacent duplicates from gt_sagmeld comparing guid_lclic lgreg ctsty srvll GUID_POBJ GUID_MOBJ .
    now i am confused how to use delete adjacent dupliacate and on which fields
    as first table /sal../lclic primary key is guid_lclic
    and it is joined to table
    legcon by guid_lclci ( not a primary key here)
    legcon primary key is guid_legcon
    and table 3 legsv by lgreg (pk here)
    table 3 has tow more primary key srvll and ctsty also
    NOW MY QUESTIO IS TAHT IS I USE ABOVE DELETE ADJACENT STATMENT IT FETCHES 20 LAKH RECORDS
    I WANT TO REDUCE IS LET ME KNOW ON WHAT fields i need to use delete adjacen duplicates
    or use comparing all fields?
    regards
    Arora

    hi sudha
    if u see my select statement is contains four Primary keys
    srvll
    lgreg
    ctsty
    guid_lclic
    but the next table connected to this table legcon is by guid_pobj and anothe table by guid_mobj
    and if i take this gt_sagmeld to another temp table and i find abt 10 lakh uniques guid_pobj
    similary 6 lakh guid_mobj so the next slect is hanpering because of this
    not COMING TO OUR POINT IF I SORT ONLY BY OUR PRIMARY KEYS NOT TAKING INTO ACCOUNT TEH GUID_POBJ AND GUID_MOBJ
    THE ENTRIES ARE VERY LESS BUT IF I TAKE INOT ACCCOUNT IN GT_SAGMELD THE ENTRIES ARE ABT 20 LAKH
    SO I AM NOT SURE WHETHER TO TAKNE GUID_POBJ AND GUID_MOBJ INOT ACCOUNT FOR DELECTING ADJACENT DUPLICATES?
    HENCE THE QUESTION OF ON WHICH FIRLD DELETE OR COMPARING ALL FIELDS I USE?

  • Help with a query using ISNULL and RTRIM in the same select statement

    What I'm trying to accomplish is to blend the following two statements together:
    ISNULL (EMail_Address, '') As Matrix_Member_Email
     AND
    RTRIM (EMail_Address) as Matrix_Member_EMail
    So that if the email address is NULL, it gets changed to blank and I still need to RTRIM the other entries and use the column header of Matrix_Member_Email.
    I've tried:
    ISNULL ((EMail_Address, '') (RTRIM (EMail_Address)) as Matrix_Member_EMail
    Any help would be greatly appreciated.
    Thank you,

    I'm sorry.  Here is the query:
    --Declare @EMail_Address nvarchar(100) = null
    Select SVAssociationID.R_ShortValue as MATRIX_AssociationID, Matrix_Modified_DT as Matrix_LastModified
      ,RTRIM (MLS_ID) As Matrix_MLS_ID
      , ISNULL(EMail_Address, '') AS MATRIX_member_Email
      ,RTRIM (EMail_Address) as Matrix_Member_EMail
      --,RTRIM( LTRIM(  ISNULL (@EMail_Address, '') ) ) as Matrix_Member_EMail 
      ,Last_Name AS MATRIX_LastName, Nickname AS MATRIX_NickName
      FROM    dbo.Agent_Roster_VIEW a
            LEFT JOIN dbo.select_values_VIEW SV ON a.Status_SEARCH = SV.ID
            LEFT JOIN dbo.select_values_VIEW BillType ON a.Bill_Type_Code_SEARCH = BillType.ID
      LEFT JOIN dbo.select_values_VIEW SVAssociationID ON A.Primary_Association_SEARCH = SVAssociationID.ID
    WHERE   Status_SEARCH IN (66,68) 
        Order by MLS_ID
    Results:
    MATRIX_AssociationID
    Matrix_LastModified
    Matrix_MLS_ID
    MATRIX_member_Email
    Matrix_Member_EMail
    MATRIX_LastName
    MATRIX_NickName
    STC
    09/02/14
    CCWILLI
    [email protected]
    [email protected]
    Williams                      
    Christine   
    STC
    09/12/14
    CCWORSL
    [email protected]
    [email protected]
    Worsley                       
    Charlie
    STC
    09/02/14
    CCYROBIN
    NULL
    Robinson       
    ECBR
    09/02/14
    CDABLACK
    [email protected]
    [email protected]
    Black                         
    Dale        
    STC
    09/02/14
    CDABRADY
    [email protected]
    [email protected]
    Brady                         
    David       
    Thank you,

  • [u][b]Performance Tuning Help[/b][/u] : Repeating HUGE Select Statement...

    I have a select statement that I am repeating 12 times with the only difference between them all being the date range that is grabbed. Essentially, I am grabbing the last 12 months of information from the same table here is a copy of one of the sections:
    (select
    a.account_id as account_id,
    a.company_id as company_id,
    a.account_number as acct_number,
    to_char(a.trx_date, 'YYYY/MM') as month,
    sum(a.amount_due_original) as amount
    from
    crystal.financial_vw a
    where
    a.cust_since_dt is not null
    and a.cust_end_dt is null
    and a.trx_date > add_months(sysdate, -13)
    and a.trx_date <= add_months(sysdate, -12)
         group by
              a.account_id,
              a.company_id,
              a.account_number,
              to_char(a.trx_date, 'YYYY/MM')) b
    I am now looking to do some tuning on this and was wondering if anyone has any suggestions. My initial thought was to use cursors or some sort of in memory storage to temporarily process the information into a pipe-delimited flat file.

    "Don't need:
    to_char(a.trx_date, 'YYYY/MM')"
    Are you sure?
    "Change to (just to make it easier to read):
    a.trx_date between add_months(sysdate, -13)
    and a.trx_date <= add_months(sysdate, -12)"
    What? That's not even valid syntax is it? Besides the fact that the BETWEEN operator is inclusive (i.e. > add_months(sysdate, -13) is not the same as between add_months(sysdate, -13) ...).
    "And be sure you have an index on:
    cust_since_dt, cust_end_dt, trx_date in financial_vw."
    What information did you base this conclusion on. Just because something is in the where clause doesn't mean you should automatically throw an index on it. What if 90% of the rows satisfy those null/not null criteria? What if there's only one year of data in this table? Are you certain an index would help pick out all the data for one month more efficiently than a full table scan?
    My immediate question was why are you breaking the data for each month up into separate subqueries like this at all? What is it that your doing with these subqueries that you don't believe can be accomplished with a single grouped query?

  • How to use case to replace the & in select statement

    Oracle version : 11.1.0.6.0
    RHEL
    Hi,
    I want to get a list of all expired and locked users but want to replace the "&" with and here is my code but it isn't working.
    Please some guidance, thanks.
    SQL> select username,
    2 case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    3 ELS 'no locked users'
    4 end) account_status
    5 from dba_users
    6 where account_status like 'EXPI%';
    case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    ERROR at line 2:
    ORA-00907: missing right parenthesis

    798188 wrote:
    thanks for the comments I corrected the ELSE and 'EPX%' but still same error :
    EOH> select username,
    2 case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    3 ELSE 'no locked users'
    4 end) account_status
    5 from dba_users
    6 where account_status like 'EXP%';
    case (account_status like 'EXP%' then 'EXPIRED AND LOCKED'
    ERROR at line 2:
    ORA-00907: missing right parenthesisFirst of all, you do not need a CASE statement.
    Did you see the where clause of your query??
    6 where account_status like 'EXP%';do you realise that your query will always return those records for which value of account_status column starts with 'EXP'?
    Why use CASE when it could be simply written like this :
    SELECT username,
           'EXPIRED AND LOCKED'
    FROM   dba_users
    WHERE  account_status LIKE 'EXP%'

  • Is possible to connect the 2 SELECTS statements into 1 for my stored procdr

    ELSE IF(@number = 2)
    BEGIN
    SELECT COUNT([outlet #])
    FROM OM
    WHERE [address] LIKE '%' + @address + '%';
    SELECT [outlet #], [name], address, [city/town], [postal code], telephone
    FROM OM
    WHERE address LIKE '%' + @address + '%'
    END
    Would it be possible to obtain the # of records and records in one shot
    Thank u for your time

    I suppose it depends on what you are really talking about, and what database and driver you are using.
    A statement can return more than one result set - see Statement.getMoreResults().

  • Alias of a table is not recognized in the nested select statement

    Hi,
    I have to migrate a query in DB2 to Oracle. The query in DB2 uses Fetch First Row, this is being replaced by Rownum as follow.
    DB2 Query
    SELECT S.ID AS ID, (SELECT VALUE_N FROM ARCHIVE.PACKAGE_PARAMETERS WHERE PACKAGE_ID = S.ID AND ROWNUM < 2) AS HUB_UNIT_ID FROM ARCHIVE.SHIPMENTS S WHERE S.ARCHIVE_ID < 900
    This is working fine
    Oracle Query
    SELECT S.ID AS ID, ( SELECT VALUE_N FROM (SELECT VALUE_N FROM ARCHIVE.PACKAGE_PARAMETERS WHERE PACKAGE_ID = S.ID ) tempTable where ROWNUM < 2) AS HUB_UNIT_ID FROM ARCHIVE.SHIPMENTS S WHERE S.ARCHIVE_ID < 900
    This is throwing error
    Error: ORA-00904: "S"."ID": invalid identifier
    Table Script
    CREATE TABLE "ARCHIVE"."PACKAGE_PARAMETERS" (
    "ID" NUMBER(19,0) NOT NULL ,
    "PACKAGE_ID" NUMBER(19,0) ,
    "DESCRIPTION" VARCHAR(128) ,
    "VALUE_N" VARCHAR(128) ,
    "NOTES" VARCHAR(512) )
    CREATE TABLE "ARCHIVE"."SHIPMENTS" (
    "ID" NUMBER(19,0) NOT NULL ,
    "CREATED" DATE DEFAULT SYSDATE NOT NULL ,
    "CI_INSURANCE_CHARGE" FLOAT DEFAULT 0 NOT NULL ,
    "ARCHIVE_ID" NUMBER(19,0) ,
    Please suggest me some alternative.
    Let me know if you need more information.
    Regards,
    Anil Sinha

    SELECT S.ID AS ID, ( SELECT VALUE_N FROM (SELECT VALUE_N FROM >>ARCHIVE.PACKAGE_PARAMETERS WHERE PACKAGE_ID = S.ID ) >>tempTable where ROWNUM < 2) AS HUB_UNIT_ID FROM >>ARCHIVE.SHIPMENTS S WHERE S.ARCHIVE_ID < 900
    This is throwing error
    Error: ORA-00904: "S"."ID": invalid identifierBecause S.ID is out of scope of correlated subquery. See:
    http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/queries8.htm#2054088
    Has to be formally:
    SELECT S.ID AS ID,
    (SELECT VALUE_N FROM ARCHIVE.PACKAGE_PARAMETERS WHERE PACKAGE_ID = S.ID AND ROWNUM < 2) AS HUB_UNIT_ID
    FROM ARCHIVE.SHIPMENTS S WHERE S.ARCHIVE_ID < 900
    But to be honest it's doubtful logic to use arbitrary row from multi-row subquery as the source for column value...
    Rgds.

  • Simplify the query/select statement help

    select distinct B_Billing_key, B_COMPANY_ID "Company" ,
    to_char(to_date('01/'||trim(substr(B_REPORT_PERIOD,5,2))||'/'||
    trim(substr(B_REPORT_PERIOD,1,4)),'DD/MM/YYYY'),'Month YYYY') "Billing Period",
    (nvl(SURCH_AMOUNT,0)+nvl(ADJUST_AMOUNT,0)+nvl(PI_AMOUNT,0))-(nvl(sum(AMOUNT),0))"Period_Balance",
    decode(sign((nvl(SURCH_AMOUNT,0)+nvl(ADJUST_AMOUNT,0)+nvl(PI_AMOUNT,0))-(nvl(sum(AMOUNT),0))),1, 'Yes'
    ,'No'
    )"outstanding_balance"
    p.PROGRAM_NAME_ID|| ' . ' ||p.PROGRAM_NAME_DESCR "Programname"
    FROM tuff_balance_view,MV_PROG_SURCH S,MV_PAYMENT_HOLDING H,MV_PROGRAM_DICT P where
    b_company_id = 'XYZ'
    and B_Billing_key=s.BILLING_KEY
    and S.PROGRAM_KEY = P.PROGRAM_KEY
    and P.PROGRAM_KEY= H.PROGRAM_KEY
    GROUP BY B_Billing_key,B_COMPANY_ID,B_REPORT_PERIOD,SURCH_AMOUNT,ADJUST_AMOUNT,PI_AMOUNT,PROG_SURCH_KEY,
    S.PROGRAM_KEY,p.PROGRAM_NAME_ID|| ' . ' ||p.PROGRAM_NAME_DESCR,AMOUNT
    order by B_Billing_key desc
    B_Billing_key is the primary key. I am looking for the output only one record for each biling perid. there are 2 programs for each billing period. if any of the program has period balance >0 then outstanding balance should be yes.
    The output from the above query is as below.
    biling_key company billing period period_balance outstandingbalance programname
    123 xyz January 2011 4 Yes ABC
    123 xyz January 2011 -5 NO DEF
    456 xyz February 2011 -3.0 No ABC
    456 xyz February 2011 2 Yes DEF
    Need the output as below from the above query. Can you please help to simplify query. If anyof theprogram having outstanding balance for that particular period show the outstandigbalance as yes. Else NO.
         company billing period outstandingbalance programname
    xyz January 2011 Yes ABC
    xyz February 2011 Yes DEF
    Thanks,
    vi

    <font face="courier">
    <font color="green">-- this just simulates the results of your original query - meant just to play with until everything works</font>
    <br>
    with
    your_query as
    (select 123 billing_key,'xyz' company,'Jan 2011' billing_period,'No' outstandingbalance,'ABC' programname from dual union all
     select 123,'xyz','Jan 2011','Yes','DEF' programname from dual union all
     select 123,'xyz','Jan 2011','No','GHI' programname from dual union all
    <font color="green">--comment out some data row to see what happens when having just 5 programs
    --</font> select 123,'xyz','Jan 2011','No','JKL' programname from dual union all
     select 123,'xyz','Jan 2011','Yes','MNO' programname from dual union all
     select 123,'xyz','Jan 2011','No','PQR' programname from dual union all
     select 456,'xyz','Feb 2011','No','ABC' programname from dual union all
     select 456,'xyz','Feb 2011','No','DEF' programname from dual union all
     select 456,'xyz','Feb 2011','No','GHI' programname from dual union all
     select 456,'xyz','Feb 2011','No','JKL' programname from dual union all
     select 456,'xyz','Feb 2011','No','MNO' programname from dual union all
     select 456,'xyz','Feb 2011','No','PQR' programname from dual
    <font color="green">-- end of generated data
    -- the "real" query follows
    </font>
    <br>
    <font color="blue">
    select billing_key,
           company,
           billing_period,
           case when programname = ',,,,,' then 'No' else 'Yes' end outstandingbalance,
           case when programname != ',,,,,'
                then trim(',' from replace(replace(replace(programname,',',',~'),'~,'),'~'))
           end programname
      from (select billing_key,company,billing_period,'Yes' outstandingbalance,
                   max(case when outstandingbalance = 'Yes' and programname = 'ABC' then 'ABC' end) || ',' ||
                   max(case when outstandingbalance = 'Yes' and programname = 'DEF' then 'DEF' end) || ',' ||
                   max(case when outstandingbalance = 'Yes' and programname = 'GHI' then 'GHI' end) || ',' ||
                   max(case when outstandingbalance = 'Yes' and programname = 'JKL' then 'JKL' end) || ',' ||
                   max(case when outstandingbalance = 'Yes' and programname = 'MNO' then 'MNO' end) || ',' ||
                   max(case when outstandingbalance = 'Yes' and programname = 'PQR' then 'PQR' end) programname
              from (
                     <font color="green">--your_query here</font><br>
    <font color="red">
                    select distinct B_Billing_key, B_COMPANY_ID "Company" ,
                    to_char(to_date('01/'||trim(substr(B_REPORT_PERIOD,5,2))||'/'||
                    trim(substr(B_REPORT_PERIOD,1,4)),'DD/MM/YYYY'),'Month YYYY') "Billing Period",
                    (nvl(SURCH_AMOUNT,0)+nvl(ADJUST_AMOUNT,0)+nvl(PI_AMOUNT,0))-(nvl(sum(AMOUNT),0))"Period_Balance",
                    decode(sign((nvl(SURCH_AMOUNT,0)+nvl(ADJUST_AMOUNT,0)+nvl(PI_AMOUNT,0))-(nvl(sum(AMOUNT),0))),1, 'Yes'
                    ,'No'
                    )"outstanding_balance"
                    p.PROGRAM_NAME_ID|| ' . ' ||p.PROGRAM_NAME_DESCR "Programname"
                    FROM tuff_balance_view,MV_PROG_SURCH S,MV_PAYMENT_HOLDING H,MV_PROGRAM_DICT P where
                    b_company_id = 'XYZ'
                    and B_Billing_key=s.BILLING_KEY
                    and S.PROGRAM_KEY = P.PROGRAM_KEY
                    and P.PROGRAM_KEY= H.PROGRAM_KEY
                    GROUP BY B_Billing_key,B_COMPANY_ID,B_REPORT_PERIOD,SURCH_AMOUNT,ADJUST_AMOUNT,PI_AMOUNT,PROG_SURCH_KEY,
                    S.PROGRAM_KEY,p.PROGRAM_NAME_ID|| ' . ' ||p.PROGRAM_NAME_DESCR,AMOUNT
                    order by B_Billing_key desc
    </font>
             group by billing_key,company,billing_period
    </font>
    </font>
    Regards
    Etbin
    your simplified query should look somehow like below
    you didn't provide table structures and sample data so:
    aliases were assigned arbitrarily
    using sum(nvl(s.SURCH_AMOUNT,0) + nvl(s.ADJUST_AMOUNT,0) + nvl(s.PI_AMOUNT,0) - nvl(v.AMOUNT,0)) is most certainly wrong
    select v.B_Billing_key,
           v.B_COMPANY_ID company,
           to_char(to_date(substr(v.B_REPORT_PERIOD,1,6),'yyyymm'),'Month yyyy') billing_period
           case when sum(nvl(s.SURCH_AMOUNT,0) + nvl(s.ADJUST_AMOUNT,0) + nvl(s.PI_AMOUNT,0) - nvl(v.AMOUNT,0)) > 0
                then 'Yes'
                else 'No'
           end outstandingbalance,
           p.PROGRAM_NAME_ID programname
      FROM tuff_balance_view v,
           MV_PROG_SURCH S,
           MV_PAYMENT_HOLDING H,
           MV_PROGRAM_DICT P
    where v.B_COMPANY_ID = 'U-7052-C'
       and v.B_Billing_key = s.BILLING_KEY
       and S.PROGRAM_KEY = P.PROGRAM_KEY
       and P.PROGRAM_KEY = H.PROGRAM_KEY
    group by B_Billing_key,
              B_COMPANY_ID,
              to_char(to_date(substr(B_REPORT_PERIOD,1,6),'yyyymm'),'Month yyyy'),
              p.PROGRAM_NAME_IDEdited by: Etbin on 2.3.2011 22:44

  • Please help me in completing the remaining select statements

    1.     Based on the selection made from the selection screen, get PO Number (EBELN), Company Code (BUKRS), Vendor (LIFNR), Vendor Name (ZNAME1), PO_Date (AEDAT), PO Creator (ERNAM) from EKKO table
    2.     For the corresponding PO number (EBELN) from EKKO table, get  PO Item Text (TXZ01) from EKPO table
    3.     For the corresponding PO number (EBELN) from EKKO table, get PO_Iten_No(EBELP), from EKPO table
    4.     For the corresponding PO number (EBELN) from EKKO table get the GR_Posting Dt(BUDAT) from EKBE table when PO History cat (HCT-EKBE) is equal to “E” .
    a)     For getting the first GR Posting date and first Invoice posting date, sort the column (BUDAT) of table, get the 1st GR_Posting_dt(BUDAT) when PO History cat (HCT-EKBE) is equal to “E”  AND get the 1st Inv_Posting_Dt(BUDAT) when PO when PO History cat (HCT-EKBE) is equal to “Q” For this corresponding invoice date (when when PO History cat (HCT-EKBE) is equal to “Q” ) get the invoice number (BELNR) .
    b)     IF  1ST GR_POSTING_DATE < 1ST INVOICE_POSTING_DATE, DO NOT DISPLAY THE RESULT
    5.     For the corresponding PO number (EBELN) from EKKO table and Invoice_No (BELNR) (from step4,a),get Inv_Creator (ERNAM), Inv_Amount(REEWR), Currency (WAERS) from EKBE table when PO History cat (Hct-EKBE) is equal to “Q”
    6.     For the corresponding PO number (EBELN) from EKKO table and invoice No (BELNR) from EKBE, get the Reference Document Number (XBLNR) from RSEG table.
    for this i have written
    select ebeln bukrs lifnr zname1 aedat ernam
                                          from
                                          ekko into
                                          table itab_ekko
                                          where bukrs = p_bukrs
                                          AND   lifnr IN so_lifnr
                                          AND   aedat IN so_aedat
                                          AND   ekgrp IN so_ekgrp
                                          AND   ernam IN so_ernam.
      IF NOT itab_ekko[] IS INITIAL.
        SELECT ebeln  ebelp txz01 FROM ekpo into  TABLE
                        itab_ekpo
                        FOR ALL ENTRIES IN  itab_ekko
                        WHERE ebeln = itab_ekko-ebeln.
      ENDIF.
       IF NOT itab_ekko[] IS INITIAL.
        SELECT ebeln budat   FROM ekbe into  TABLE
                        itab_ekbe
                        FOR ALL ENTRIES IN  itab_ekko
                        WHERE ebeln = itab_ekko-ebeln
                              AND bewtp = 'E'
                              AND bewtp = 'Q'.
      ENDIF.
      SORT itab_ekbe by budat.
      read table itab_ekbe index 1."to get first invoice posting date.
    please halp me in completing remaining code

    Hi
    select ebeln bukrs lifnr zname1 aedat ernam
    from
    ekko into
    table itab_ekko
    where bukrs = p_bukrs
    AND lifnr IN so_lifnr
    AND aedat IN so_aedat
    AND ekgrp IN so_ekgrp
    AND ernam IN so_ernam.
    IF NOT itab_ekko[] IS INITIAL.
    SELECT ebeln ebelp txz01 FROM ekpo into TABLE
    itab_ekpo
    FOR ALL ENTRIES IN itab_ekko
    WHERE ebeln = itab_ekko-ebeln.
    ENDIF.
    IF NOT itab_ekko[] IS INITIAL.
    SELECT ebeln budat FROM ekbe into TABLE
    itab_ekbe
    FOR ALL ENTRIES IN itab_ekko
    WHERE ebeln = itab_ekko-ebeln
    AND bewtp = 'E'
    AND bewtp = 'Q'.
    ENDIF.
    SORT itab_ekbe by budat.
    read table itab_ekbe with key HCT = 'E'."to get first GR posting date.
    loop at itab_ekbe where HCT = 'Q'.
    addped itab_ekbe to temp_itab_ekbe.
    endloop.
    read table temp_itab_ekbe index 1. " to get first invoice posting date
    if itab_ekbe-budat < temp_itab_ekbe-budat.
    exit.
    endif.
    *point 5
    select ERNAM REEWR WAERS from EKBE
    into corresponding fields of table i_temp1_ekbe
    for all entries in itab_ekko
    where ebeln = itab_ekko-ebeln.
    loop at i_temp1_ekbe.
    loop at temp_itab_ekbe where ebeln = i_temp1_ekbe-ebeln and
                        belnr = i_temp1_ekbe-belnr.
    if sy-subrc <> 0.
    delete i_temp1_ekbe.
    endif.
    endloop.
    endloop.
    *Point 6
    select XBLNR form rseg
    into corresponding fields of table itab_rseg
    for all entries in i_temp1_ekbe
    where ebeln = i_temp1_ekbe-ebeln and
          belnr = i_temp1_ekbe-belnr.
    regards
    Aditya

  • I would just like to thank Verizon for the huge selection of Windows phones available.

    WHEN ARE YOU GOING TO OFFER MORE HARDWARE OPTIONS FOR WINDOWS PHONES? 
    I MAY JUST HAVE TO MOVE TO ATT TO GET A DECENT PHONE!!!

        Good morning and thank you for all of your feedback & responses.  I understand you're anxious to get phones that will actually meet your needs.  We certainly do not want you to leave VZW due to not having the phones you want. We are constantly rolling out new devices so please don't lose hope.  I'm sure we'll having something to meet your needs soon.  You can sign up for e-mail alerts about new products and services to ensure you are notified as soon as new devices roll out.  You can sign up at the following link: http://bit.ly/dDumph
    Thank you,
    VanessaS_VZW
    Follow us on Twitter at www.twitter.com/vzwsupport

  • How to find the number of fetched lines from select statement

    Hi Experts,
    Can you tell me how to find the number of fetched lines from select statements..
    and one more thing is can you tell me how to check the written select statement or written statement is correct or not????
    Thanks in advance
    santosh

    Hi,
    Look for the system field SY_TABIX. That will contain the number of records which have been put into an internal table through a select statement.
    For ex:
    data: itab type mara occurs 0 with header line.
    Select * from mara into table itab.
    Write: Sy-tabix.
    This will give you the number of entries that has been selected.
    I am not sure what you mean by the second question. If you can let me know what you need then we might have a solution.
    Hope this helps,
    Sudhi
    Message was edited by:
            Sudhindra Chandrashekar

  • How to use a table name in the select statement using a variable?

    Hi Everybody,
                       I got a internal table which has a field or a variable that gets me some tables names. Now I need to retrieve the data from all these tables in that field dynamically at runtime. So could you suggest me a way out to use the select query which uses this variable as a table ?
    Regards,
    Mallik.

    Hi all,
    Actually i need some more clarification. How to use the same select statement, if i've to use the tabname in the where clause too?
    for ex : select * from (tab_name) where....?
    Can we do inner join on such select statements? If so how?
    Thanks & Regards,
    Mallik.

  • Select statement having internal table in the wher clause...

    Hi,
    will all the entry from the internal i_crhd will be pased to the next select statement in the sample code below without looping? Thanks  a lot!
    REFRESH i_crhd.
       SELECT objid vgwts
         FROM crhd INTO CORRESPONDING FIELDS OF TABLE i_crhd
         WHERE arbpl IN s_arbpl.
    pulling the cost centre linked to the resource
       IF sy-subrc EQ 0 AND i_crhd[] IS NOT INITIAL.
         REFRESH i_crco.
         SELECT kostl FROM crco
           INTO CORRESPONDING FIELDS OF TABLE i_crco
           FOR ALL ENTRIES IN i_crhd
           WHERE objid = i_crhd-objid.
         IF sy-subrc EQ 0.
          do nothing.
         ENDIF.
       ENDIF.

    Hi,
    The code looks fine..u can very well go ahead with the code...
    Wht all other ABAPers said about FOR ALL ENTRIES is absolutely rite.
    But i have a small suggestion...
    Why cant u use a INNNER JOIN for those 2 queries...
    like...
    SELECT cr~objid
                 cr~vgwts
                 co~kostl
    FROM crhd AS cr INNER JOIN
              crco AS co
    ON  crobjid = coobjid
    INTO TABLE i_crco
    WHERE cr~arbpl IN s_arbpl.
    Now in i_crco 3 fields will be thr
    objid, vgwts, kostl.....this one can change as u wish...
    Why i suggest not to ue FOR ALL ENTRIES is sometimes it is a performanc killer....in this code i feel this will do fine...
    Please let me know if u feel not ok with this code...
    Reward if found useful...
    Regards,
    ABAPer 007.

Maybe you are looking for

  • Error While Deploying MYDemo application in ias 10.1.2

    I deployed the MYDemo in iAS 10.1.2 while calling URL (executing the app) http://xxx.abc.org:7777/HRDemo/pages/EmployeesTable.jspx I get the following error ? Any Clue? java.lang.NoClassDefFoundError: javax/servlet/jsp/el/VariableResolver     at java

  • Will I lose my unlimited data if I upgrade from an Android to an iphone?

    I am due for an upgrade in a few months and I am really leaning towards the new iphone. My question is...I have an unlimited family data plan, if I switch to an iphone will I be forced to change data plans? I work in litigation and I use a lot of dat

  • System.loadLibrary works in 1.3.1 but not in 1.2.2

    I Have a basic hello world application on Sun Solaris 7 that calls C++ from Java using JNI. The code works well on 1.3.1, but when I recompile it under 1.2.2 and run I get the following exception when I call System.loadLibrary to load my shared libra

  • Convertion of Danish characters in UTF8

    Hello everybody, I am facing a strange situation on my project. We are not able to convert a special/national Danish character into UTF (e.g. convert "JordbÿŠr" to "Jordbær JordbÿŠr"). I am using the built-in function CONVERT and an Oracle 9.2 data

  • Java for OS X 2012-005 causing crash?

    I think the new update is causing illustrator to crash. Is that possible?  Whenever I move an object in illustrator, my machine freezes up. The only change to my system has been the java update.