Advanced Join Statement

Hi All,
I need some assistance creating a sophisticated join statement. Please find below my sample data tables:
Table 1 (Region Product)
Region
Product Code
Product Desc
Dubai
Prod A
Prod A Desc
Dubai
Prod B
Prod B Desc
Dubai
Prod C
Prod C Desc
Sharjah
Prod B
Prod B Desc
Sharjah
Prod C
Prod C Desc
Abu Dhabi
Prod A
Prod A Desc
Abu Dhabi
Prod C
Prod C Desc
Table 2 (Region Locations)
Region
Location
Location Desc
Dubai
Loc 1
Dubai Mall
Dubai
Loc 2
MOE
Dubai
Loc 3
DCC
Dubai
Loc 4
MCC
Sharjah
Loc 11
Sahara Center
Sharjah
Loc 12
Mega Mall
Abu Dhabi
Loc 21
Marina Mall
Final Joined Table (Regions Locations Products)
Region
Location
Location Desc
Product Code
Product Desc
Dubai
Loc 1
Dubai Mall
Prod A
Prod A Desc
Dubai
Loc 1
Dubai Mall
Prod B
Prod B Desc
Dubai
Loc 1
Dubai Mall
Prod C
Prod C Desc
Dubai
Loc 2
MOE
Prod A
Prod A Desc
Dubai
Loc 2
MOE
Prod B
Prod B Desc
Dubai
Loc 2
MOE
Prod C
Prod C Desc
Dubai
Loc 3
DCC
Prod A
Prod A Desc
Dubai
Loc 3
DCC
Prod B
Prod B Desc
Dubai
Loc 3
DCC
Prod C
Prod C Desc
Dubai
Loc 4
MCC
Prod A
Prod A Desc
Dubai
Loc 4
MCC
Prod B
Prod B Desc
Dubai
Loc 4
MCC
Prod C
Prod C Desc
Sharjah
Loc 11
Sahara Center
Prod B
Prod B Desc
Sharjah
Loc 11
Sahara Center
Prod C
Prod C Desc
Sharjah
Loc 12
Mega Mall
Prod B
Prod B Desc
Sharjah
Loc 12
Mega Mall
Prod C
Prod C Desc
Abu Dhabi
Loc 21
Marina Mall
Prod A
Prod A Desc
Abu Dhabi
Loc 21
Marina Mall
Prod C
Prod C Desc
Here is the description:
We have a table of products available to be sold in each region
We have a table of locations in each region
We want to get a final table of which product is available in each location (region being the common factor)
I can do it manually, let’s say first for Dubai, then for Sharjah and then for Abu Dhabi and then take a union. But it's quite cumbersome and manual process, as I’ve more than 30-40 regions (1,000+ locations and 10,000+ products).
Your help will be highly appreciated.
Thanks

Try this
DECLARE @Product TABLE (Region VARCHAR(50), ProductCode VARCHAR(50));
DECLARE @Location TABLE (Region VARCHAR(50), Location VARCHAR(50));
INSERT INTO @Product VALUES
('Dubai', 'Prod A'),
('Dubai', 'Prod B'),
('Dubai', 'Prod C'),
('Sharjah', 'Prod B'),
('Sharjah', 'Prod C'),
('Abu Dhabi', 'Prod A'),
('Abu Dhabi', 'Prod C')
INSERT INTO @Location VALUES
('Dubai', 'Loc 1'),
('Dubai', 'Loc 2'),
('Dubai', 'Loc 3'),
('Dubai', 'Loc 4'),
('Sharjah', 'Loc 11'),
('Sharjah', 'Loc 12'),
('Abu Dhabi', 'Loc 21')
SELECT
P.Region, L.Location, P.ProductCode
FROM @Product P
INNER JOIN @Location L
ON P.Region = L.Region
ORDER BY P.Region, L.Location
Krishnakumar S

