0

I experienced a strange behaviour, and I already was thinking of looking for a proper shrink. Can anyone explain this one here ?

This select returns, what I want:

SELECT struc~node,struc~tree_level,struc~parent,
       strut~ltext,
       mar~matnr, mar~mtart, mar~matkl,
       mtx~maktx

    FROM            wrf_matgrp_struc AS struc

    LEFT OUTER JOIN wrf_matgrp_strct AS strut
            ON strut~hier_id = struc~hier_id
           AND strut~node    = struc~node

    LEFT OUTER JOIN wrf_matgrp_sku AS sku
            ON sku~hier_id = struc~hier_id
           AND sku~node    = struc~node

    LEFT OUTER JOIN mara AS mar
            ON mar~matnr = sku~matnr

    LEFT OUTER JOIN maw1 AS maw
            ON maw~matnr = mar~matnr

    LEFT OUTER JOIN makt AS mtx
           ON mtx~matnr = mar~matnr
          AND mtx~spras = strut~spras

    INTO TABLE @lt_result_tab
          WHERE struc~hier_id    = @gs_act_hier-hier_id
            AND struc~node      IN @grt_node
            AND struc~date_from <= @sy-datum
            AND struc~date_to   >= @sy-datum
            AND strut~spras      = @sy-langu
            AND mar~matnr       IN @grt_matnr
            AND mar~matkl       IN @grt_matkl
            AND maw~wekgr       IN @grt_wekgr

I remove the join AND mtx~spras = strut~spras and place it into a where condition like this, and it does not work as expected ( resultset is incomplete ).

SELECT struc~node,struc~tree_level,struc~parent,
       strut~ltext,
       mar~matnr, mar~mtart, mar~matkl,
       mtx~maktx

    FROM            wrf_matgrp_struc AS struc

    LEFT OUTER JOIN wrf_matgrp_strct AS strut
            ON strut~hier_id = struc~hier_id
           AND strut~node    = struc~node

    LEFT OUTER JOIN wrf_matgrp_sku AS sku
            ON sku~hier_id = struc~hier_id
           AND sku~node    = struc~node

    LEFT OUTER JOIN mara AS mar
            ON mar~matnr = sku~matnr

    LEFT OUTER JOIN maw1 AS maw
            ON maw~matnr = mar~matnr

    LEFT OUTER JOIN makt AS mtx
           ON mtx~matnr = mar~matnr

    INTO TABLE @lt_result_tab
          WHERE struc~hier_id    = @gs_act_hier-hier_id
            AND struc~node      IN @grt_node
            AND struc~date_from <= @sy-datum
            AND struc~date_to   >= @sy-datum
            AND strut~spras      = @sy-langu
            AND mar~matnr       IN @grt_matnr
            AND mar~matkl       IN @grt_matkl
            AND maw~wekgr       IN @grt_wekgr
            AND mtx~spras        = @sy-langu.
icbytes
  • 1,831
  • 1
  • 17
  • 27
  • 4
    By moving the comparison to the `where` clause, you've turned the `left join` into an `inner join` – HoneyBadger Jun 20 '18 at 11:06
  • Wait, is this REALLY true ? OUTER JOIN = if You find some entries, apply the where clause, else go home. – icbytes Jun 20 '18 at 11:08
  • 1
    If you were using an old system, it would tell you "you cannot put fields from an LEFT JOIN in the WHERE clause"... I guess it's the same for your SQLs :( – VXLozano Jun 20 '18 at 11:10
  • As You already see, this is a new one. And I know the restriction on older systems, therefore I am pretty satisfied , that they extended the where conditions. – icbytes Jun 20 '18 at 11:11
  • I am too, but I was trying to point the fact SAP considers different a condition within the WHERE clause than within the JOIN one, and maybe (maybe, I say) it's the cause of your weird behavior. – VXLozano Jun 20 '18 at 14:21
  • I'm not familiar with SAP, but I've never come across a definition of an `outer join` as you posted. When you use an `outer join`, the outer joined table contains `null` values in all columns for non matching rows. Since `[something] = null` evaluates to `null` (not `true`), the comparison in the `where` filters out all non matching rows: inner join behaviour. I'm confident this is true in SAP as well. – HoneyBadger Jun 20 '18 at 20:46
  • https://stackoverflow.com/a/1019432/1474519 – jcjr Jun 25 '18 at 12:57
  • 2
    Possible duplicate of [Left Join With Where Clause](https://stackoverflow.com/questions/4752455/left-join-with-where-clause) – jcjr Jun 25 '18 at 12:57

0 Answers0