Program without inner join..

hi frds...
i need help in the programming without using the inner join and views,
i want to take  data from 2  different table without join and views, give some sample codes or links
it will help to us.
by
pari..
Edited by: Alvaro Tejada Galindo on Feb 22, 2008 12:18 PM

Hi pari,
         This is the sample code 'for all entries'
in alv report.
TYPE-POOLS:SLIS.
DATA:BODY TYPE SLIS_T_FIELDCAT_ALV,
     HEADER TYPE SLIS_FIELDCAT_ALV.
DATA:BEGIN OF ITAB OCCURS 0,
     MATNR LIKE MARA-MATNR,
     ERSDA LIKE MARA-ERSDA,
     MEINS LIKE MARA-MEINS,
     END OF ITAB.
DATA:BEGIN OF ITAB1 OCCURS 0,
     MATNR LIKE MAKT-MATNR,
     MAKTX LIKE MAKT-MAKTX,
     END OF ITAB1.
DATA:BEGIN OF ITAB2 OCCURS 0,
     MATNR LIKE MARA-MATNR,
     ERSDA LIKE MARA-ERSDA,
     MEINS LIKE MARA-MEINS,
     MAKTX LIKE MAKT-MAKTX,
    END OF ITAB2.
SELECT-OPTIONS:SMATNR FOR ITAB-MATNR.
PERFORM FETCH_MARA.
PERFORM FETCH_MAKT.
PERFORM READ.
PERFORM FIELDCATALOG.
PERFORM ALVDISPLAY.
*&      Form  FETCH_MARA
form FETCH_MARA .
SELECT MATNR ERSDA MEINS FROM MARA INTO TABLE ITAB WHERE MATNR IN SMATNR
endform.                    " FETCH_MARA
*&      Form  FETCH_MAKT
form FETCH_MAKT .
IF ITAB[] IS NOT INITIAL.
SELECT MATNR MAKTX FROM MAKT INTO TABLE ITAB1 FOR ALL ENTRIES IN ITAB
WHERE MATNR = ITAB-MATNR.
ENDIF.
endform.                    " FETCH_MAKT
FORM READ.
LOOP AT ITAB.
READ TABLE ITAB1 WITH KEY MATNR = ITAB-MATNR.
IF SY-SUBRC = 0.
MOVE ITAB-MATNR TO ITAB2-MATNR.
MOVE ITAB-ERSDA TO ITAB2-ERSDA.
MOVE ITAB-MEINS TO ITAB2-MEINS.
MOVE ITAB1-MAKTX TO ITAB2-MAKTX.
ENDIF.
APPEND ITAB2.
ENDLOOP.
ENDFORM.
*&      Form  FIELDCATALOG
      text
-->  p1        text
<--  p2        text
form FIELDCATALOG .
HEADER-COL_POS = '1'.
HEADER-SELTEXT_L = 'MATNR1'.
HEADER-TABNAME = 'ITAB2'.
HEADER-FIELDNAME = 'MATNR'.
APPEND HEADER TO BODY.
CLEAR HEADER.
HEADER-COL_POS = '2'.
HEADER-SELTEXT_L = 'ERSDA'.
HEADER-TABNAME = 'ITAB2'.
HEADER-FIELDNAME = 'ERSDA'.
APPEND HEADER TO BODY.
CLEAR HEADER.
HEADER-COL_POS = '3'.
HEADER-SELTEXT_L = 'MEINS'.
HEADER-TABNAME = 'ITAB2'.
HEADER-FIELDNAME = 'MEINS'.
APPEND HEADER TO BODY.
CLEAR HEADER.
HEADER-COL_POS = '4'.
HEADER-SELTEXT_L = 'MAKTX'.
HEADER-TABNAME = 'ITAB2'.
HEADER-FIELDNAME = 'MAKTX'.
APPEND HEADER TO BODY.
CLEAR HEADER.
endform.                    " FIELDCATALOG
*&      Form  ALVDISPLAY
form ALVDISPLAY .
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
  I_INTERFACE_CHECK              = ' '
  I_BYPASSING_BUFFER             =
  I_BUFFER_ACTIVE                = ' '
   I_CALLBACK_PROGRAM             = 'ZALVREPORT'
  I_CALLBACK_PF_STATUS_SET       = ' '
  I_CALLBACK_USER_COMMAND        = ' '
  I_STRUCTURE_NAME               =
  IS_LAYOUT                      =
   IT_FIELDCAT                    = BODY
  IT_EXCLUDING                   =
  IT_SPECIAL_GROUPS              =
  IT_SORT                        =
  IT_FILTER                      =
  IS_SEL_HIDE                    =
  I_DEFAULT                      = 'X'
  I_SAVE                         = ' '
  IS_VARIANT                     =
  IT_EVENTS                      =
  IT_EVENT_EXIT                  =
  IS_PRINT                       =
  IS_REPREP_ID                   =
  I_SCREEN_START_COLUMN          = 0
  I_SCREEN_START_LINE            = 0
  I_SCREEN_END_COLUMN            = 0
  I_SCREEN_END_LINE              = 0
IMPORTING
  E_EXIT_CAUSED_BY_CALLER        =
  ES_EXIT_CAUSED_BY_USER         =
  TABLES
    t_outtab                       = ITAB2
EXCEPTIONS
  PROGRAM_ERROR                  = 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.
ENDFORM.