Similar Messages

  • Using offset in join statement

    hi,
    i wanted to use offset in join statement. but it is giving error.
    select a~kunnr
              a~vkorg
             from knvv as inner join zcust
             on knvv-kunnr+5(5) = zcust-refid.

    hi neha,
    try this code i tested it.
    types: begin of ty_knvv,
           INCLUDE type knvv,
           kunid type i, " For u type is zcust-refid
           end of ty_knvv.
    data: it_knvv type STANDARD TABLE OF ty_knvv INITIAL SIZE 0,
          wa_knvv type ty_knvv.
    select *
      from knvv
        into table it_knvv.
    loop at it_knvv into wa_knvv.
      kunid = wa_knvv-kunnr+5(5).
      wa_knvv-kunid = kunid.
      MOdify it_knvv TRANSPORTING kunid.
      read table zcust into wa_zcust with key resid = wa_knvv-kunid.
    endloop.

  • Query based on "NATURAL JOIN" statement of Oracle9i

    I have created following 4 tables
    create table CUST_MASTER
    (CUST_NO NUMBER(3),CUST_NAME VARCHAR2(20),CUST_CITY VARCHAR2(20));
    create table ITEM_MASTER
    (ITEM_NO NUMBER(2),ITEM_NAME VARCHAR2(20),
    PRICE NUMBER(4));
    create table ORDER_HEADER
    ( ORDER_NO NUMBER(4), CUST_NO NUMBER(3));
    create table ORDER_ITEMS
    ( ORDER_NO NUMBER(4), ITEM_NO NUMBER(2), QTY NUMBER(3));
    Based on the above 4 table I have executed the following query.
    select c.cust_no, c.cust_name, c.cust_city, oh.order_no, i.item_no, i.item_name, i.price, oi.qty
    from cust_master c, item_master i, order_header oh, order_items oi
    where c.cust_no = oh.cust_no and oh.order_no = oi.order_no and oi.item_no = i.item_no;
    How should I build similar query in Oracle9i making use of "NATURAL JOIN" statement?

    Hallo,
    yes you are correct.
    From SQL Reference
    NATURAL JOIN The NATURAL keyword indicates that a natural join is being performed. A natural join is based on all columns in the two tables that have the same name. It selects rows from the two tables that have equal values in the relevant columns
    Nothing about foreign keys.
    Test:
    select * from scott.emp natural join scott.dept
        DEPTNO      EMPNO ENAME      JOB               MGR HIREDATE                  SAL       COMM DNAME          LOC         
            20           7369     SMITH          CLERK                7902     17.12.1980            800                    RESEARCH           DALLAS
            30           7499     ALLEN          SALESMAN             7698     20.02.1981           1600            300     SALES              CHICAGO
            30           7521     WARD           SALESMAN             7698     22.02.1981           1250            500     SALES              CHICAGO
            20           7566     JONES          MANAGER              7839     02.04.1981           2975                    RESEARCH           DALLAS
            30           7654     MARTIN         SALESMAN             7698     28.09.1981           1250           1400     SALES              CHICAGO
            30           7698     BLAKE          MANAGER              7839     01.05.1981           2850                    SALES              CHICAGO
            10           7782     CLARK          MANAGER              7839     09.06.1981           2450                    ACCOUNTING         NEW YORK
            20           7788     SCOTT          ANALYST              7566     19.04.1987           3000                    RESEARCH           DALLAS
            10           7839     KING           PRESIDENT                     17.11.1981           5000                    ACCOUNTING         NEW YORK
            30           7844     TURNER         SALESMAN             7698     08.09.1981           1500              0     SALES              CHICAGO
            20           7876     ADAMS          CLERK                7788     23.05.1987           1100                    RESEARCH           DALLAS
            30           7900     JAMES          CLERK                7698     03.12.1981            950                    SALES              CHICAGO
            20           7902     FORD           ANALYST              7566     03.12.1981           3000                    RESEARCH           DALLAS
            10           7934     MILLER         CLERK                7782     23.01.1982           1300                    ACCOUNTING         NEW YORK
    alter table scott.emp drop constraint fk_deptno
    select * from scott.emp natural join scott.dept
        DEPTNO      EMPNO ENAME      JOB               MGR HIREDATE                  SAL       COMM DNAME          LOC         
            20           7369     SMITH          CLERK                7902     17.12.1980            800                    RESEARCH           DALLAS
            30           7499     ALLEN          SALESMAN             7698     20.02.1981           1600            300     SALES              CHICAGO
            30           7521     WARD           SALESMAN             7698     22.02.1981           1250            500     SALES              CHICAGO
            20           7566     JONES          MANAGER              7839     02.04.1981           2975                    RESEARCH           DALLAS
            30           7654     MARTIN         SALESMAN             7698     28.09.1981           1250           1400     SALES              CHICAGO
            30           7698     BLAKE          MANAGER              7839     01.05.1981           2850                    SALES              CHICAGO
            10           7782     CLARK          MANAGER              7839     09.06.1981           2450                    ACCOUNTING         NEW YORK
            20           7788     SCOTT          ANALYST              7566     19.04.1987           3000                    RESEARCH           DALLAS
            10           7839     KING           PRESIDENT                     17.11.1981           5000                    ACCOUNTING         NEW YORK
            30           7844     TURNER         SALESMAN             7698     08.09.1981           1500              0     SALES              CHICAGO
            20           7876     ADAMS          CLERK                7788     23.05.1987           1100                    RESEARCH           DALLAS
            30           7900     JAMES          CLERK                7698     03.12.1981            950                    SALES              CHICAGO
            20           7902     FORD           ANALYST              7566     03.12.1981           3000                    RESEARCH           DALLAS
            10           7934     MILLER         CLERK                7782     23.01.1982           1300                    ACCOUNTING         NEW YORK
    ALTER TABLE scott.emp
    ADD CONSTRAINT fk_deptno FOREIGN KEY (deptno)
    REFERENCES SCOTT.dept (deptno)
    ENABLE NOVALIDATE
    /Regards
    Dmytro Dekhtyaryuk

  • Updateable scrollable result sets with join statement

    I am writing a generic GUI fronend for any database that has a JDBC2.0 driver available.
    I have been using scrollable updateable result sets. These work well for individual tables but as soon as two tables are linked either implicitly or explicitly with a join statement the result set meta data isDefinitelyWriteable is set to false thus preventing the result set from being updated.
    Assuming I am using the JDBC-ODBC driver with java sdk1.4.0 and MS Access (although I have used other databases and JDBCs I assume that the one mentioned will be a common combination and needs to work) is there any way of getting linked tables to be updateable with scollable result sets.
    I am using scrollable result sets since this prevents the necessity of putting the data in a secondary data store.
    I am able to link tables programmatically by requerying the linked table with a new where clause each time the cursor moves in the linked table but this seems rather wasteful. This method is not vey satisfactory when attempting to display data from more than one table which have more than one linked level (i.e. cascaded links).
    Is there a simple solution to this problem or do I have to do a rewrite using an update statement instead of having an updateable result set. I assume this method would also require the result set to be reloaded after the update.
    Any suggestions much appreciated.

    I am trying to make the GUI as flexible as possible by constructing "views" which if necessary link tables on one field in each table. This is fine for two tables but when linking to several tables the information thats produced cannot be read easily because as it stands the information from each table is displayed on a separate tabbed page. This mechanism allows me to keep each record set for each table separate and updateable.
    Since I could see that this was not very user friendly in the way that it displayed the data I decided to try and introduce a join on two or more tables and hence the introduction of the current problem.
    I mentioned that the objective was to be flexible and therefore I also allow queries to be written by the user to facilitate for any shortfalls of the automatic query construction produced by using the "views" mechanism.
    So the answer to your question is yes I do control the SQL selections with one mechanism but ultimately no I do not because I provide a fail safe which allows the user to enter arbitary SQL.
    I only really want a solution for the controlled SQL construction mechanism where I create the link between two or more tables. As mentioned earlier these are linked on one field only but I wish to provide the option of displaying the result in a single table (tabbed page) rather than spread across multiple tabbed pages.

  • AP name is not consistent in APs Join Stats.

    hi all,
    i have strange issue in my controller (WISM2) AP name is not consistent in APs Join Stats page while the naming is consistent in Wireless  status page.as shown below.

    Thanks,
    The problem solved after i cleared the access point from join AP stats. then restart it after that will join with normal name.

  • Does 'For All Entries in itab' work exactly like 'Join' statement?

    Hi,
    I would like to know that if 'For All Entries in itab' work exactly like 'Join' statement?
    If yes, then when I use 'For All Entries in itab' and a 'Join' statement seperately with the same logical conditions for both, the number of records returned by the two methods are not same. Ideally, they should both return the same number of recs.
    Can somebody help?
    With regards.

    Hi,
    for all entries will not work in the same way unless untill it should satisfy some conditions,
    it has some pre-requisests...
    like in the select clause or in where clause or in both the cluases, there should be entire key..
    then only it will behave like the join statement..
    hope i am clear.
    please revert back if u have any quiries.
    Regards,
    Sunil Kumar Mutyala.

  • Sql complex join statements

    I have been seraching for reference material that show
    an illustration of ER Diagrams and SQL join statements.
    I am usually developing queries from looking at an ER diagram and no other documentation. The books I am finding only have simple easy SQL join examples. I can no find a book that shows difficult join queries or relate join queries with ER diagrams. I am stuck in one case
    where I have a table that relates to two other tables but there is a third table in common with both of them.
    How should this join statement look?
    Sometime I code the join logic correct but the parentheses
    are in the wrong place. Can you help on this one as well?

    NLV, 
    Is this still an issue?
    If so, please read Visakh16's last
    response. We need more information.
    [Personal Site] [Blog] [Facebook]

  • Replicas stuck in join state 0

    After a partition merge, all replicas are stuck in join state 0. Have been this way for about a week.
    Any Help would be appreciated.
    Thanks.

    Well, if you have a server in your tree which is down ( e.g. s server you shut off 5 years ago, but neglected to delete its NCP server object ) that will cause the issue, as well as a time sync issue. So these issues you can check and fix. Have you? If its not that, then you have a stuck partition operation. Something is preventing it from transitioning. That could be corruption or one of a number of preconditions have fallen apart. Do you see any errors showing up when you SET DSTRACE = ON and SET DSTRACE = +SYNC +PART You can use the "cancel partition operation" and hope that work, which it may well not. But if it does not, you will have a broken directory and have burnt all your bridges - especially if you 5 minutes later discover that 5 year old dead server object I spoke of.
    What can Novell do that you cannot? A lot. And the guy you will be deal with sees 10 totally messed up beyond all comprehension eDir systems a day and can likely fix your issue in his sleep. IF you "followed the directions" and made a backup of eDir on each server and done a health check prior to the Merge, Novell can use the backups to restore the masters. They have the ability to edit the databases underneath to force things.
    We've all been where you are now. Looking for the Easy Button. But you need to rapidly go through the stages of grieving an get to the acceptance stage an call Novell.
    -- Bob

  • Inner join Statement

    Hi All,
    I am using this inner Join Statement
          SELECT *
          FROM vbap AS a INNER JOIN vbak AS b
                         ON avbeln = bvbeln
                         AND amandt = bmandt
          WHERE a~vbeln IN lr_vbeln AND
                a~posnr IN lr_posnr AND
                a~aedat IN lr_aedat AND
              ( b~vbtyp EQ 'A' OR
                b~vbtyp EQ 'B' OR
                b~vbtyp EQ 'G' ).
    But when i see the Output, One of the field ERDAT is geeting picked up from VBAK table instead of VBAP table.
    Any pointers on how can we select the ERDAT from VBAP using this inner Join.
    I dont want to use 2 select stetements.
    Regards,
    Mayank

    In Inner join, it is always advisable to put the header table as First table and secondly, it should be advisable to fetch only the requried fields. I believe that you might not required all the fields of VBAP table.
    Select A~Vbeln
              A~vbtyp
              B~posnr
              B~erdat
              into table I_VBAP
              from vbak as A innner Join VBAP as B
              on  a~vbeln = b~vbeln
              where   a~vbeln IN lr_vbeln AND
                          b~posnr IN lr_posnr AND
                          b~aedat IN lr_aedat AND
                         ( a~vbtyp EQ 'A' OR
                           a~vbtyp EQ 'B' OR
                           a~vbtyp EQ 'G' ).

  • Cross-join statements

    Can someone explain me, why result of this query:
    select * from  sys.v_$sql
    where hash_value in
          (select hash_value from sys.v_$sql_plan
           where options = 'CARTESIAN' and operation LIKE '%JOIN%' )
    order by hash_value; is that statement:
    select count(1) from all_objects where object_name = :1;My point to run this query is find all cartesian join statements.

    I am not sure but may it has something to do with multiple tables used through the view "all_objects" ...
    Regards

  • Regarding join statement

    SELECT  vbrkvbeln vbfavbelv
              vbrkfkdat vbrkkunag
              vbrpwerks kna1name1
                  FROM vbrk
                  INNER JOIN vbrp ON vbrkvbeln = vbrpvbeln
                  INNER JOIN vbfa ON vbfavbeln = vbrkvbeln
                  INNER JOIN kna1 ON kna1kunnr = vbrkkunag
                  INTO CORRESPONDING FIELDS OF TABLE INVTAB
                  WHERE vbrp~werks IN s_werks
                  AND vbrk~fkdat IN s_fkdat
                  AND vbrk~kunag IN s_kunag
                  AND vbrk~fkart IN ('ZF2','FVAT')
                  AND vbrk~fksto <> 'X'
                  AND vbrk~rfbsk = 'C'
                  AND vbrk~sfakn = ''
                  AND vbrk~inco1 = 'TWO'
                  AND VBRK~VKORG NE '5000'
                  AND vbfa~vbtyp_n = 'M'
                  AND vbfa~vbtyp_v = 'J'.
    i am writng this join statement
    but when displaying i am getting multiple records for a particular entry
    say for a particular documnet number if it is having N no. of line itmes and when searching in vbfa thenumber of preeecig documents are M then the resords that it is disp;laying is N*M.
    but in some cases it is showing records more than this
    can you please help me why it is showing more records.
    and in if you can explain me with an example with a dummy no of records how it will fetch record that will be a great help.

    Hi Ashish,
    please chk the following links for inner and outer join ,
    http://help.sap.com/saphelp_erp2004/helpdata/en/cf/21ec77446011d189700000e8322d00/frameset.htm
    http://www.sap-img.com/abap/several-nested-inner-join-statements-can-be-inefficient.htm
    http://www.thespot4sap.com/Articles/SAPABAPPerformanceTuning_InnerJoinStatement.asp
    now how to write join statements
    SELECT
    a~ebeln
    a~ebelp
    a~matnr
    a~txz01
    a~menge
    a~meins
    a~netpr
    a~brtwr
    a~netwr
    a~mwskz
    a~navnw
    a~effwr
    a~werks
    b~eindt
    FROM
    ekpo AS a
    INNER JOIN
    eket AS b
    ON
    aebeln = bebeln
    INTO
    TABLE t_ekpo
    WHERE
    a~ebeln = t_ekko_temp-ebeln
    AND b~ebeln = t_ekko_temp-ebeln.
    Think there are 2 tables . a right hand table and a left hand table.
    in inner join : only those fields having coresponding values in the left hand side table are selected.
    in outer join all the fields on the left hand side table are selected and the fields which have no corresponding values in the right hand side table are displayed as null values..
    Reward points if helpful
    Regards ,
    Amber S

  • Query tuning - update with join statement

    Hi,
    I have 2 tables (table1 and table 2) with following condition:
    TABLE 1 - Need to selection a column say staus ! = 'C'
    join emp_id and project_id of TABLE1 and TABLE2
    From join I am selecting minimun and maximum of TABLE2.salary and updating TABLE3 respective column.
    Now as per requirement I have to update required column on TABLE2.EMP_ID = TABLE3.EMP_ID
    Following is the query I have written,
    update TABLE3 T3
    set (T3.MIN_salary, T3.MAX_salary) = (
    select min(c.salary), max(c.salary)
    from TABLE2 t2, TABLE1 t1
    where t2.emp_id = t1.emp_id
    and t2.project_id = t1.project_id
    and t1.status != 'C'
    and t2.emp_id = t3.emp_id)
    The above query takes 7 min to update some 8 lakhs records. Can you please suggest some other optimized method to do so?
    Thanks in advance
    Sandeep

    Hi SBH
    Required info for "Please provide oracle version, indexes on the 3 tables and also stats info for the table from user_tables"
    1) Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit
    2) Index is not there on TABLE3 (It is temporary table)
    Composite index on TABLE 1 - column1, column2 - Index name say table1index1
    Composite index on TABLE 2 - column1, column2 - Index name say table2index1
    TABLE_NAME     NUM_ROWS     BLOCKS     DEGREE     INSTANCES     SAMPLE_SIZE     PARTITIONED
    FXO_CFW     11767341     144930     1     1     11767341     NO
    FXO_RSL_STBL               1     1          NO
    FXO_TRN     4917778     230196     1     1     4917778     NO
    execution plan is:
    Execution Plan
    Plan hash value: 3131833900
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | UPDATE STATEMENT | | 789K| 23M| 2397 (3)| 00:00:29 |
    | 1 | UPDATE | TABLE3 | | | |
    | 2 | TABLE ACCESS FULL | TABLE3 | 789K| 23M| 2397 (3)| 00:00:29 |
    | 3 | SORT AGGREGATE | | 1 | 28 | | |
    | 4 | TABLE ACCESS BY INDEX ROWID | TABLE2 | 1 | 17 | 3 (0)| 00:00:01 |
    | 5 | NESTED LOOPS | | 1 | 28 | 12 (0)| 00:00:01 |
    |* 6 | TABLE ACCESS BY INDEX ROWID| TABLE1 | 1 | 11 | 9 (0)| 00:00:01 |
    |* 7 | INDEX RANGE SCAN | IDX_TABLE1INDEX1 | 6 | | 3 (0)| 00:00:01 |
    |* 8 | INDEX RANGE SCAN | IDX_TABLE2INDEX1 | 1 | | 2 (0)| 00:00:01 |
    Thanks for quick response,
    Sandeep

  • Printing the header of a report  and checking the join statement...

    Hi All,
    This report of mne is working fine but there are things that i want to include: when i print this report i want it to display heading of which is in the html_top_of_page but it does not print it, i am not familiar with smart forms so i won't be able to use smart forms.If anyone can help me out i will really appreciated and will immediately reward points......please help me guys as the due date is very close...and please check the joins on the select statement, not sure whether i've joined the tables properly!
    REPORT  Z_ORDER_AUDIT_NEW.
    TYPE-POOLS: SLIS.
                   LIST OF TABLES
    Tables: vbak, likp, vbrk, kna1, vbrp, nast.
                         DEFINITION OF FIELDS                               *
    DATA: itb_fieldcat TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
          w_fieldcat TYPE slis_fieldcat_alv.
    DATA: w_cnt LIKE sy-tabix.
    DATA: lv_layout TYPE slis_layout_alv,
          lv_tab_group TYPE slis_t_sp_group_alv,
          lv_repid LIKE sy-repid,
          lv_events TYPE slis_t_event,
          lv_print  TYPE slis_print_alv,
          lv_user   TYPE slis_formname VALUE 'USER_COMMAND'.
             SELECTION SCREEN                                                 *
    SELECT-OPTIONS:
      s_lfdat FOR likp-lfdat,
      s_imwrk FOR likp-imwrk.
                 DEFINITION OF AN INTERNAL TABLE                              *
    DATA: Begin of i_ordertab occurs 0,
             audat  LIKE vbak-audat,
             vbeln  LIKE vbak-vbeln,
             lfdat  LIKE likp-lfdat,
             erdat  LIKE likp-erdat,
             vbeln1 LIKE likp-vbeln,
             vbeln2 LIKE vbrk-vbeln,
             erdat1 LIKE vbrk-erdat,
             kunrg  LIKE vbrk-kunrg,
             name1  LIKE kna1-name1,
             regio  LIKE vbrk-regio,
             inco1  LIKE vbrk-inco1,
             vrkme  LIKE vbrp-vrkme,
             ntgew  LIKE vbrp-ntgew,
             gewei  LIKE vbrp-gewei,
             volum  LIKE vbrp-volum,
             voleh  LIKE vbrp-voleh,
             netwr  LIKE vbrk-netwr,
           End of i_ordertab.
                   READ THE DATA                                               *
      SELECT vbakaudat vbakvbeln likplfdat likperdat likpvbeln vbrkvbeln
             vbrkerdat vbrkkunrg kna1name1 vbrkregio vbrkinco1 vbrpvrkme
             vbrpntgew vbrpgewei vbrpvolum vbrpvoleh vbrk~netwr
             FROM likp join kna1 on likpkunnr EQ kna1kunnr
                       join vbrk on kna1kunnr EQ vbrkkunrg
                       join vbrp on vbrkvbeln EQ vbrpvbeln
                       join vbak on kna1kunnr EQ vbakkunnr
             INTO i_ordertab
             WHERE likp~lfdat IN s_lfdat
             AND   likp~imwrk IN s_imwrk.
             APPEND i_ordertab.
      ENDSELECT.
                         PERFORM STATEMENTS                                   *
    PERFORM build_fieldcatalog.
    PERFORM build_layout.
    PERFORM build_event.
    PERFORM build_print.
    PERFORM display_alv_report.
    FORM build_fieldcatalog.
      w_cnt = 1.
      itb_fieldcat-fieldname = 'AUDAT'.
      itb_fieldcat-seltext_m = 'Document date'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      itb_fieldcat-key       = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'VBELN'.
      itb_fieldcat-seltext_m = 'S/O number'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'LFDAT'.
      itb_fieldcat-seltext_m = 'Deliv. date'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'ERDAT'.
      itb_fieldcat-seltext_m = 'Del. Crt Date'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'VBELN1'.
      itb_fieldcat-seltext_m = 'Delivery No.'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'VBELN2'.
      itb_fieldcat-seltext_m = 'Invoice No.'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'ERDAT1'.
      itb_fieldcat-seltext_m = 'Invoice Date'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'KUNRG'.
      itb_fieldcat-seltext_m = 'Payer'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'NAME1'.
      itb_fieldcat-seltext_m = 'Payer-Description'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'REGIO'.
      itb_fieldcat-seltext_m = 'Region'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'INCO1'.
      itb_fieldcat-seltext_m = 'Incoterms'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'VRKME'.
      itb_fieldcat-seltext_m = 'Billed Quantity'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'NTGEW'.
      itb_fieldcat-seltext_m = 'Net weight'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'GEWEI'.
      itb_fieldcat-seltext_m = 'Weight Unit'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'VOLUM'.
      itb_fieldcat-seltext_m = 'Volume'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'VOLEH'.
      itb_fieldcat-seltext_m = 'VOLUME UNIT'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
      w_cnt = w_cnt + 1.
      itb_fieldcat-fieldname = 'NETWR'.
      itb_fieldcat-seltext_m = 'Net Value'.
      itb_fieldcat-col_pos   = w_cnt.
      itb_fieldcat-emphasize = 'X'.
      itb_fieldcat-ref_tabname  = 'VBRK' .
      itb_fieldcat-ref_fieldname = 'NETWR' .
      itb_fieldcat-do_sum = 'X' .
      APPEND itb_fieldcat TO itb_fieldcat.
      CLEAR itb_fieldcat.
    ENDFORM.
    FORM build_layout.
        lv_layout-no_input           = 'X'.
        lv_layout-colwidth_optimize  = 'X'.
        lv_layout-totals_text        = 'Totals'(201).
        lv_layout-detail_popup       = 'X'.
    ENDFORM.
    FORM build_event.
      DATA i_event TYPE slis_alv_event.
      CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
         EXPORTING
           i_list_type = 0
         IMPORTING
           et_events   = lv_events[].
       READ TABLE lv_events WITH KEY name = slis_ev_user_command
                            INTO i_event.
       if sy-subrc = 0.
         MOVE lv_user TO i_event-form.
         APPEND i_event TO lv_events.
       endif.
    ENDFORM.
    FORM build_print.
         lv_print-reserve_lines = '2'.
         lv_print-no_coverpage  = 'X'.
    ENDFORM.
    *&          FUNCTION ALV DISPLAY
    FORM display_alv_report.
    lv_repid = sy-repid.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
       I_CALLBACK_PROGRAM             = lv_repid
       I_CALLBACK_USER_COMMAND        = slis_ev_user_command
       I_CALLBACK_HTML_TOP_OF_PAGE    = 'HTML_TOP_OF_PAGE' " see FORM
       I_CALLBACK_HTML_END_OF_LIST    = 'END_OF_LIST_HTML'   "see FORM
       IS_LAYOUT                      = lv_layout
       IT_FIELDCAT                    = itb_fieldcat[]
       IT_SPECIAL_GROUPS              = lv_tab_group
       I_SAVE                         = 'X'
       IT_EVENTS                      = lv_events
       IS_PRINT                       = lv_print
      TABLES
        t_outtab                      = i_ordertab
      EXCEPTIONS
        PROGRAM_ERROR                 = 1
        OTHERS                        = 2.
    IF sy-subrc <> 0.
    ENDIF.
    ENDFORM.
    *&      Form  html_top_of_page
    FORM html_top_of_page USING document TYPE REF TO cl_dd_document.
      DATA: text TYPE sdydo_text_element.
      CALL METHOD document->add_gap
        EXPORTING
          width = 100.
      text =  'Cadbury: Daily Audit of Orders'.
      CALL METHOD document->add_text
        EXPORTING
          text      = text
          sap_style = 'HEADING'.
      CALL METHOD document->new_line.
      CALL METHOD document->new_line.
      CALL METHOD document->new_line.
      text = 'User Name : '.
      CALL METHOD document->add_text
        EXPORTING
          text         = text
          sap_emphasis = 'Strong'.
      CALL METHOD document->add_gap
        EXPORTING
          width = 6.
      text = sy-uname.
      CALL METHOD document->add_text
        EXPORTING
          text      = text
          sap_style = 'Key'.
      CALL METHOD document->add_gap
        EXPORTING
          width = 50.
      text = 'Date : '.
      CALL METHOD document->add_text
        EXPORTING
          text         = text
          sap_emphasis = 'Strong'.
      CALL METHOD document->add_gap
        EXPORTING
          width = 6.
      text = sy-datum.
      CALL METHOD document->add_text
        EXPORTING
          text      = text
          sap_style = 'Key'.
      CALL METHOD document->add_gap
        EXPORTING
          width = 50.
      text = 'Time : '.
      CALL METHOD document->add_text
        EXPORTING
          text         = text
          sap_emphasis = 'Strong'.
      CALL METHOD document->add_gap
        EXPORTING
          width = 6.
      text = sy-uzeit.
      CALL METHOD document->add_text
        EXPORTING
          text      = text
          sap_style = 'Key'.
    ENDFORM.
    FORM end_of_list_html USING end TYPE REF TO cl_dd_document.
    DATA: ls_text TYPE sdydo_text_element,
        l_grid TYPE REF TO cl_gui_alv_grid,
        f(14) TYPE c VALUE 'SET_ROW_HEIGHT',
        w_lines type i,
        w_lines1(9) type n.
    DESCRIBE TABLE i_ordertab LINES w_lines.
    w_lines1 = w_lines.
    concatenate: 'TOTAL NUMBER OF RECORDS SELECTED:  ' w_lines1 INTO ls_text
                                  SEPARATED BY space.
    adds test (via variable)
       CALL METHOD end->add_text
      EXPORTING
        text = ls_text
        sap_emphasis = 'strong'.
    adds new line (start new line)
       CALL METHOD end->new_line.
    *set height of this section
       CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
         e_grid = l_grid.
        CALL METHOD l_grid->parent->parent->(f)
        EXPORTING
          id = 3
          height = 14.
    ENDFORM. "end_of_list_html.
    --END OF THE REPORT--

    Hi
    1. first remove the ENDSELECT..use INTO table itab or INTO CORRESPONDING fields, Since you are joining the  more tables this will take lot of time.
    2. You are just using two fields from LIKP as selection screen  fields to fetch the so much data.
    see the table linkings apartfrom KUNNR between the tables
    LIKP-VBELN = LIPS-VBELN
    LIPS-VGBEL = VBAK-VBELN and  LIPS-VGPOS = VBAP-POSNR
    and
    VBRP-AUBEL = VBAK-VBELN and VBRP-AUPOS = VBAP-POSNR and
    VBRP-VGBEL = LIKP-VBELN  and VBRP-VGPOS = LIPS-POSNR
    use the above links and code again.
    <b>Reward points for useful Answers</b>
    Regards
    Anji

  • Using CASE in the join statement in the AND clause. Where's the problem?

    All,
    I have my code, where based on the case statement, I would like to restrict the number of records in my JOIN condition. It's the CASE statement that I have used, that's giving me the problem. Not sure, what the problem is. The error says (in my case expression):
    Msg 102, Level 15, State 1, Line 59
    Incorrect syntax near '>'.
    Select a.Col1, a.Col2, b.Col1, b.Col2, b.Col3
    From Table1 a
    Join Table2 b
    On a.id = b.id
    Join Table3 c
    On c.FiltId = a.FiltId
    And
    Case
    When b.Col3 <> 5 --b.Col3 is an INT field
    Then (b.OutDate >= DATEADD(DAY, -30, c.InsertDate) And b.OutDate <= DATEADD(DAY, 30, c.InsertDate))
    When b.Col3 = 5
    Then (b.InDate >= DATEADD(DAY, 5, c.InsertDate))
    End
    Join Table 4 -- Other Join conditions and where clause conditions

    You do not know the CASE is an expression and fields are not columns. This is fundamental and probably means that a lot of your code needs work. 
    You can probably re-write the query with something like this: 
    SELECT A.col1, A.col2, B.col1, B.col2, B.col3
      FROM Table_1 AS A, Table_2 AS B, Table_3 AS C
     WHERE A.generic_id = B.generic_id
       AND C.foobar_id = A.foobar_id
       AND CASE
           WHEN B.col3 <> 5 
                AND (B.something_out_date 
                     <= DATEADD(DAY, -30, C.something_insertion_date) 
           THEN 'T'
           WHEN B.col3 = 5 
                AND (B.something_out_date 
                    >= DATEADD(DAY, -30, C.something_insertion_date) 
           THEN 'T'
           ELSE 'F' END = 'T' ;
    --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

  • Problem of using formula in select join  statement

    I  would like to use following statement but I can not.(do you have an example of an statement where this wuld work):
    SELECT 
    lipsprodh  sum(lipslfimg * lipsumvkz / lipsumvkn)  sum(lipsvolum)  lipsvtweg  likp~wadat_ist
    INTO CORRESPONDING
    FIELDS OF TABLE itab FROM
    lips
    JOIN likp 
    ON lipsvbeln = likpvbeln
    WHERE lips~vkorg = '0200'.

    Hi  ,
    types: BEGIN OF s_itab      ,
           prodh  LIKE lips-prodh,
           lfimg  LIKE lips-lfimg,
           umvkz  LIKE lips-umvkz,
           umvkn  LIKE lips-umvkn,
           volum  LIKE lips-volum,
           vtweg  LIKE lips-vtweg,
           wadat_ist LIKE likp-wadat_ist,
           END OF s_itab.
    DATA : temp   type p decimal  2 ,
           temp1  type p decimal 2 ,
           temp2  type p decimal 2 ,
           itab   type table of s_itab ,
           wt_tab type s_itab .
    SELECT lips~prodh
                 lips~lfimg
                 lips~umvkz
                 lips~umvkn
                 lips~volum
                 lips~vtweg
                 likp~wadat_ist
    INTO CORRESPONDING FIELDS OF TABLE  itab
    FROM  ( lips INNER JOIN likp ON lipsvbeln = likpvbeln )
    WHERE lips~vkorg = '0200'.
    loop at itab inro wt_tab .
    temp   =  ( wt_tab-lfimg * wt_tab-umvkz ) / lips~umvkn  .
    temp1 = temp1 + temp .
    temp2 = temp2 + wt_tab .
    endloop.
    Hope it helped you .
    Message was edited by:
            Lakshminarayanan rohini

Maybe you are looking for

  • Is it possible to syc my photos from the iphone4 to cpu or itunes to free up space?

    Is it possible to syc my photos from the iphone4 to cpu or itunes to free up space?

  • Is the Swing Package in Java obsolete?

    hey guys, i'm just a newbie java enthusiast here and all the while i've been learning to manipulate data in minimal ways using the Swing package especially the JOptionPane class. is this class/package already obsolete? a person here in school told me

  • HTML insertion

    For some time now when I want to insert a video from YouTube, the application quits. I insert the HTML code, do OK, it starts to "load" and Muse left. I tried with different videos, it is the same, even those that are already on my website. No proble

  • Strange window popup on every roundtrip to server

    hello i have 2004s web dynpro application outside the portal (via url address). sometimes when user change lead selection of a table or making any other action that requires roundtrip to the server, i see a little pop up window that starts and close.

  • Change date format to European

    Currently the date on the top bar appears to be American format. Can someone advise how to change to European format. Thanks!