Inner Join or Merge in a single Table?

I have a Single table with
City            date               type     vlCC       vlVV       
    key
London    24-01-2014        CC       blue                         key1
London    25-01-2014        VV                    yellow         key4
London    24-01-2014        VV                    green          key2
Lisbon     24-01-2014        CC       green                     
 key5
Lisbon     24-01-2014        VV                     red           
 key6
London    25-01-2014        CC       black                        key3
My first approach was a inner join but hanged here.
SELECT distinct p.city, p.date, p.type FROM Cities AS P INNER JOIN Cities AS P2
ON P.City = P2.City AND P.Date = P2.Date   AND P.type <> P2.type and P.Date > GETDATE() order by   p.city, p.date,  p.type
My wanted output is
London     24-01-2014       blue        green      key1      key2
Lisbon      24-01-2014        green      red          key5      key6
London     25-01-2014       black       yellow     key3      key4
(ordered by   p.city, p.date,  p.type)
It means I want to "merge" the rows by city and date and order it like that (city,date )
Thanks

I have included solutions posted earlier (make them all work) and they can return what you need. 
It seems the solution Latheesh provided with Max(Case...) works faster  by viewing their execution plan with this sample data.
Thanks for everyone's input. I have combined all queries in one script now:
Create Table Test(City varchar(50),date Date, type Char(2), vlCC varchar(100),vlVV varchar(100),[key] Varchar(10))
Insert into Test Values
('London','01/24/2014','CC','blue','','key1'),
('London','01-25-2014','VV','','yellow','key4'),
('London','01-24-2014','VV','','green','key2'),
('Lisbon','01-24-2014','CC','green','','key5'),
('Lisbon','01-24-2014','VV','','red','key6'),
('London','01-25-2014','CC','black','','key3')
,('Lisbon','01-26-2014','cc','','violet','key7'),
('London','01-27-2014','vv','yellow','','key8')
--Latheesh
Select City,date,vLcc,vLcc, ccKey, vvKey FROM (
Select City,date
,MAX (vlCC) as vLcc
,MAX (vlVV) as vLvv
,MAX(Case when type = 'cc' then [Key] else null end) ccKey
,MAX(Case when type = 'vv' then [Key] else null end) vvKey
From Test
Group by City,date) t
WHERE ccKey+vvKey IS Not NULL
Order by Date, City DESC
--Visakh16
Select * from (SELECT COALESCE(t1.city,t2.city) AS city,
COALESCE(t1.date,t2.date) AS date,
t1.vlCC,
t2.vlVV,
t1.[key] as key1,
t2.[key] as key2
FROM Test t1
FULL JOIN Test t2
On t2.city = t1.city
AND t2.date = t1.date
AND t2.type = 'vv'
AND t1.type = 'cc') t
WHERE key1+key2 IS Not NULL
Order by Date, City DESC
--eralper
; with cte as (
select *, rn = ROW_NUMBER() over (partition by city, date order by type) from test
SELECT * FROM (
select distinct
city,
date,
max(vlCC) over (partition by city, date) as vlCC,
max(vlVV) over (partition by city, date) as vlVV,
key1 = max(case when rn = 1 then [key] else null end) over (partition by city, date),
key2 = max(case when rn = 2 then [key] else null end) over (partition by city, date)
from cte) t
WHERE key1+key2 is not null
--Jayakumaur
;with cte
as
select City,Date,max(vlCC) vlCC,max(vlVV) vlVV
From test
group by city,date
,cte1
as
select city,date,[key]
from test
select city,date,vlCC,vlVV,[1] as keya,[2] as keyb
from
select c.* ,a.[key],row_number() over(partition by c.city,c.date order by [key]) rn
from cte c
inner join cte1 a on c.city=a.city and c.date=a.date
) tab
PIVOT
max([key]) for rn in([1],[2])
) pvt
WHERE [1] +[2] IS Not NULL
Order by Date, City DESC
Drop table Test