Similar Messages

  • Report -without inner join

    hi,
    this is report i created in this i used two tables and used inner join to make relation b/t these tables...
    so my doubt is ..if we r using three diff.. tables without inner join we can do this report ......if u can.. plz can u show me wit example...
    TABLES: VBAP,VBAK.
    DATA :  BEGIN OF ITAB OCCURS 0,
      vbeln like VBAK-VBELN, "Sales Document No.
      kunnr like VBAK-KUNNR,"customerno
      VKORG like VBAK-VKORG,"salesorg
      audat like VBAK-AUDAT,"documentdate
      matnr like VBAP-MATNR,"materialno
      netwr like VBAK-NETWR,"netamount
      end of itab.
    selection-screen : begin of block a1. "SCREEN 500 AS WINDOW..
    select-options : KUNNR for VBAK-kunnr.
    select-options : VKORG FOR VBAK-vkorg.
    select-options : AUDAT for VBAK-audat.
    select-options : MATNR for VBAP-matnr.
    select-options : NETWR FOR VBAK-netwr.
    SELECTION-SCREEN :end of block a1.
    SELECT vbakvbeln vbakkunnr vbakvkorg vbakaudat  vbapmatnr vbaknetwr into table itab from vbak
      INNER JOIN vbap on vbakvbeln = vbapvbeln
      where
      VBAK~kunnr in kunnr
    AND
       VBAK~VKORG in VKORG
    and vbak~audat in audat
    AND vbap~matnr in matnr.
      write sy-dbcnt.
    write:/ 'CUSTOMERNO' ,20 'SALESORG',40 'DOCUMENTDATE',60 'MATERIAL',80 'NETAMOUNT'.
    ULINE.
    loop at itab.
        write :/ itab-kunnr,20 itab-vkorg, 40 itab-audat, 60 itab-matnr, 80 itab-netwr.
        endloop.
    thanx
    bhanu

    Refer to this psuedo code
    SELECT vbak~vbeln vbak~kunnr vbak~vkorg vbak~audat vbap~matnr vbak~netwr into table itab from vbak
    INNER JOIN vbap on vbak~vbeln = vbap~vbeln
    where
    VBAK~kunnr in kunnr
    AND
    VBAK~VKORG in VKORG
    and vbak~audat in audat
    AND vbap~matnr in matnr.
    SELECT vbak~vbeln vbak~kunnr vbak~vkorg vbak~audat vbak~netwr vbak~matnr
                 into table itab
    from vbak
    where kunnr in kunnr....
    select vbeln matnr
          into table itab2
    from vbap
    for all entries in itab
    where vbap~matnr = itab~matnr....
    loop at itab assigning <fs_itab>.
    read table itab2 into wa_itab2 with key vbeln = <fs_itab>-vbeln
                                                             matnr = <fs_itab>-matnr.
    " your move's to final table
    endloop.

  • Need help with program for Inner join

    Hello Experts,
    I need to create a list from table sbook containing booking number (sbook-bookid), customer number (sbook-customid), customer name (scustom-name) , customer class (sbook-class) and ticket price (sflight-price). I am new to ABAP and am very confused I tried reading up some examples and came up with the attached program
    Attached is my program for inner join
    Kindly Help
    Thanks Su

    Hi Su K
              You May use key fields , Foreign keys for joining , Here
    SELECT SBOOK~BOOKID SBOOK~CUSTOMID SBOOK~CLASS
      SCUSTOM~ID SCUSTOM~NAME  FROM SFILGHT
       INNER JOIN SBOOK ON   SBOOK~CARRID EQ SFILGHT~CARRID
                                              SBOOK~CONNID EQ SFILGHT~CONNID
                                              SBOOK~FLDATE EQ SFILGHT~FLDATE
      INNER JOIN SCUSTOM ON SCUSTOM~ID = SBOOK~ID

  • How to use BKPF and BSEG without using  inner join

    Hi,
    can anybody plz tell me the logic about how to complete the report without inner joining bkpf and bseg.Is this report can be really be made by using only bseg,bkpf ?
    *selection-criteria .
    BKPF-USNAM
    BKPF-CPUDT
    BKPF-BUDAT
    BKPF-GJAHR
    BKPF-BUKRS
    BKPF-BELNR
    BSEG-SAKNR
    BSEG-LIFNR
    BSEG-KUNNR
    BSEG-ZUONR
    BSEG-SGTXT
    Output fields required.
    BKPF-BUKRS
    BKPF-BELNR
    BKPF-GJAHR
    BKPF-MONAT
    BKPF-BLART
    BKPF-CPUDT
    BKPF-BLDAT
    BKPF-BUDAT
    BKPF-USNAM
    BKPF-XBLNR
    BKPF-STBLG
    BKPF-BKTXT
    BKPF-WAERS
    BKPF-KURSF
    BSEG-BUZEI
    BSEG-BSCHL
    BSEG-KOART
    BSEG-UMSKZ
    BSEG-SHKZG
    BSEG-MWSKZ
    BSEG-PSWBT
    BSEG-PSWSL
    BSEG-DMBTR
    BSEG-SAKNR
    BSEG-HKONT
    BSEG-KOSTL
    BSEG-KUNNR
    BSEG-VBELN
    BSEG-LIFNR
    BSEG-ANLN1
    BSEG-ANLN2
    BSEG-AUFNR
    BSEG-MATNR
    BSEG-MENGE
    BSEG-MEINS
    BSEG-ZUONR
    BSEG-SGTXT
    Thanks,
    Rahman
    Moderator Message: Please search before posting your question. Thread locked.
    Edited by: Suhas Saha on Jan 31, 2012 3:07 PM

    Quick question - what's preventing your from using built-in functions?
    I suggest mapping your Time Dim to a standard Time Dim and work with it - you'll save a lot more time. You'd even need this table if you wanted to work around Todate/AGO.
    It is possible to model this functionality w/o functions, but it'll be time consuming task, similar to using Time Series Wizard in Siebel Analytics. You'll need to build aliases and views along with some complex joins (such as TIME.KEY=TIME.KEY-365), introduce a bunch of variables to control. Also, you might not get much flexibility in terms of years. For each year, you'd need an alias table. (TIME_DIM_PY).

  • INNER JOIN Where to Use

    hi,
    is there any criteria determining the usage of <b>inner join</b>, i.e no. of rows to be retrieved from the tables... can we avoid using <b>inner joins</b>...
    i hope i have cleared my query.....
    thanx in advance
    abhishek suppal

    Abhishek,
    Inner joins (when used correctly) are a GOOD thing.
    The criteria as to when to use an inner join is that the tables must have common values. For example let's look at VBAK and VBAP. The both share VBELN as their common link. And becasue VBELN is part of their keys it is very good to use an inner join if you need data from both tables.
    eg. We want to get sales Order data into TBL_SALESORDER from VBAK / VBAP
    Without Inner Join you can do it something like this:
    data: begin of tbl_vbak occurs 0,
            vbeln  like vbak-vbeln,
            kunnr  like vbak-kunnr,
            audat  like vbak-audat,
          end of tbl_vbak.
    data: begin of tbl_vbap occurs 0,
            vbeln  like vbap-vbeln,
            posnr  like vbap-posnr,
            matnr  like vbap-matnr,
            zmeng  like vbap-zmeng,
          end of tbl_vbap.
    data: begin of tbl_salesorder occurs 0,
            vbeln  like vbak-vbeln,
            kunnr  like vbak-kunnr,
            audat  like vbak-audat,
            posnr  like vbap-posnr,
            matnr  like vbap-matnr,
            zmeng  like vbap-zmeng,
          end of tbl_salesorder.
    data: l_index like st-tabix.
    select vbeln kunnr audat
           into table tbl_vbak
           from vbak.
    check sy-subrc eq 0.
    select vbeln posnr matnr zmeng
           into table tbl_vbap
           from vbap
           for all entries in tbl_vbak
           where vbeln = tbl_vbak-vbeln.
    check sy-subrc eq 0.
    sort tbl_vbap by vbeln posnr.
    loop at tbl_vbak.
      read table tbl_vbap with key vbeln = tbl_vbak-vbeln
                                   binary search.
      if sy-subrc eq 0.
        l_index = sy-tabix.
        loop at tbl_vbap from l_index.
          if tbl_vbak-vbeln ne tbl_vbap-vbeln.
            exit.
          endif.
          clear tbl_salesorder.
          move-corresponding tbl_vbak to tbl_salesorder.
          move-corresponding tbl_vbap to tbl_salesorder.
          append tbl_salesorder.
        endloop.
      endif.
    endloop.
    With an inner join you can do it like this:
    data: begin of tbl_salesorder occurs 0,
            vbeln  like vbak-vbeln,
            kunnr  like vbak-kunnr,
            audat  like vbak-audat,
            posnr  like vbap-posnr,
            matnr  like vbap-matnr,
            zmeng  like vbap-zmeng,
          end of tbl_salesorder.
    select vbak~vbeln vbak~kunnr vbak~audat
           vbap~posnr vbap~matnr vbap~zmeng
           into table tbl_salesorder
           from vbak
           inner join vbap
           on vbak~vbeln = vbap~vbeln.
    In the above example it is more efficient to use the inner join because we hit the database once. Without the inner join we hit the database twice (once for the VBAK info and the other time for the VBAP info).
    If you want to restrict the data you canuse a select option or parameter like any other SELECT statement. What you probably would not do is use the "UP TO X ROWS" clause as this will essentially limit the data from VBAP not VBAK.
    I know that this example is a very simple one and we were luck to be able to "join" the tables using their primary keys. If the "join" between the tables are not by their primary keys or by an index then you may need to reconsider using an inner join and this will cause performance problems.
    I hope this makes some sense. Let me know if you need more info, alternatively take a look at the SAP Help (F1) for SELECT.
    Cheers,
    Pat.
    PS. Kindly assign Reward Points to the posts you find helpful.

  • Avoid inner join

    Hi experts,
    Iam using following inner join statement. But it is taking lot of time.
    Could any one please tell me how can i combined two tables without inner join.
    Can i use loop statement?
    SELECT a~aufnr
             a~posnr
             a~etenr
             a~werks
             a~matnr
             a~j_3asize
             a~j_4krcat
             a~vdatu
             a~menge
             a~j_3aresm
             INTO TABLE t_j3abdsi
             FROM j_3abdsi AS a INNER JOIN mara AS b
             ON a~matnr = b~matnr
             WHERE b~mtart IN so_mtart AND
                   a~matnr IN so_matnr AND
                   a~werks IN so_werks AND
                   a~vdatu IN so_vdatu.
    thanks
    p.

    Hi Priya,
    Your problem probably lies in the fact that your select statement is not picking an appropriate index for table t_j3abdsi. My system (version 4.7) does not have this table, so I cannot advice you on how to use an index. You will have to arrange the criteria in your where clause to pick the best index or create an index that suits your select. Again creating an index will increase your database size so you will have to weight your pros and cons. If you have an available index, try and make some of your select-options used on the where condition for table t_j3abdsi mandatory.
    Your question however was to convert the code from a join to a for all entries. You can write a code like the one I am showing you.
    TYPES: BEGIN OF ty_t_j3abdsi,
             matnr    TYPE t_j3abdsi-matnr   ,
             aufnr    TYPE t_j3abdsi-aufnr   ,
             posnr    TYPE t_j3abdsi-posnr   ,
             etenr    TYPE t_j3abdsi-etenr   ,
             werks    TYPE t_j3abdsi-werks   ,
             j_3asize TYPE t_j3abdsi-j_3asize,
             j_4krcat TYPE t_j3abdsi-j_4krcat,
             vdatu    TYPE t_j3abdsi-vdatu   ,
             menge    TYPE t_j3abdsi-menge   ,
             j_3aresm TYPE t_j3abdsi-j_3aresm,
             del(1)   TYPE c,
           END OF ty_t_j3abdsi,
           BEGIN OF ty_mara,
             matnr TYPE mara-matnr,
           END OF ty_mara.
    DATA: w_index         TYPE                 sy-tabix    ,
          w_found(1)      TYPE                 c           ,
          w_t_j3abdsi     TYPE                 ty_t_j3abdsi,
          t_t_j3abdsi     TYPE        TABLE OF ty_t_j3abdsi,
          t_t_j3abdsi_tmp TYPE        TABLE OF ty_t_j3abdsi,
          t_mara          TYPE HASHED TABLE OF ty_mara
            WITH UNIQUE KEY matnr.
    * You will have to adjust the where clause on this select to pick the best index
    SELECT matnr
           aufnr
           posnr
           etenr
           werks
           j_3asize
           j_4krcat
           vdatu
           menge
           j_3aresm
      FROM j_3abdsi
      INTO TABLE t_t_j3abdsi
      WHERE matnr IN so_matnr
      AND   werks IN so_werks
      AND   vdatu IN so_vdatu.
    IF sy-subrc EQ 0.
      t_t_j3abdsi_tmp[] = t_t_j3abdsi[].
      SORT t_t_j3abdsi_tmp BY matnr.
      DELETE ADJACENT DUPLICATES FROM t_t_j3abdsi_tmp COMPARING matnr.
      SELECT matnr
        FROM mara
        INTO TABLE t_mara
        FOR ALL ENTRIES IN t_t_j3abdsi_tmp
        WHERE matnr EQ t_t_j3abdsi_tmp-matnr
        AND mtart   IN so_mtart.
      LOOP AT t_t_j3abdsi INTO w_t_j3abdsi.
        w_index = sy-tabix.
        AT NEW matnr.
          READ TABLE t_mara WITH KEY matnr = w_t_j3abdsi-matnr
                                     TRANSPORTING NO FIELDS.
          IF sy-subrc EQ 0.
            w_found = 'X'.
          ELSE.
            CLEAR w_found.
          ENDIF.
        ENDAT.
        IF w_found IS INITIAL.
          w_t_j3abdsi-del = 'X'.
          MODIFY t_t_j3abdsi FROM w_t_j3abdsi INDEX w_index
            TRANSPORTING
              del.
        ENDIF.
      ENDLOOP.
      DELETE t_t_j3abdsi WHERE del EQ 'X'.
    ENDIF.

  • Any differences between inner join and join without any keyword(inner join)

    Are there any differences between following two join statements?
    Join Statement 1:
    select column1, column2 from table1 t1, table2 t2 where t1.t1Key=t2.t2Key;
    Join Statement 2:
    select column1, column2 from table1 t1 inner join table2 t2 on t1.t1Key = t2.t2Key;
    Thanks for your reply.
    Kevin

    Hi, Kevin,
    user13531850 wrote:
    Are there any differences between following two join statements?To the system, those two are equivalent. They will produce exactly the same results, and will probably result in the same execution plan, so they will be equally efficient.
    Some people find it easier to read and understand one rather than the other. Personally, I find the ANSI syntax (JOIN ... ON ...) easier to understand. The join conditions that apply to each table are listed right next to the table; you don't have to hunt through a long WHERE clause to find them. Also, it makes debugging easier. If you forget the join condition, then you get a syntax error, pinpointing where you forgot the join condition. Using the old syntax, if you forget a join condition, you get a cross-join, and it may not be obvious that any error occurred, but even if you do notice the mistake, you have no clue where it happened.
    Join Statement 1:
    select column1, column2 from table1 t1, table2 t2 where t1.t1Key=t2.t2Key;This is the old join syntax. It works in all versions of Oracle (so far).
    Join Statement 2:
    select column1, column2 from table1 t1 inner join table2 t2 on t1.t1Key = t2.t2Key;This is the ANSI join syntax. It works in Oracle 9.1 and higher. The keyword INNER is optional.

  • How to produce the effect of a date range inner join without actually doing one

    I have a T-SQL inner date range join that has done the job, but now that I am starting to learn about query optimization (as in just starting), I'm scratching my head on how to make it better. I see the non-equi join as being the issue because the optimizer
    scans through the entire table.  As you will see from the DDL code pasted below, there are no possible equi-joins. The enrollment history table has about 6000 rows so far, whereas the calendar table has only 10 so far, but is subject to change, which
    is why I have it as a table join.
    Another way to describe this is in the necessary outcome, which you see in the "Is_FAY" column in the Select statement. For each row in the Enrollment History table, I use the Case statement to determine whether the pair of Entry and Exit dates
    are within the Calendar date ranges (and if so, which row the match is found). There are no nulls in either table by design.
    At first, I thought of adding a non-clustered index, but the credible resources and blogs are mixed. Some sources say to use them, others say not to use them, and the remainder are on the fence. That and the
    fact that I drop and recreate the table every 24 hours (there are many updates to the Black Box) makes me very leery about having indices other than the primary key.
    I have pasted DDL language below with sample data.  Change the "Use Learning_Curve" statement to whatever database you want to use -- the rest will run as written.
    Now that I've given you some background and what I hope to accomplish, I hope that someone has encountered this issue before and can suggest an avenue of further inquiry, because I am out of usable research sources and ideas. 
    Thank you
    USE Learning_Curve;
    GO
    IF OBJECT_ID('dbo.Enrollment_History', 'U') IS NOT NULL
    DROP TABLE dbo.Enrollment_History;
    CREATE TABLE dbo.Enrollment_History
    (ID INT NOT NULL identity(1, 1) PRIMARY KEY,
    Campus_ID INT NOT NULL,
    Student_ID INT NOT NULL,
    Entry_Date DATE NOT NULL,
    Exit_Date DATE NOT NULL);
    INSERT INTO dbo.Enrollment_History
    VALUES (1, 103934, '2014-08-11', '2015-01-10'),
    (1, 102912, '2014-09-10', '2015-05-10'),
    (1, 199234, '2014-08-07', '2015-05-01');
    IF OBJECT_ID('dbo.Calendar_FAY_Dates', 'U') IS NOT NULL
    DROP TABLE dbo.Calendar_FAY_Dates;
    CREATE TABLE dbo.Calendar_FAY_Dates
    (ID INT NOT NULL identity(1, 1) PRIMARY KEY,
    FY VARCHAR(4) NOT NULL,
    Start_B DATE NOT NULL,
    Start_E DATE NOT NULL,
    End_B DATE NOT NULL,
    End_E DATE NOT NULL);
    INSERT INTO dbo.Calendar_FAY_Dates
    VALUES ('FY14', '2013-08-05', '2013-08-29', '2014-04-30', '2014-05-15'),
    ('FY15', '2014-08-07', '2014-08-28', '2015-04-30', '2015-05-15');
    SELECT eh.ID,
    eh.Campus_ID,
    eh.Student_ID,
    eh.Entry_Date,
    eh.Exit_Date,
    cfd.FY,
    case
    when eh.Entry_Date >= cfd.Start_B
    and eh.Entry_Date <= cfd.Start_E
    and eh.Exit_Date >=cfd.End_B
    and eh.Exit_Date <= cfd.End_E
    then 1 else 0 end as Is_FAY
    FROM dbo.Enrollment_History eh
    inner join dbo.Calendar_FAY_Dates cfd
    on eh.Entry_Date >= cfd.Start_B
    and eh.Exit_Date <= cfd.End_E

    This definitely a case where an indexes on the two tables will speed things up significantly. 
    It's also one of those strange cases where you can't really rely on the numbers you see in the execution plan. To really get an idea the impact to need to actually time the query...
    For example. Running your final query with out any additional indexes took ~4,000 microseconds...
    Adding the following index, dropped that time down to ~3,000 microseconds...
    CREATE NONCLUSTERED INDEX ix_Enrollment_History_EntryDates ON dbo.Enrollment_History (
    Entry_Date,
    Exit_Date)
    INCLUDE (
    ID,
    Campus_ID,
    Student_ID)
    WITH (DATA_COMPRESSION = PAGE)
    Adding the following index (and keeping the 1st) dropped it again, down to ~2,000 microseconds
    CREATE NONCLUSTERED INDEX ix_CalendarFAYDates_Detes ON dbo.Calendar_FAY_Dates (
    Start_B,
    End_E)
    INCLUDE (
    Start_E,
    End_B,
    ID,
    FY)
    WITH (DATA_COMPRESSION = PAGE)
    Keep in mind that 5 rows of data doesn't even begin to provide an accurate representation of read execution times against actual production data (the smallest changes in background processes on my computer caused the time to vary pretty drastically between
    executions).
    Anyway, the following is a quick & dirty way to test execution times when you're query tuning or checking the impact of different indexes on a query.
    DECLARE @b DATETIME2(7) = SYSDATETIME()
    SELECT eh.ID,
    eh.Campus_ID,
    eh.Student_ID,
    eh.Entry_Date,
    eh.Exit_Date,
    cfd.FY,
    case
    when eh.Entry_Date >= cfd.Start_B
    and eh.Entry_Date <= cfd.Start_E
    and eh.Exit_Date >=cfd.End_B
    and eh.Exit_Date <= cfd.End_E
    then 1 else 0 end as Is_FAY
    FROM dbo.Enrollment_History eh
    inner join dbo.Calendar_FAY_Dates cfd
    on eh.Entry_Date >= cfd.Start_B
    and eh.Exit_Date <= cfd.End_E
    SELECT DATEDIFF(mcs, @b, SYSDATETIME())
    HTH,
    Jason
    Jason Long

  • Very Slow Query with CTE inner join

    I have 2 tables (heavily simplified here to show relevant columns):
    CREATE TABLE tblCharge
    (ChargeID int NOT NULL,
    ParentChargeID int NULL,
    ChargeName varchar(200) NULL)
    CREATE TABLE tblChargeShare
    (ChargeShareID int NOT NULL,
    ChargeID int NOT NULL,
    TotalAmount money NOT NULL,
    TaxAmount money NULL,
    DiscountAmount money NULL,
    CustomerID int NOT NULL,
    ChargeShareStatusID int NOT NULL)
    I have a very basic View to Join them:
    CREATE VIEW vwBASEChargeShareRelation as
    Select c.ChargeID, ParentChargeID, s.CustomerID, s.TotalAmount, isnull(s.TaxAmount, 0) as TaxAmount, isnull(s.DiscountAmount, 0) as DiscountAmount
    from tblCharge c inner join tblChargeShare s
    on c.ChargeID = s.ChargeID Where s.ChargeShareStatusID < 3
    GO
    I then have a view containing a CTE to get the children of the Parent Charge:
    ALTER VIEW [vwChargeShareSubCharges] AS
    WITH RCTE AS
    SELECT ParentChargeId, ChargeID, 1 AS Lvl, ISNULL(TotalAmount, 0) as TotalAmount, ISNULL(TaxAmount, 0) as TaxAmount,
    ISNULL(DiscountAmount, 0) as DiscountAmount, CustomerID, ChargeID as MasterChargeID
    FROM vwBASEChargeShareRelation Where ParentChargeID is NULL
    UNION ALL
    SELECT rh.ParentChargeID, rh.ChargeID, Lvl+1 AS Lvl, ISNULL(rh.TotalAmount, 0), ISNULL(rh.TaxAmount, 0), ISNULL(rh.DiscountAmount, 0) , rh.CustomerID
    , rc.MasterChargeID
    FROM vwBASEChargeShareRelation rh
    INNER JOIN RCTE rc ON rh.PArentChargeID = rc.ChargeID and rh.CustomerID = rc.CustomerID
    Select MasterChargeID as ChargeID, CustomerID, Sum(TotalAmount) as TotalCharged, Sum(TaxAmount) as TotalTax, Sum(DiscountAmount) as TotalDiscount
    from RCTE
    Group by MasterChargeID, CustomerID
    GO
    So far so good, I can query this view and get the total cost for a line item including all children.
    The problem occurs when I join this table. The query:
    Select t.* from vwChargeShareSubCharges t
    inner join
    tblChargeShare s
    on t.CustomerID = s.CustomerID
    and t.MasterChargeID = s.ChargeID
    Where s.ChargeID = 1291094
    Takes around 30 ms to return a result (tblCharge and Charge Share have around 3.5 million records).
    But the query:
    Select t.* from vwChargeShareSubCharges t
    inner join
    tblChargeShare s
    on t.CustomerID = s.CustomerID
    and t.MasterChargeID = s.ChargeID
    Where InvoiceID = 1045854
    Takes around 2 minutes to return a result - even though the only charge with that InvoiceID is the same charge as the one used in the previous query.
    The same thing occurs if I do the join in the same query that the CTE is defined in.
    I ran the execution plan for each query. The first (fast) query looks like this:
    The second(slow) query looks like this:
    I am at a loss, and my skills at decoding execution plans to resolve this are lacking.
    I have separate indexes on tblCharge.ChargeID, tblCharge.ParentChargeID, tblChargeShare.ChargeID, tblChargeShare.InvoiceID, tblChargeShare.ChargeShareStatusID
    Any ideas? Tested on SQL 2008R2 and SQL 2012

    >> The database is linked [sic] to an established app and the column and table names can't be changed. <<
    Link? That is a term from pointer chains and network databases, not SQL. I will guess that means the app came back in the old pre-RDBMS days and you are screwed. 
    >> I am not too worried about the money field [sic], this is used for money and money based calculations so the precision and rounding are acceptable at this level. <<
    Field is a COBOL concept; columns are totally different. MONEY is how Sybase mimics the PICTURE clause that puts currency signs, commas, period, etc in a COBOL money field. 
    Using more than one operation (multiplication or division) on money columns will produce severe rounding errors. A simple way to visualize money arithmetic is to place a ROUND() function calls after 
    every operation. For example,
    Amount = (Portion / total_amt) * gross_amt
    can be rewritten using money arithmetic as:
    Amount = ROUND(ROUND(Portion/total_amt, 4) * 
    gross_amt, 4)
    Rounding to four decimal places might not seem an 
    issue, until the numbers you are using are greater 
    than 10,000. 
    BEGIN
    DECLARE @gross_amt MONEY,
     @total_amt MONEY,
     @my_part MONEY,
     @money_result MONEY,
     @float_result FLOAT,
     @all_floats FLOAT;
     SET @gross_amt = 55294.72;
     SET @total_amt = 7328.75;
     SET @my_part = 1793.33;
     SET @money_result = (@my_part / @total_amt) * 
    @gross_amt;
     SET @float_result = (@my_part / @total_amt) * 
    @gross_amt;
     SET @Retult3 = (CAST(@my_part AS FLOAT)
     / CAST( @total_amt AS FLOAT))
     * CAST(FLOAT, @gross_amt AS FLOAT);
     SELECT @money_result, @float_result, @all_floats;
    END;
    @money_result = 13525.09 -- incorrect
    @float_result = 13525.0885 -- incorrect
    @all_floats = 13530.5038673171 -- correct, with a -
    5.42 error 
    >> The keys are ChargeID(int, identity) and ChargeShareID(int, identity). <<
    Sorry, but IDENTITY is not relational and cannot be a key by definition. But it sure works just like a record number in your old COBOL file system. 
    >> .. these need to be int so that they are assigned by the database and unique. <<
    No, the data type of a key is not determined by physical storage, but by logical design. IDENTITY is the number of a parking space in a garage; a VIN is how you identify the automobile. 
    >> What would you recommend I use as keys? <<
    I do not know. I have no specs and without that, I cannot pull a Kabbalah number from the hardware. Your magic numbers can identify Squids, Automobile or Lady Gaga! I would ask the accounting department how they identify a charge. 
    >> Charge_Share_Status_ID links [sic] to another table which contains the name, formatting [sic] and other information [sic] or a charge share's status, so it is both an Id and a status. <<
    More pointer chains! Formatting? Unh? In RDBMS, we use a tiered architecture. That means display formatting is in a presentation layer. A properly created table has cohesion – it does one and only one data element. A status is a state of being that applies
    to an entity over a period time (think employment, marriage, etc. status if that is too abstract). 
    An identifier is based on the Law of Identity from formal logic “To be is to be something in particular” or “A is A” informally. There is no entity here! The Charge_Share_Status table should have the encoded values for a status and perhaps a description if
    they are unclear. If the list of values is clear, short and static, then use a CHECK() constraint. 
    On a scale from 1 to 10, what color is your favorite letter of the alphabet? Yes, this is literally that silly and wrong. 
    >> I understand what a CTE is; is there a better way to sum all children for a parent hierarchy? <<
    There are many ways to represent a tree or hierarchy in SQL.  This is called an adjacency list model and it looks like this:
    CREATE TABLE OrgChart 
    (emp_name CHAR(10) NOT NULL PRIMARY KEY, 
     boss_emp_name CHAR(10) REFERENCES OrgChart(emp_name), 
     salary_amt DECIMAL(6,2) DEFAULT 100.00 NOT NULL,
     << horrible cycle constraints >>);
    OrgChart 
    emp_name  boss_emp_name  salary_amt 
    ==============================
    'Albert'    NULL    1000.00
    'Bert'    'Albert'   900.00
    'Chuck'   'Albert'   900.00
    'Donna'   'Chuck'    800.00
    'Eddie'   'Chuck'    700.00
    'Fred'    'Chuck'    600.00
    This approach will wind up with really ugly code -- CTEs hiding recursive procedures, horrible cycle prevention code, etc.  The root of your problem is not knowing that rows are not records, that SQL uses sets and trying to fake pointer chains with some
    vague, magical non-relational "id".  
    This matches the way we did it in old file systems with pointer chains.  Non-RDBMS programmers are comfortable with it because it looks familiar -- it looks like records and not rows.  
    Another way of representing trees is to show them as nested sets. 
    Since SQL is a set oriented language, this is a better model than the usual adjacency list approach you see in most text books. Let us define a simple OrgChart table like this.
    CREATE TABLE OrgChart 
    (emp_name CHAR(10) NOT NULL PRIMARY KEY, 
     lft INTEGER NOT NULL UNIQUE CHECK (lft > 0), 
     rgt INTEGER NOT NULL UNIQUE CHECK (rgt > 1),
      CONSTRAINT order_okay CHECK (lft < rgt));
    OrgChart 
    emp_name         lft rgt 
    ======================
    'Albert'      1   12 
    'Bert'        2    3 
    'Chuck'       4   11 
    'Donna'       5    6 
    'Eddie'       7    8 
    'Fred'        9   10 
    The (lft, rgt) pairs are like tags in a mark-up language, or parens in algebra, BEGIN-END blocks in Algol-family programming languages, etc. -- they bracket a sub-set.  This is a set-oriented approach to trees in a set-oriented language. 
    The organizational chart would look like this as a directed graph:
                Albert (1, 12)
        Bert (2, 3)    Chuck (4, 11)
                       /    |   \
                     /      |     \
                   /        |       \
                 /          |         \
            Donna (5, 6) Eddie (7, 8) Fred (9, 10)
    The adjacency list table is denormalized in several ways. We are modeling both the Personnel and the Organizational chart in one table. But for the sake of saving space, pretend that the names are job titles and that we have another table which describes the
    Personnel that hold those positions.
    Another problem with the adjacency list model is that the boss_emp_name and employee columns are the same kind of thing (i.e. identifiers of personnel), and therefore should be shown in only one column in a normalized table.  To prove that this is not
    normalized, assume that "Chuck" changes his name to "Charles"; you have to change his name in both columns and several places. The defining characteristic of a normalized table is that you have one fact, one place, one time.
    The final problem is that the adjacency list model does not model subordination. Authority flows downhill in a hierarchy, but If I fire Chuck, I disconnect all of his subordinates from Albert. There are situations (i.e. water pipes) where this is true, but
    that is not the expected situation in this case.
    To show a tree as nested sets, replace the nodes with ovals, and then nest subordinate ovals inside each other. The root will be the largest oval and will contain every other node.  The leaf nodes will be the innermost ovals with nothing else inside them
    and the nesting will show the hierarchical relationship. The (lft, rgt) columns (I cannot use the reserved words LEFT and RIGHT in SQL) are what show the nesting. This is like XML, HTML or parentheses. 
    At this point, the boss_emp_name column is both redundant and denormalized, so it can be dropped. Also, note that the tree structure can be kept in one table and all the information about a node can be put in a second table and they can be joined on employee
    number for queries.
    To convert the graph into a nested sets model think of a little worm crawling along the tree. The worm starts at the top, the root, makes a complete trip around the tree. When he comes to a node, he puts a number in the cell on the side that he is visiting
    and increments his counter.  Each node will get two numbers, one of the right side and one for the left. Computer Science majors will recognize this as a modified preorder tree traversal algorithm. Finally, drop the unneeded OrgChart.boss_emp_name column
    which used to represent the edges of a graph.
    This has some predictable results that we can use for building queries.  The root is always (left = 1, right = 2 * (SELECT COUNT(*) FROM TreeTable)); leaf nodes always have (left + 1 = right); subtrees are defined by the BETWEEN predicate; etc. Here are
    two common queries which can be used to build others:
    1. An employee and all their Supervisors, no matter how deep the tree.
     SELECT O2.*
       FROM OrgChart AS O1, OrgChart AS O2
      WHERE O1.lft BETWEEN O2.lft AND O2.rgt
        AND O1.emp_name = :in_emp_name;
    2. The employee and all their subordinates. There is a nice symmetry here.
     SELECT O1.*
       FROM OrgChart AS O1, OrgChart AS O2
      WHERE O1.lft BETWEEN O2.lft AND O2.rgt
        AND O2.emp_name = :in_emp_name;
    3. Add a GROUP BY and aggregate functions to these basic queries and you have hierarchical reports. For example, the total salaries which each employee controls:
     SELECT O2.emp_name, SUM(S1.salary_amt)
       FROM OrgChart AS O1, OrgChart AS O2,
            Salaries AS S1
      WHERE O1.lft BETWEEN O2.lft AND O2.rgt
        AND S1.emp_name = O2.emp_name 
       GROUP BY O2.emp_name;
    4. To find the level and the size of the subtree rooted at each emp_name, so you can print the tree as an indented listing. 
    SELECT O1.emp_name, 
       SUM(CASE WHEN O2.lft BETWEEN O1.lft AND O1.rgt 
       THEN O2.sale_amt ELSE 0.00 END) AS sale_amt_tot,
       SUM(CASE WHEN O2.lft BETWEEN O1.lft AND O1.rgt 
       THEN 1 ELSE 0 END) AS subtree_size,
       SUM(CASE WHEN O1.lft BETWEEN O2.lft AND O2.rgt
       THEN 1 ELSE 0 END) AS lvl
      FROM OrgChart AS O1, OrgChart AS O2
     GROUP BY O1.emp_name;
    5. The nested set model has an implied ordering of siblings which the adjacency list model does not. To insert a new node, G1, under part G.  We can insert one node at a time like this:
    BEGIN ATOMIC
    DECLARE rightmost_spread INTEGER;
    SET rightmost_spread 
        = (SELECT rgt 
             FROM Frammis 
            WHERE part = 'G');
    UPDATE Frammis
       SET lft = CASE WHEN lft > rightmost_spread
                      THEN lft + 2
                      ELSE lft END,
           rgt = CASE WHEN rgt >= rightmost_spread
                      THEN rgt + 2
                      ELSE rgt END
     WHERE rgt >= rightmost_spread;
     INSERT INTO Frammis (part, lft, rgt)
     VALUES ('G1', rightmost_spread, (rightmost_spread + 1));
     COMMIT WORK;
    END;
    The idea is to spread the (lft, rgt) numbers after the youngest child of the parent, G in this case, over by two to make room for the new addition, G1.  This procedure will add the new node to the rightmost child position, which helps to preserve the idea
    of an age order among the siblings.
    6. To convert a nested sets model into an adjacency list model:
    SELECT B.emp_name AS boss_emp_name, E.emp_name
      FROM OrgChart AS E
           LEFT OUTER JOIN
           OrgChart AS B
           ON B.lft
              = (SELECT MAX(lft)
                   FROM OrgChart AS S
                  WHERE E.lft > S.lft
                    AND E.lft < S.rgt);
    7. To find the immediate parent of a node: 
    SELECT MAX(P2.lft), MIN(P2.rgt)
      FROM Personnel AS P1, Personnel AS P2
     WHERE P1.lft BETWEEN P2.lft AND P2.rgt 
       AND P1.emp_name = @my_emp_name;
    I have a book on TREES & HIERARCHIES IN SQL which you can get at Amazon.com right now. It has a lot of other programming idioms for nested sets, like levels, structural comparisons, re-arrangement procedures, etc. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Inner join and group by

    Hi Friends,
    My code is;
      SELECT T1~NUMBER SUM( T2~QUANT )
      INTO (ZTABLE1-NUMBER, ZTABLE2-QUANT)
      FROM ZTABLE1 AS T1
      INNER JOIN ZTABLE2 AS T2
      ON T1~NUMBER = T2~NUMBER
      GROUP BY T2~VBELN.
      ENDSELECT.
    I am getting an error as;
    The field T1~NUMBER from SELECT is missing in the GROUP BY clause. addition INTO wa or INTO(g1,...,gn) is required. fields of type "" or "T2-VBELN".
    I couldn't understand anything from the error message. Can you help what the error message says and how can I do my desire on above query.
    Thanks.

    Usually GROUP BY Is used to get only aggregated fields.
    It is not meant for regular selection of fields.
    Instead do a regular selection of all the fields without group by and do your aggregation in program logic as per your requirement.
    In that way your performance will be better.
    Regards
    Sudhir Atluru

  • Need help with inner join and distinct rows

    Hey Guys,
    i have
    1) BaseEnv Table 
    2) Link Table
    3) BaseData Table
    Link table has three columns Id,BaseEnvId,BaseDataId 
    the BaseEnvID is unique in the table where as BaseDataId can be repeated i.e multile rows of BaseEnv Table can point to same BaseData  table row
    Now i want to do  BaseEnvTable inner join Link Table inner join BaseData Table and select 5 columsn ; Name,SyncName,Version,PPO,DOM  from the BaseData table.. the problem is that after i do the inner join I get duplciate records..
    i want to eliminate the duplicate records , can any one help me here

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. Now we have to guess and type, guess and type, etc. because of your bad manners. 
    CREATE TABLE Base_Env
    (base_env_id CHAR(10) NOT NULL PRIMARY KEY,
    Think about the name Base_Data; do you have lots of tables without data? Silly, unh? 
    CREATE TABLE Base_Data
    (base_data_id CHAR(10) NOT NULL PRIMARY KEY,
    Your Links table is wrong in concept and implementation. The term “link” refers to a pointer chain structure used in network databases and makes no sense in RDBMS. There is no generic, magic, universal “id” in RDBMS! People that do this are called “id-iots”
    in SQL slang. 
    We can model a particular relationship in a table by referencing the keys in other tables. But we need to know if the relationship is 1:1, 1:m, or n:m. This is the membership of the relationship. Your narrative implies this: 
    CREATE TABLE Links
    (base_env_id CHAR(10) NOT NULL UNIQUE
       REFERENCES Base_Env (base_env_id),
     base_data_id CHAR(10) NOT NULL
       REFERENCES Base_Data (base_data_id));
    >> The base_env_id is unique in the table where as base_data_id can be repeated I.e multiple rows of Base_Env Table can point [sic] to same Base_Data table row. <<
    Again, RDBMS has no pointers! We have referenced an referencing tables. This is a fundamental concept. 
    That narrative you posted has no ON clauses! And the narrative is also wrong. There is no generic “name”, etc. What tables were used in your non-query? Replace the ?? in this skeleton: 
    SELECT ??.something_name, ??.sync_name, ??.something_version, 
           ??.ppo, ??.dom
    FROM Base_Env AS E, Links AS L, Base_Data AS D
    WHERE ?????????;
    >> I want to eliminate the duplicate records [sic], can any one help me here?<<
    Where is the sample data? Where is the results? Please read a book on RDBMS so you can post correct SQL and try again. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Data is not fetching in inner join

    Hi Experts,
    I developed a select query using inner joins, all the fields data is gettting appended, but one field DISPO data is not getting appeneded. please check the innerjoin and suggest me ur soluitons.
    MODULE GET_DATA OUTPUT.
      select   arbpl
             sptag
             aufnr
             plnum
             posnr
             plnfl
             werks  from s022
                        into table it_s022
                         where arbpl in s_arbpl
                          and  werks in s_werks
                          and  sptag in s_sptag
                          and  matnr in s_matnr.
    if sy-subrc eq 0.
    select a~aufnr
           a~posnr
           a~kdauf
           a~pgmng
           a~plnum
           a~strmp
           b~plnty
           b~gamng
           b~igmng
           b~iasmg
           b~rmnga
           b~gstrp
           b~rsnum
           b~plnbez
           b~dispo
           c~maktx
           b~plnnr
           b~plnty
           b~zaehl
           d~vornr
           d~ltxa1
           b~aufpl
           b~aplzt
           e~astnr
           into corresponding fields of table it_afpo from  afko as b
                    inner join afpo as  a
                     on a~aufnr = b~aufnr
                    inner join makt as c
                     on b~plnbez = c~matnr
                    inner join plpo as d
                     on b~plnnr  = d~plnnr
                      and b~plnty = d~plnty
                      and b~zaehl  = d~zaehl
                     inner join aufk as e
                      on b~aufnr = e~aufnr
                    inner join marc as f
                     on bplnty = fmatnr
                    for all entries in it_s022
                     where b~aufnr = it_s022-aufnr.
                         and e~erdat = it_s022-sptag
                     and  a~posnr = it_s022-posnr
                    and  a~plnum = it_s022-plnum .  .
      if sy-subrc eq 0.
        it_afpo1[] = it_afpo[].
        sort it_afpo1 by aufpl." aplzt."""
        delete ADJACENT DUPLICATES FROM it_afpo1 COMPARING aufpl." aplzt ."""
        select aufpl
               aplzl
               fsavd
               fsavz
               ssedd
               ssedz
               mgvrg
                FROM  afvv  INTO TABLE It_afvv
                               FOR ALL ENTRIES IN IT_afpo1
                               WHERE aufpl = IT_afpo1-aufpl.
                                and aplzl = it_afpo1-aplzt.
       endif.
    loop at it_afpo into wa_afpo.
    DATA : G_SLNO(8) TYPE C.
    G_SLNO = G_SLNO + 1.
    wa_afpo-slno = g_slno.
        move-corresponding wa_afpo to wa_final.
       move-corresponding wa_afko to wa_final.
      read table it_afvv into wa_afvv with key
                aufpl = wa_afpo-aufpl binary search."aplzl = wa_afpo-aplzt binary search."""
    *read table it_afko into wa_afko with key aufnr = wa_final-aufnr.
      IF SY-SUBRC EQ 0.
        move-corresponding wa_afvv to wa_final.
    bal_qty = wa_final-gamng - wa_final-igmng.
        move : wa_afpo-slno  to wa_final-slno,
               wa_afpo-aufnr to wa_final-aufnr,
               wa_afpo-posnr to wa_final-posnr,
               wa_afpo-kdauf to wa_final-kdauf, " Sales order Number
               wa_afpo-pgmng to wa_final-pgmng, " Order item qty
               wa_afpo-plnum to wa_final-plnum, " Planned  Order Qty
               wa_afpo-strmp to wa_final-strmp, " Planned Order Date
               wa_afpo-gstrp to wa_final-gstrp, " Basic Finish  date
               wa_afpo-rsnum to wa_final-rsnum, " Number of Reservation
               wa_afpo-plnbez to wa_final-plnbez, " Material Number
               wa_afpo-maktx to wa_final-maktx,
               wa_afpo-plnnr to wa_final-plnnr,
               wa_afpo-plnty to wa_final-plnty,
               wa_afpo-zaehl to wa_final-zaehl,
               wa_afpo-vornr to wa_final-vornr,
               wa_afpo-ltxa1 to wa_final-ltxa1,
               wa_afpo-aufpl to wa_final-aufpl,
               wa_afvv-fsavd to wa_final-fsavd,
               wa_afvv-fsavz to wa_final-fsavz,
               wa_afvv-ssedd to wa_final-ssedd,
               wa_afvv-ssedz to wa_final-ssedz,
               wa_afvv-mgvrg to wa_final-mgvrg,
               wa_afko-igmng to wa_final-igmng,
               wa_afko-iasmg to wa_final-iasmg,
               wa_afko-rmnga to wa_final-rmnga,
               wa_afko-dispo to wa_final-dispo,
              wa_afko-bal_qty to wa_final-bal_qty,
               wa_aufk-astnr to wa_final-astnr,
               wa_afpo-text  to wa_final-text.
    *bal_qty = wa_final-gamng - wa_final-igmng.
    *bal_qty = wa_afko-bal_qty.
      endif.
      append wa_final to it_final.
      clear:wa_final ,wa_afpo,wa_afvv,wa_aufk,wa_afko.
    endloop.
    endif.
    ENDMODULE.                 " GET_DATA  OUTPUT
    *&      Module  DISPLAY_DATA  OUTPUT
          text
    MODULE DISPLAY_DATA OUTPUT.
      IF REF_CONTAINER IS INITIAL.
         PERFORM FIELD_CAT.
      CREATE OBJECT REF_CONTAINER
      EXPORTING
           PARENT                      =
        CONTAINER_NAME              = 'CUSTOM_CONTROL'
           STYLE                       =
           LIFETIME                    = lifetime_default
           REPID                       =
           DYNNR                       =
           NO_AUTODEF_PROGID_DYNNR     =
          EXCEPTIONS
            CNTL_ERROR                  = 1
            CNTL_SYSTEM_ERROR           = 2
            CREATE_ERROR                = 3
            LIFETIME_ERROR              = 4
            LIFETIME_DYNPRO_DYNPRO_LINK = 5
            others                      = 6
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
      L_LAYO-ZEBRA = 'X'.
      L_LAYO-GRID_TITLE = 'JOB CARD'.
      data: variant type disvariant.
      CREATE OBJECT REF_ALV_GRID
        EXPORTING
           I_SHELLSTYLE      = 0
           I_LIFETIME        =
          I_PARENT          = REF_CONTAINER
           I_APPL_EVENTS     = space
           I_PARENTDBG       =
           I_APPLOGPARENT    =
           I_GRAPHICSPARENT  =
           I_NAME            =
           I_FCAT_COMPLETE   = SPACE
          EXCEPTIONS
            ERROR_CNTL_CREATE = 1
            ERROR_CNTL_INIT   = 2
            ERROR_CNTL_LINK   = 3
            ERROR_DP_CREATE   = 4
            others            = 5
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    l_variant-report = sy-cprog.
    *data: l_prnt type lvc_s_prnt.
    *l_prnt-print = 'X'.
      SORT T_FCAT.
      call method ref_alv_grid->set_ready_for_input
       exporting
        i_ready_for_input = 1.
    create object event_receiver.
       set handler event_receiver->handle_top_of_page for ref_alv_grid.
      CALL METHOD REF_ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
           EXPORTING
           I_BUFFER_ACTIVE               =
           I_BYPASSING_BUFFER            =
           I_CONSISTENCY_CHECK           =
           I_STRUCTURE_NAME              =
            is_variant                     =  variant   "l_variant
            i_save                        = 'X'
           I_DEFAULT                     = 'X'
             IS_LAYOUT                     = L_LAYO
           IS_PRINT                      =
           IT_SPECIAL_GROUPS             =
           IT_TOOLBAR_EXCLUDING          =
           IT_HYPERLINK                  =
           IT_ALV_GRAPHICS               =
           IT_EXCEPT_QINFO               =
           IR_SALV_ADAPTER               =
        CHANGING
          IT_OUTTAB                     = IT_final[]
          IT_FIELDCATALOG               = T_FCAT
           IT_SORT                       =
           IT_FILTER                     =
          EXCEPTIONS
            INVALID_PARAMETER_COMBINATION = 1
            PROGRAM_ERROR                 = 2
            TOO_MANY_LINES                = 3
            others                        = 4
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
       create object g_event_receiver.
       set handler g_event_receiver->handle_user_command for ref_alv_grid.
       set handler g_event_receiver->handle_toolbar for ref_alv_grid.
    § 4.Call method 'set_toolbar_interactive' to raise event TOOLBAR.
       call method ref_alv_grid->set_toolbar_interactive.
       call method cl_gui_control=>set_focus exporting control = ref_alv_grid.
    ENDIF.
    ENDMODULE.                 " DISPLAY_DATA  OUTPUT
    *&      Module  STATUS_0100  OUTPUT
          text
    MODULE STATUS_0100 OUTPUT.
      SET PF-STATUS 'MAIN001'.
    SET TITLEBAR 'xxx'.
    Regards,
    Bharath Kumar

    I agree with Shiva Kumar - most likely there is no such field in it_afpo. A few more suggestions though:
    1) Do not post large pieces of code here - if you have a question about a particular SELECT statement, post only that statement and relevant data definitions. No one is going to read your whole program  anyway - we have work to do too.
    2) Your JOIN is way too big and would be difficult to analyze if anything goes wrong. You might want to start with smaller SELECTs and, when you're sure everything is working correctly, then combine them into one. Also huge JOINT is not necessarily more efficient.
    3) You don't have to use aliases "AS a... AS b", etc. Instead, just use the table name, e.g. AFKO... AFPO... .It is much easier to read this way. The aliases are needed only if you're joining the same table more than once. Also they're not limited to one letter either.

  • Help needed in rewriting the code using inner joins

    Hi all,
    I need help in rewriting this code
    DATA :  FLD LIKE ZTABLE-ZFLD,
            FLD1 LIKE ZTABLE2-ZFLD2.
    select single ZFLD from zTABLE1 into FLD
                            where MATNR  = '123' and
                                  werks = 'ABC'.
    select single ZFLD1 from zTABLE2 into FLD1
                            where  zFLD = FLD.    
    iS there way that we can write this with inner joins
    Thanks

    Hi,
    help me out
    TABLE 1 HAS
    MATNR , WERKS , FLD1.
    TABLE 2 HAS
    FLD1, FLD2.
    I KNOW MATNR AND WERKS BASED ON THAT I HAVE TO GET FLD2
    I WANT IT BE WRITTEN WITH INNEWR JOINS
    WITH FOLLOWING COD E I GET AN ERROR MESSAGE SAYING
    comma without preccing colon (after select?)
    data : FLD1 like zTABLE1-ZFLD1.
    data : FLD2 like ZTABLE2-ZFLD2.
    SELECT FZFLD1 BZFLD2
        INTO ( FLD1, FLD2)
        FROM ZTABLE1 AS F INNER JOIN ZTABLE2 AS B
               ON FZFLD1 = BFLD2
        WHERE F~MATNR = '123'
          AND F~WERKS   = 'ABC' .
    Thanks

  • Help on Inner Join Query

    Hi ABAPers,
    I was trying to develop a report for which query is something like this.
    *& Report  ZT_SERIAL
    REPORT  ZT_SERIAL.
    TABLES : SER01,SER03,OBJK,LIKP,LIPS. " LIKP - SD invoice header table, LIPS - SD invoice details table
    TYPE-POOLS: SLIS.
    DATA: AFIELD TYPE SLIS_FIELDCAT_ALV.
    DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
    MANDATORY WHILE DECLARING ALV ***
    DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV..
    DATA: V_EVENTS TYPE SLIS_T_EVENT.
    DATA: LAYOUT TYPE SLIS_LAYOUT_ALV.
    DATA: P_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV.
    DATA: I_TITLE_SL_NO_TRACK TYPE LVC_TITLE VALUE 'SERIAL NO TRACKER'.
    DATA : BEGIN OF ITAB OCCURS 0,
           OBKNR LIKE OBJK-OBKNR,     "OBJECT LIST
           OBZAE LIKE OBJK-OBZAE,     "OBJECT COUNTER
           KUNDE LIKE SER01-KUNDE,    "CUSTOMER NO
           MATNR LIKE OBJK-MATNR,     "PART NO
           SERNR LIKE OBJK-SERNR,     "SERIAL NO
           MBLNR LIKE SER03-MBLNR,    "GRN/MATERIAL DOC NO
           LIEF_NR LIKE SER01-LIEF_NR,"OUTBOUND DELV NO
           DATUM LIKE SER01-DATUM,    "OUTBOUND DELV DATE
           TASER LIKE OBJK-TASER,     "HEADER TABLE
           ANZSN LIKE SER01-ANZSN,    "NO OF SERIAL NOS
           VGBEL LIKE LIPS-VGBEL,     "SALES ORDER NO
           END OF ITAB.
    SELECTION-SCREEN : BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
    SELECT-OPTIONS : S_KUNDE FOR SER01-KUNDE,
                      S_VGBEL FOR LIPS-VGBEL,
                      S_MATNR FOR OBJK-MATNR,
                      S_SERNR FOR OBJK-SERNR,
                      S_MBLNR FOR SER03-MBLNR,
                      S_LIFNR FOR SER01-LIEF_NR,
                      S_DATUM FOR SER01-DATUM.
    SELECTION-SCREEN : END OF BLOCK B1.
    PERFORM TRACKING.
    PERFORM FLDCAT.
    PERFORM BUILD_LAYOUT.
    ***CALL FUNCTION TO DISPLAY IN ALV FORMAT*******
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        I_CALLBACK_PROGRAM = SY-REPID
        I_GRID_TITLE       = I_TITLE_SL_NO_TRACK
        IS_LAYOUT          = LAYOUT
        IT_FIELDCAT        = P_FIELDTAB[]
        I_SAVE             = 'A'
        IT_EVENTS          = V_EVENTS
      TABLES
        T_OUTTAB           = ITAB.
    ***END OF CALL FUNCTION
    FORM TRACKING.
    SELECT OBJKOBKNR OBJKOBZAE OBJKMATNR OBJKSERNR OBJK~TASER
           SER01KUNDE SER01LIEF_NR SER01DATUM SER01ANZSN
           SER03~MBLNR
           LIPS~VGBEL
           INTO TABLE ITAB FROM OBJK
           INNER JOIN SER01 ON OBJKOBKNR = SER01OBKNR
           INNER JOIN SER03 ON OBJKOBKNR = SER03OBKNR
           INNER JOIN LIPS ON SER01LIEF_NR = LIPSVBELN
           WHERE
           OBJKTASER = 'SER01' OR OBJKTASER = 'SER03'.
           OBJK~MATNR IN S_MATNR AND
           OBJK~SERNR IN S_SERNR AND
           SER01~KUNDE IN S_KUNDE AND
           SER01~LIEF_NR IN S_LIFNR AND
           SER01~DATUM IN S_DATUM AND
           SER03~MBLNR IN S_MBLNR AND
           LIPS~VGBEL IN S_VGBEL.
    ENDFORM.
    *&      Form  fldcat
          text
    FORM FLDCAT.
      PERFORM FLDCAT1 USING 'KUNDE' 'Customer No'.
      PERFORM FLDCAT1 USING 'VGBEL' 'Sales Order No'.
      PERFORM FLDCAT1 USING 'MATNR' 'Tejas Part No'.
      PERFORM FLDCAT1 USING 'SERNR' 'Serial No'.
      PERFORM FLDCAT1 USING 'MBLNR' 'Material Doc No'.
      PERFORM FLDCAT1 USING 'LIEF_NR' 'Outbound Delv No'.
      PERFORM FLDCAT1 USING 'DATUM' 'Outbound Delv Date'.
    ENDFORM.                    "fldcat
    *&      Form  fldcat1
          text
         -->FNAM       text
         -->FTEXT      text
    FORM FLDCAT1 USING FNAM FTEXT.
        DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
              L_FIELDCAT-FIELDNAME  = FNAM.
              L_FIELDCAT-SELTEXT_M = FTEXT.
      IF FNAM EQ 'MATNR'."OR fnam EQ 'PKUNAG'.
        L_FIELDCAT-KEY = 'X'.
      ENDIF.
      LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
      APPEND L_FIELDCAT TO P_FIELDTAB.
    CLEAR fldcat1.
    ENDFORM.                                                    "fldcat1
    *&      Form  build_layout
          text
    FORM BUILD_LAYOUT .
      LAYOUT-NO_INPUT          = 'X'.
      LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
      LAYOUT-ZEBRA = 'X'.
      LAYOUT-TOTALS_TEXT       = 'Totals'(256).
    layout-coltab_fieldname = 'CELL_COLOUR'.
    *clear layout.
    ENDFORM.                   
    But when I run this query,the program gets terminated.Can you please help me in rectifying the query and generate the output?
    Thanks in advance
    Bhavin

    Hi
    I have corrected the code please copy and past it and then try.
    Report ZT_SERIAL.
    TABLES : SER01,SER03,OBJK,LIKP,LIPS.
    " LIKP - SD invoice header table, LIPS - SD invoice details table
    TYPE-POOLS: SLIS.
    DATA: AFIELD TYPE SLIS_FIELDCAT_ALV.
    DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
         + MANDATORY WHILE DECLARING ALV ***
    DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV..
    DATA: V_EVENTS TYPE SLIS_T_EVENT.
    DATA: LAYOUT TYPE SLIS_LAYOUT_ALV.
    DATA: P_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV.
    DATA: I_TITLE_SL_NO_TRACK TYPE LVC_TITLE VALUE 'SERIAL NO TRACKER'.
    DATA : BEGIN OF ITAB OCCURS 0,
    OBKNR LIKE OBJK-OBKNR, "OBJECT LIST
    OBZAE LIKE OBJK-OBZAE, "OBJECT COUNTER
    KUNDE LIKE SER01-KUNDE, "CUSTOMER NO
    MATNR LIKE OBJK-MATNR, "PART NO
    SERNR LIKE OBJK-SERNR, "SERIAL NO
    MBLNR LIKE SER03-MBLNR, "GRN/MATERIAL DOC NO
    LIEF_NR LIKE SER01-LIEF_NR,"OUTBOUND DELV NO
    DATUM LIKE SER01-DATUM, "OUTBOUND DELV DATE
    TASER LIKE OBJK-TASER, "HEADER TABLE
    ANZSN LIKE SER01-ANZSN, "NO OF SERIAL NOS
    VGBEL LIKE LIPS-VGBEL, "SALES ORDER NO
    END OF ITAB.
    SELECTION-SCREEN : BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
    SELECT-OPTIONS : S_KUNDE FOR SER01-KUNDE,
    S_VGBEL FOR LIPS-VGBEL,
    S_MATNR FOR OBJK-MATNR,
    S_SERNR FOR OBJK-SERNR,
    S_MBLNR FOR SER03-MBLNR,
    S_LIFNR FOR SER01-LIEF_NR,
    S_DATUM FOR SER01-DATUM.
    SELECTION-SCREEN : END OF BLOCK B1.
    PERFORM TRACKING.
    PERFORM FLDCAT.
    PERFORM BUILD_LAYOUT.
    ***CALL FUNCTION TO DISPLAY IN ALV FORMAT*******
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
    I_CALLBACK_PROGRAM = SY-REPID
    I_GRID_TITLE = I_TITLE_SL_NO_TRACK
    IS_LAYOUT = LAYOUT
    IT_FIELDCAT = P_FIELDTAB[]
    I_SAVE = 'A'
    IT_EVENTS = V_EVENTS
    TABLES
    T_OUTTAB = ITAB.
    ***END OF CALL FUNCTION
    FORM TRACKING.
    *SELECT OBJK~OBKNR
          OBJK~OBZAE
          OBJK~MATNR
          OBJK~SERNR
          OBJK~TASER
          SER01~KUNDE
          SER01~LIEF_NR
          SER01~DATUM
          SER01~ANZSN
          SER03~MBLNR
          LIPS~VGBEL
      INTO TABLE ITAB FROM OBJK
      INNER JOIN  SER01 ON OBJKOBKNR = SER01OBKNR
      INNER JOIN  SER03 ON OBJKOBKNR = SER03OBKNR
      INNER JOIN  LIPS ON SER01LIEF_NR = LIPSVBELN
      WHERE
    *( OBJKTASER = 'SER01' OR OBJKTASER = 'SER03' ) and
    *OBJK-MATNR IN S_MATNR AND
    *OBJK~SERNR IN S_SERNR AND
    *SER01~KUNDE IN S_KUNDE AND
    *SER01~LIEF_NR IN S_LIFNR AND
    *SER01~DATUM IN S_DATUM AND
    *SER03~MBLNR IN S_MBLNR AND
    *LIPS~VGBEL IN S_VGBEL.
    SELECT a~OBKNR         "OBJK
           a~OBZAE
           a~MATNR
           a~SERNR
           a~TASER
           b~KUNDE         "SER01
           b~LIEF_NR
           b~DATUM
           b~ANZSN
           c~MBLNR        "SER03
           D~VGBEL        "LIPS
       INTO TABLE ITAB FROM  ( OBJK as a
       INNER JOIN  SER01 as b ON aOBKNR = bOBKNR
       INNER JOIN  SER03 as c ON aOBKNR = cOBKNR
       INNER JOIN  LIPS as  d ON bLIEF_NR = dVBELN )
       WHERE
    ( aTASER = 'SER01' OR aTASER = 'SER03' ) and
    a~MATNR IN S_MATNR AND
    a~SERNR IN S_SERNR AND
    b~KUNDE IN S_KUNDE AND
    b~LIEF_NR IN S_LIFNR AND
    b~DATUM IN S_DATUM AND
    c~MBLNR IN S_MBLNR AND
    d~VGBEL IN S_VGBEL.
    ENDFORM.
    *& Form fldcat
       * text
    FORM FLDCAT.
    PERFORM FLDCAT1 USING 'KUNDE' 'Customer No'.
    PERFORM FLDCAT1 USING 'VGBEL' 'Sales Order No'.
    PERFORM FLDCAT1 USING 'MATNR' 'Tejas Part No'.
    PERFORM FLDCAT1 USING 'SERNR' 'Serial No'.
    PERFORM FLDCAT1 USING 'MBLNR' 'Material Doc No'.
    PERFORM FLDCAT1 USING 'LIEF_NR' 'Outbound Delv No'.
    PERFORM FLDCAT1 USING 'DATUM' 'Outbound Delv Date'.
    ENDFORM. "fldcat
    *& Form fldcat1
        text
        -->FNAM text
        -->FTEXT text
    FORM FLDCAT1 USING FNAM FTEXT.
    DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
    L_FIELDCAT-FIELDNAME = FNAM.
    L_FIELDCAT-SELTEXT_M = FTEXT.
    IF FNAM EQ 'MATNR'."OR fnam EQ 'PKUNAG'.
    L_FIELDCAT-KEY = 'X'.
    ENDIF.
    LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
    APPEND L_FIELDCAT TO P_FIELDTAB.
       * CLEAR fldcat1.
    ENDFORM. "fldcat1
    *& Form build_layout
       * text
    FORM BUILD_LAYOUT .
    LAYOUT-NO_INPUT = 'X'.
    LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
    LAYOUT-ZEBRA = 'X'.
    LAYOUT-TOTALS_TEXT = 'Totals'(256).
       * layout-coltab_fieldname = 'CELL_COLOUR'.
    *clear layout.
    ENDFORM.
    Regards,
    Venkat

  • Reply to vj (qery on the inner joins v/s performane test)

    this is ravi shiva's friend.
    hi vj,
             u r working for intelli group....and i had met u twice. by the way give u r contact number i want to speak to u personally.
              here is the code... 
    REPORT  rv_sid_purchase_order_status NO STANDARD PAGE HEADING MESSAGE-ID zrv_message_cl LINE-COUNT 65 LINE-SIZE 110 .
    This include contains declarations
    INCLUDE zrv_sid_purchase_order_t.
    This include contains code for the performs
    INCLUDE zrv_sid_purchase_order_f.
                          Initialization
    INITIALIZATION.
    This perform will initialize the values.
      PERFORM f_initilaize.
    This perform will initialize the field catalog.
      PERFORM f_e01_fieldcat_init USING gt_fieldcat[].
                         Start-Of-Selection
    START-OF-SELECTION.
    This perform will read the values from the database.
      PERFORM f_selection.
    This perform will call the function module to display the values on the list.
      PERFORM f_display.
              End-Of-Selection
    END-OF-SELECTION.
    *&  Include           ZRV_SID_PURCHASE_ORDER_T                         *
               T a b l e s   D e c l a r a t i o n s                     *
    TABLES: ekko,ekpo,ekbe,ekkn,mkpf.
               Type Pools  D e c l a r a t i o n s                       *
    TYPE-POOLS: slis.
          Selection Screen Declaration
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-100.
    SELECT-OPTIONS: ebeln FOR ekko-ebeln,
                    aedat FOR ekko-aedat.
    SELECTION-SCREEN END OF BLOCK b1.
               V a r i  a b l e s                                        *
    DATA : gt_fieldcat         TYPE  slis_t_fieldcat_alv,
           g_repid             LIKE  sy-repid,
           gs_keyinfo          TYPE  slis_keyinfo_alv,
           g_tabname_header    TYPE  slis_tabname,
           g_tabname_item      TYPE  slis_tabname,
           ls_layout           TYPE  slis_layout_alv,
           gt_list_top_of_page TYPE  slis_t_listheader.
               I n t e r n a l  T a b l e s                               *
    DATA: BEGIN OF itab OCCURS 0,
           ebeln LIKE ekko-ebeln,
           lifnr LIKE ekko-lifnr,
           aedat LIKE ekko-aedat,
           submi LIKE ekko-submi,
           name1 LIKE lfa1-name1,
           netwr LIKE erev-netwr,
          END OF itab.
    DATA: BEGIN OF jtab OCCURS 0,
           ebeln      LIKE  ekpo-ebeln,
           ebelp      LIKE  ekpo-ebelp,
           aedat      LIKE  ekpo-aedat,
           txz01      LIKE  ekpo-txz01,
           menge1     LIKE  ekpo-menge,
           menge2     LIKE  ekbe-menge,
           effwr1     LIKE  ekpo-effwr,
           mwskz1     LIKE  ekpo-mwskz,
           ps_psp_pnr LIKE  ekkn-ps_psp_pnr,
           bewtp1     LIKE  ekbe-bewtp,
           belnr1     LIKE  ekbe-belnr,
           dmbtt      LIKE  ekbe-dmbtr,
           mblnr1     LIKE  mkpf-mblnr,
           bldat1     LIKE  mkpf-bldat,
           xblnr1     LIKE  mkpf-xblnr,
           frbnr      LIKE  mkpf-frbnr,
          END OF jtab.
    *&  Include           ZRV_SID_PURCHASE_ORDER_F                         *
    FORM f_initilaize .
      g_repid = sy-repid.
      g_tabname_header            = 'itab'.
      g_tabname_item              = 'jtab'.
      CLEAR gs_keyinfo.
      gs_keyinfo-header01         = 'EBELN'.
      gs_keyinfo-item01           = 'EBELN'.
      gs_keyinfo-header02         = space.
      gs_keyinfo-item02           = 'EBELP'.
      ls_layout-group_change_edit = 'X'.
      ls_layout-colwidth_optimize = 'X'.
      ls_layout-zebra             = 'X'.
      ls_layout-detail_popup      = 'X'.
      ls_layout-get_selinfos      = 'X'.
      ls_layout-window_titlebar   = 'PURCHASE ORDER STATUS'.
      ls_layout-no_keyfix         = 'X'.
    ENDFORM.                    " f_INITILAIZE
    *&      Form  f_e01_fieldcat_init
          text
         -->E01_LT_FIELtext
    FORM f_e01_fieldcat_init  USING  e01_lt_fieldcat TYPE slis_t_fieldcat_alv.
      DATA: ls_fieldcat TYPE slis_fieldcat_alv.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'SUBMI'.
      ls_fieldcat-tabname       = g_tabname_header.
      ls_fieldcat-ref_fieldname = 'SUBMI'.
      ls_fieldcat-ref_tabname   = 'EKKO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'EBELN'.
      ls_fieldcat-tabname       = g_tabname_header.
      ls_fieldcat-ref_fieldname = 'EBELN'.
      ls_fieldcat-ref_tabname   = 'EKPO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'LIFNR'.
      ls_fieldcat-tabname       = g_tabname_header.
      ls_fieldcat-ref_fieldname = 'LIFNR'.
      ls_fieldcat-ref_tabname   = 'EKKO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'NAME1'.
      ls_fieldcat-tabname       = g_tabname_header.
      ls_fieldcat-ref_fieldname = 'NAME1'.
      ls_fieldcat-ref_tabname   = 'LFA1'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'NETWR'.
      ls_fieldcat-tabname       = g_tabname_header.
      ls_fieldcat-ref_fieldname = 'NETWR'.
      ls_fieldcat-ref_tabname   = 'EREV'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-do_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'EBELP'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'EBELP'.
      ls_fieldcat-ref_tabname   = 'EKPO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'TXZ01'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'TXZ01'.
      ls_fieldcat-ref_tabname   = 'EKPO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-just = 'L'.
      ls_fieldcat-fieldname     = 'MWSKZ1'.
      ls_fieldcat-seltext_l     = 'Tax code'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'MWSKZ1'.
      ls_fieldcat-ref_tabname   = 'EKPO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-just          = 'L'.
      ls_fieldcat-fieldname     = 'PS_PSP_PNR'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'PS_PSP_PNR'.
      ls_fieldcat-ref_tabname   = 'EKKN'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'MENGE1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'Po quantity'.
      ls_fieldcat-ref_fieldname = 'MENGE1'.
      ls_fieldcat-ref_tabname   = 'EKPO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'MENGE1'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'EFFWR1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'Po value'.
      ls_fieldcat-ref_fieldname = 'EFFWR1'.
      ls_fieldcat-ref_tabname   = 'EKPO'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'BEWTP1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'Cat'.
      ls_fieldcat-ref_fieldname = 'BEWTP1'.
      ls_fieldcat-ref_tabname   = 'EKBE'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'BELNR1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'GR/IR MDoc'.
      ls_fieldcat-ref_fieldname = 'BELNR1'.
      ls_fieldcat-ref_tabname   = 'EKBE'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'MENGE2'.
      ls_fieldcat-seltext_l     = 'GR/IR quantity'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'MENGE2'.
      ls_fieldcat-ref_tabname   = 'EKBE'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'MBLNR1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'GR No'.
      ls_fieldcat-ref_fieldname = 'MBLNR1'.
      ls_fieldcat-ref_tabname   = 'MKPF'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'BLDAT1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'GR Date'.
      ls_fieldcat-ref_fieldname = 'BLDAT1'.
      ls_fieldcat-ref_tabname   = 'MKPF'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'FRBNR'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'FRBNR'.
      ls_fieldcat-ref_tabname   = 'MKPF'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'XBLNR1'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-seltext_l     = 'Invoice No in GR'.
      ls_fieldcat-ref_fieldname = 'XBLNR1'.
      ls_fieldcat-ref_tabname   = 'MKPF'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname     = 'DMBTT'.
      ls_fieldcat-seltext_l     = 'Invoice value W/O tax'.
      ls_fieldcat-tabname       = g_tabname_item.
      ls_fieldcat-ref_fieldname = 'DMBTT'.
      ls_fieldcat-ref_tabname   = 'EKBE'.
      ls_fieldcat-key           = 'X'.
      ls_fieldcat-no_out        = 'X'.
      ls_fieldcat-no_sum        = 'X'.
      ls_fieldcat-sp_group      = 'A'.
      APPEND ls_fieldcat TO e01_lt_fieldcat.
    ENDFORM.                    " f_e01_fieldcat_init
    *&      Form  f_selection
          text
    FORM f_selection .
      SELECT   ekko~ebeln
               ekko~lifnr
               ekko~aedat
               ekko~submi
               lfa1~name1
               erev~netwr
                               FROM ekko  INNER JOIN lfa1 ON ( ekkolifnr = lfa1lifnr )
                                          INNER JOIN erev ON ( ekkoebeln = erevedokn )
                                          INTO TABLE itab WHERE ekko~ebeln IN ebeln AND
                                                                ekko~aedat IN aedat AND
                                                                bsart <> 'AN'.
      SELECT
             ekpo~ebeln
             ekpo~ebelp
             ekpo~aedat
             ekpo~txz01
             ekpo~menge
             ekbe~menge
             ekpo~effwr
             ekpo~mwskz
             ekkn~ps_psp_pnr
             ekbe~bewtp
             ekbe~belnr
             ekbe~dmbtr
             mkpf~mblnr
             mkpf~bldat
             mkpf~xblnr
             mkpf~frbnr
                            FROM  ekpo INNER JOIN ekkn      ON ( ekpoebeln = ekknebeln
                                                                           AND
                                                                 ekknebelp = ekpoebelp )
                                       INNER JOIN ekbe      ON ( ekknebeln = ekbeebeln
                                                                           AND
                                                                 ekknebelp = ekbeebelp )
                                       LEFT OUTER JOIN mkpf ON ( ekbebelnr = mkpfmblnr )
                                       INTO TABLE jtab FOR ALL ENTRIES IN itab WHERE ekpo~ebeln = itab-ebeln AND
                                                                                    ekpo~aedat = itab-aedat.
    ENDFORM.                    " f_selection
    *&      Form  f_display
          text
    FORM f_display .
      CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
        EXPORTING
          i_callback_program = g_repid
          it_fieldcat        = gt_fieldcat[]
          i_tabname_header   = g_tabname_header
          i_tabname_item     = g_tabname_item
          is_keyinfo         = gs_keyinfo
          is_layout          = ls_layout
        TABLES
          t_outtab_header    = itab
          t_outtab_item      = jtab.
    ENDFORM.                    " f_display

    Hi Ravi,
    1. To check performance of a report we use the perfromance trace. The transaction code is ST05.
    You can also refer the link :
         http://help.sap.com/saphelp_erp2005/helpdata/en/8a/3b834014d26f1de10000000a1550b0/content.htm
    2.  Also we can do the run time analysis of the code uising transaction code SE30.
    3. To do a general check on the code , you can use the SAP Code Inspector. The transaction code for this is SCI. This will defentely give you a detailed analysis of the program.
    4 . To have a general idea about the performance tuning of a report , you should refer the link :
       http://www.sapgenie.com/abap/performance.htm.
    Hope this solves yopur query.
    Regards,
    Kunal.

Maybe you are looking for

  • Open Sales Order Report

    Hello.. my client wants to have the open sales Order report in following format : ====================================================================================================== Customer Name | Item 1 code  | Item 2 code  | Item 3 code  |  Ite

  • How to create a relative path for Flash video on a CDROM

    Hello - I need to distribute a web site to a client on a cross-platform Hybrid CDROM so that they can edit and make adjustments. This website also includes 2 flash videos that need to be included on the CDROM. My problem is this: I encoded the video

  • T40 video driver issues

    My ThinkPad T40 recently had some strange scrambled lines across the screen.  They aren't constant and they don't show up when I switch HD's to my Fedora setup.  It only shows up in the XP setup, and it is red on white, blue on black, and various oth

  • IPhone 5s fell down and damaged

    My iPhone 5s fell down and damaged, I've bought it from Canada . Now I am in Saudi Arabia, and there is no Apple Store here , What is the procedure in this case?

  • Second side of doc upside down?

    My officejet pro 8600 all in one prints the second side of a double sided doc, upside down.  I use word 7 and vista and windows 7 (2 diff comps).  How do I stop this?