Similar Messages

  • Inner join query used with 7 Database tables

    HI All,
    In a report they used the Inner join Query with 6 Data base table..now there is a performance issue with at query.
    its taking so much of time to trigger that query. Please help how to avoid that performance issue for that.
    In that 2 database tables containing lakhs of records..
    According to my knowledge it can be avoided by using secondary indexs for those 2 database tables..
    and by replacing the Inner join Query with FOR ALL ENTRIES statement.
    i want how to use the logic by using FORALL ENTRIES statement for this..
    So, please give you proper suggestion to avoid this issue..
    Thanking you.
    Moderator message: Please Read before Posting in the Performance and Tuning Forum
    Edited by: Thomas Zloch on Oct 16, 2011 10:27 PM

    Hi,
    And what do you mean with "they used"? If "SAP used" then yo will need to ask a SAP for note
    FOR ALL ENTRIES is quite good described in help. Please search forum also.
    Without query it won't be possible to tell how it can be optimized, however you can try to use SE30/SAT and ST05. Maybe it will help you.
    BR
    Marcin Cholewczuk

  • Inner join command is not supporting pooled table

    Hi Forum,
    I'm new to abap. I tried passing fields of two tables into a single table using inner join command. But one of the tables is a pooled table so the command "inner join" is not supporting that. How to pass the fields in those two tables into a single table? Kindly help me out.
    Rgrds,
    Mahathi

    Hi ....
    U can't perform JOIN with Pooled or Cluster tables
    trying to join A018 and KONP .... here A018 is Pooled table... Look at below threads...
    Re: abap query join A018/KONP
    Can't perform join table
    Hope it will solve your problem..
    <b>Reward points if useful.</b>
    Thanks & Regards
    ilesh 24x7

  • Error in inner join when i am using 3 tables to join..

    hi,
    why error is giving when i am joining 3 tables ..
    tables : lfa1,lfb1,lfm1.
    types : begin of ty_lfa1,
              lifnr type lfa1-lifnr,
             name1 type lfa1-name1,
             bukrs type lfb1-bukrs,
               erdat type lfm1-erdat,
            end of ty_lfa1.
    data :   i_lfa1 type ty_lfa1 occurs 1.
    select a~lifnr
           a~name1
           b~bukrs
           m~erdat
    into table i_lfa1
           from ((lfa1 as a inner join lfb1 as b on alifnr = blifnr) inner join lfm1 as m on alifnr = mlifnr).
    error : The table name "((LFA1 AS A INNER JOIN LFB1 AS B ON ALIFNR = BLIFNR)          
    INNER JOIN LFM1 AS M ON ALIFNR = MLIFNR)" is too long . unique.          
    unique. unique. unique. unique. unique.          
    plz help me ...why it is showing..
    thanks & regards ,
    kalyan

    Hi Kalyan,
      Do it this way: In your code just give the space between (  & (  & lfa1 and also at the end ~lifnr )
    select a~lifnr
    a~name1
    b~bukrs
    m~erdat
    into table i_lfa1
    from ( ( lfa1 as a inner join lfb1 as b on a~lifnr eq b~lifnr ) " maintain a space bwteen ( and field
    inner join lfm1 as m on a~lifnr eq m~lifnr ).
    or
    select a~lifnr
    a~name1
    b~bukrs
    m~erdat
    into table i_lfa1
    from lfa1 as a inner join lfb1 as b
      on a~lifnr eq b~lifnr
         inner join lfm1 as m
      on a~lifnr eq m~lifnr.
    With luck,
    Pritam.
    Edited by: Pritam Ghosh on Jan 13, 2009 2:31 PM

  • Inner join-select -primary key in table issue

    Hi ,
            Iam using FEBKO(header) and FEBEP(item) in inner join  select .But the datas fetching by this selct in not correct.The analysis is the is no common primary fields in the both table.
    Question 1-> Can i use inner join without common primary key in the both tables, weather it possible to make a select without common primary key in both table. Please kindly let me know.
    Question 2-> What is the other possible way to give the selct for both table(better performance)
    Regards,
    Veera

    Hi,
    When you use INNER JOIN in this case, link your tables based on KUKEY and ESNUM fields, bcoz there can be many items under a single header. So this will work for you, even from the performance point of view.
    Hope this is helpful to you. If you need further information, revert back.
    Reward all the helpful answers.
    Regards
    Nagaraj T

  • How to use inner joins for 4 0r 5 tables

    Hai Experts,
    plz check my code, n plz help to inner join all the tables
    TYPES : BEGIN OF ITAB,
           EBELN TYPE EKKO-EBELN,            "P.O.NUMBER
           AEDAT TYPE EKKO-AEDAT,            "DATE
           BPRME TYPE EKPO-BPRME,            "UNIT PRICE
           MENGE TYPE EKPO-MENGE,            "Purchase order quantity
           WERKS TYPE EKPO-WERKS,            "PLANT
           BRTWR TYPE EKPO-BRTWR,            "Gross order value in PO
           STRAS TYPE LFA1-STRAS,            "House number and street
           LIFNR TYPE LFA1-LIFNR,            "VENDOR NO
           NAME1 TYPE LFA1-NAME1,            "V NAME
           ORT01 TYPE LFA1-ORT01,            "CITY
           LAND1 TYPE LFA1-LAND1,            "LAND
           PSTLZ TYPE LFA1-PSTLZ,             "POSTAL CODE
           MAKTX TYPE MAKT-MAKTX,            "DESCRIPTION
    END OF ITAB.
    <b>and i got stucked here:</b>
    <u></u>
    SELECT EKKO~EBELN
             EKKO~AEDAT
             EKPO~BPRME
             EKPO~MENGE
             EKPO~WERKS
             EKPO~BRTWR
             LFA1~LIFNR
             LFA1~STRAS
             LFA1~NAME1
             LFA1~ORT01
             LFA1~LAND1
             LFA1~PSTLZ
             MAKT~MAKTX
             INTO TABLE ITAB
             FROM EKKO
             INNER JOIN EKPO LFA1 T001W MAKT
             ON EKKOEBELN = EKPOEBELN
                EKKO~EBELN
             WHERE EKKO~EBELN IN S_EBELN.

    hi Rajesh,
    you have to join the tables one by one:
    SELECT ekko~ebeln
                 ekko~aedat
                 ekpo~bprme
                 ekpo~menge
                 ekpo~werks
                 ekpo~brtwr
                 lfa1~lifnr
                 lfa1~stras
                 lfa1~name1
                 lfa1~ort01
                 lfa1~land1
                 lfa1~pstlz
                 makt~maktx
    INTO TABLE itab
    FROM ekko AS ekko
    INNER JOIN ekpo AS ekpo
    ON ekkoebeln EQ ekpoebeln
    INNER JOIN lfa1 AS lfa1
    ON ekkolifnr EQ lfa1lifnr
    INNER JOIN t001w AS t001w
    ON ekpowerks EQ t001wwerks
    INNER JOIN makt AS makt
    ON ekpomatnr EQ MAKTmatnr
    WHERE ekko~ebeln IN s_ebeln.
    This is the complete SELECT. BUT! The performance will be much better if you take out T001W and MAKT from the INNER JOIN. Do select on these tables in separate steps and merge the internal tables afterwards.
    hope this helps
    ec

  • Inner Join of 3 tables is correct or not?

    Hi Guys ,
                 I have a requirement where i have to join 3 tables i  dont know whether the inner Join which i wrote for 3 tables is correct or not.I am not getting any Syntax error but whether the logci below which i wrote gets all the records or not.
    The Requirement is
    "c.     Select the BOL Number entered in the screen and query the table LIKP with the BOL number in the field LIKP-BOLNR.  Gather the list of ALL delivery documents (LIKP-VBELN) that is outputted.
    d.     Query the list of the delivery documents obtained into the table VBFA in the field VBFA- VBELV.  From the output that is displayed, select the Follow-On Document Field (VBFA-VBELN) for that item whose Subsequent Document Category (VBFA- VBTYP_N) is R and the Movement Type (VBFA- BWART) is 641.  Get the Follow-On document number for each of the above Delivery Document number.
    e.     Query the table EKBE with the Follow On document obtained above in the field Material Document (EKBE- BELNR).  Perform this activity for each of the follow on document obtained above.  Get the resultant Purchase Order (EKBE-EBELN) and Item Number (EKBE-EBELP) from the query.  After querying will all the Follow-On Documents, get the unique list of PO number and Item Number.
    The logic which i wrote is
                    Begin of t_PoolSTO_out,
                  BOLNR type LIKP-BOLNR,
                  EBELN type EKBE-EBELN,
                  EBELP type EKBE-EBELP,
                  VBELN type LIKP-VBELN,
                  VBELNV type VBFA-VBELN,
             End of t_PoolSTO_out.
          Data: i_PoolSTO type Standard table of t_PoolSTO_out.
      Select
            a~BOLNR
            c~EBELN
            c~EBELP
            a~VBELN
            b~VBELN
            from LIKP as a
            Inner Join VBFA as b on aVBELN = bVBELV
            Inner Join EKBE as c on bVBELN = cBELNR
            into Table i_PoolSTO
            Where a~BOLNR in S_LBLNE and
                  b~VBTYP_N = 'R' and
                  b~BWART = '641'.
              My doubt is whether the logic works or not i Mean does i getall the rrecords based on the requirement.?
                      If not please tell any alternative logic?
    Thanks,
    Gopi Anne.

    Hi Gopi,
    Your code is Good.
    But try to avoid Inner join with more number of Tables ...because this is a performance issue..
    try to use..
    select (primary key fields mainly,other fields) from LIKP into itab where bolnr in p_bolnr(paramater).
    next try to use for all entries option..
    select (primary key fields mainly,other fields) from VBFA for all entries in itab where (give the condition)....
    simillarly do for the other select ....ok this will try to reduce the performance issue....
    <b><REMOVED BY MODERATOR></b>
    Message was edited by:
            Alvaro Tejada Galindo

  • Please tell me the  Three table INNER JOIN Sql statement

    Hi experts,
      I got requirement like by using INNER JOIN i have to write the code in MY program i.e using 3 tables VBAK VBAP and VBUK.And the common field is VBELN .SO please give the  INNER JOIN SQL statement for above three tables...
    vbak-vbeln,erdat
    vbap-vbeln,posnr
    vbuk-vbeln,RFSTK
    Thanks in Advance

    hi guglani  please see my total code b.caus DATA is not extracting...once see the code  correct error.
    DATA:V_VBELN TYPE VBAK-VBELN.      "VBAK,VBAP AND VBUK
    SELECT-OPTIONS SORDER FOR V_VBELN.
    TYPES:BEGIN OF T_VBAK,
         VBELN TYPE VBELN_VA,
         ERDAT TYPE ERDAT,
      END OF T_VBAK.
    TYPES:BEGIN OF T_VBAP,
           VBELN TYPE VBELN_VA,
            POSNR TYPE POSNR_VA,
      END OF T_VBAP.
    TYPES:BEGIN OF T_VBUK,
        VBELN TYPE VBELN,
      RFSTK TYPE RFSTK,
      END OF T_VBUK.
    TYPES:BEGIN OF FS,
       VBELN TYPE VBELN_VA,
         ERDAT TYPE ERDAT,
    POSNR TYPE POSNR_VA,
      RFSTK TYPE RFSTK,
      END OF FS.
    DATA:WA1 TYPE T_VBAK,
         WA2 TYPE T_VBAP,
         WA3 TYPE T_VBUK,
         WA TYPE FS.
    DATA:ITAB1 TYPE TABLE OF T_VBAK,
          ITAB2 TYPE TABLE OF T_VBAP,
          ITAB3 TYPE TABLE OF T_VBUK,
          ITAB TYPE TABLE OF FS.
    select a~vbeln a~erdat b~posnr c~rfstk
           from vbak as a inner join vbAP as b on a~vbeln = b~vbeln
                          inner join vbuk as c on a~vbeln = c~vbeln
    into table itab
    where A~vbeln eq SORDER.
    IF NOT ITAB IS INITIAL.
      SORT ITAB BY VBELN.
    ENDIF.
    LOOP AT ITAB INTO WA.
       WRITE:/ WA-VBELN,WA-ERDAT,WA-rfstk.
       ENDLOOP.
       CLEAR WA.
       REFRESH ITAB.

  • Inner join inside a internal table loop

    Hello to all,
    I have this internal table in which i have selected some records for particular coulms. For  the rest of the colums i need to join 3 tables for the data. I had in mind, of looping the internal table and then having a inner join query and  modifying the internal table(all inside the loop) Just wanted to know, whether its a qood practice performance wise ? IF not, what are the alternatives ..
    Thanks,
    Shehryar Dahar

    shehryar,
      say You have internal table ITAB.Already some columns are updated in internal table.say A,B,C,D.
    To fill rest of the columns(E,F,G) you need inter join.
    write the inner join of the table for all entries of itab where A eq itab-A.
    LOOP ITAB.
        READ TABLE ITAB1  WITH KEY X = ITAB-X.
       IF SY-SUBRC EQ 0.
         move  ITAB record to I_FINAL.
         move itab1 records to i_final.
        append i_final.
       clear i_final.
      ENDIF. 
    ENDLOOP.
    Don't forget to reward if useful...

  • 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.

  • Query related to inner join V/S all entries........

    Hi all,
    I have a dought ...related to join and all entries
    Which one will be the better as per performance prospective :
    1>For all entries
    or
    2> using inner join condition
    i am combining 2 tables ...
    but in my where clause is having 6-7 condition if i used inner join concept...
    Please let me know thanks for the help...in advance
    regards
    Amit

    Check Re: Multiple Table Join instead of Nested Selects? I wrote a program that compares the performance of a join against for all entries. The join won. But the best thing to do is for you test and compare both methods for your particular situation.
    In a select, the most important factor is the use of an index.
    Rob

  • Poor Performance on Inner Join of VBUP and VBAK

    The following select is performing poorly, i.e., ST05 Execution is 1, Records is 246, but Time/Exec 94,499,272. Very poor.
    SELECT VBUPVBELN VBUPPOSNR VBAK~KUNNR
             FROM VBUP INNER JOIN VBAK
             ON VBUPVBELN = VBAKVBELN
             WHERE VBAK~VBTYP EQ 'C'     
               AND VBUP~LFSTA NE 'C'
               AND VBUP~LFGSA NE 'C'.
    VBUP has a custom index, Z01, on MANDT, LFSTA, LFGSA. There is no index on VBAK.
    The Explain under ST05 indicates an Est. I/O cost of 73.94 for a Clustered Index Seek on [VBAK].[VBAK0], and an Est. I/O cost of 81.34 for an Index Seek on [VBUP].[VBUPZ01].
    There is no index on VBAK, so I assume that's the first problem. But I don't understand why the index on VBUP does not appear to be helping. This the ST05 Explain: Index Seek WHERE: [PCI].[pci].[VBUP].[LFGSA] as [T_00].[LFGSA]<[@P4] OR [PCI].[pci].[VBUP].[LFGSA] as [T_00].[LFGSA]>[@P4] : [VBUP].MANDT EQ [@P1] ORDERED 1.
    Is my index even being used?  Any ideas how I can speed this up?
    Thanks in advance for your help.
    Beth

    tis the negative selection which is causing the problem..try to avoid selection using not equal to.
    try this.
    ranges : r_lfsta for vbup-lfsta.
    r_lfsta-sign = 'I'.
    r_lfsta-option = 'EQ'.
    r_lfsta-low = ' '.
    append r_lfsta.
    r_lfsta-low = 'A'.
    append r_lfsta.
    r_lfsta-low = 'B'.
    append r_lfsta.
    do similar for r_lfgsa also.
    SELECT VBUP~VBELN
                 VBUP~POSNR
                 VBAK~KUNNR
    FROM VBUP INNER JOIN VBAK
    into corresponding fields of table t_vbakvbup
    ON VBUPVBELN = VBAKVBELN
    WHERE VBAK~VBTYP EQ 'C'
    AND VBUP~LFSTA in r_lfsta
    AND VBUP~LFGSA in r_lfgsa. 
    or
    SELECT VBUP~VBELN
                 VBUP~POSNR
                 VBAK~KUNNR
    vbup~lfsta
    vbup~lfgsa
    FROM VBUP INNER JOIN VBAK
    into corresponding fields of table t_vbakvbup
    ON VBUPVBELN = VBAKVBELN
    WHERE VBAK~VBTYP EQ 'C' .
    delete t_vbakvbup where lfsta ne 'C'.
    delete t_vbakvbup where lfgsa ne 'C'.

  • Select querry using inner join

    Hi Friends,
    I need to fetch data from two tables, I used inner join but I am not able to fetch the data in some fields can any one tell me what is wrong in this select querry.
    SELECT but000~partner
             but000~name_org1
             but000~name_first
             but000~name_last
             but000~bpext
          FROM but000 INNER JOIN dfkkop ON but000partner = dfkkopgpart
        INTO table lt_but000
        WHERE but000~partner IN so_part
        AND dfkkop~faedn IN so_faedn.
    Thanx in advance,
    Parvez

    hi Lutin,
    SELECT but000~partner
    but000~name_org1
    but000~name_first
    but000~name_last
    but000~bpext
    FROM but000 INNER JOIN dfkkop ON but000partner = dfkkopgpart  <------check this condition,it has to *satisfy  both table                                                                        
    INTO table lt_but000
    WHERE but000~partner IN so_part
    AND dfkkop~faedn IN so_faedn.
    Regards,
    siva
    Message was edited by:
            SivaKumar

  • How to use Inner join of table as Source in Merge statement in SQL

    Hi All,
        I am trying to make source as multiple tables output using Join while coding there is no any syntax error but when i am executing this statement is giving following error
    Following is the query 
    Merge Into EmpDept Target
    Using (select E.Address,e.Design,e.EmailId,e.EmpId,e.Ename,e.ManagerId, e.Salary,D.DeptId,D.DeptName,D.Location from Employee E Inner join Dept D on E.DeptId=D.DeptId )As Source (Address,Design,EmailId,EmpId,EName,ManagerId,Salary,DeptId,DeptName,Location)
    On Source.EmpId=Target.EmpId
    when not matched then
    Insert (Target.Address,Target.Design,Target.EmailId,Target.EmpId,Target.Ename,Target.ManagerId, Target.Salary,Target.DeptId,Target.DeptName,Target.Location)
    values
    (Address,Design,EmailId,EmpId,EName,ManagerId, Salary,DeptId,DeptName,Location)
    When matched then 
    Update set Target.Address = Source.Address ,Target.Design = Source.Design,Target.EmailId      = Source.EmailId     ,Target.Ename       = Source.Ename      ,Target.ManagerId = Source.ManagerId , Target.Salary        = Source.Salary       ,Target.DeptId      = Source.DeptId      ,Target.DeptName = Source.DeptName ,Target.Location    = Source.Location;
    This is error while executing the above merge statement 
    The insert column list used in the MERGE statement cannot contain multi-part identifiers. Use single part identifiers instead.
    Please suggest me where i am wrong.. 
    Niraj Sevalkar

    MERGE INTO EmpDept Target
    Using (SELECT E.Address,
    e.Design,
    e.EmailId,
    e.EmpId,
    e.Ename,
    e.ManagerId,
    e.Salary,
    D.DeptId,
    D.DeptName,
    D.Location
    FROM Employee E
    INNER JOIN Dept D
    ON E.DeptId = D.DeptId) AS Source (Address, Design, EmailId, EmpId, EName, ManagerId, Salary, DeptId, DeptName, Location)
    ON Source.EmpId = Target.EmpId
    WHEN NOT matched THEN
    INSERT (Address,
    Design,
    EmailId,
    EmpId,
    Ename,
    ManagerId,
    Salary,
    DeptId,
    DeptName,
    Location)
    VALUES (Address,
    Design,
    EmailId,
    EmpId,
    EName,
    ManagerId,
    Salary,
    DeptId,
    DeptName,
    Location)
    WHEN matched THEN
    UPDATE SET Address = Source.Address,
    Design = Source.Design,
    EmailId = Source.EmailId,
    Ename = Source.Ename,
    ManagerId = Source.ManagerId,
    Salary = Source.Salary,
    DeptId = Source.DeptId,
    DeptName = Source.DeptName,
    Location = Source.Location;

  • Confusion in Select query having a inner join on single table

    Hi,
    I was going through coding and came a accross a select query which has a inner join on a single table. I am getting confused while analysing this . Please someone can help me analysing this query.
    select
            m~MATERIAL
            s~NUMERATOR s~DENOMINTR
            m~GROSS_WT  m~UNIT_OF_WT
          into table itab
          from ( table1as s
           inner join table1 as m
           on  m~MATERIAL = s~MATERIAL
          and m~MAT_UNIT = 'CS'
          and m~SOURSYSTEM = s~SOURSYSTEM )
          where s~MAT_UNIT = 'EA'
             and s~SOURSYSTEM = 'LD'
             and s~OBJVERS = 'A'
             and s~MATERIAL IN ( Select mat_sales
                from Table2  group by mat_sales ).
    Thank you
    Kusuma

    I don't see any use of the INNER JOIN here.
    But what's the meaning of the last selection clause?
    s~MATERIAL IN ( Select mat_sales
                from Table2  group by mat_sales ).
    Is that native SQL or something?
    Pushpraj

Maybe you are looking for