Wrong results of SELECT with OR condition

648 views Asked by At

It seems that as result the program is getting all sales orders (from VBAK table) only with AUART = 'YI01', and indeed I am getting 1.699.698 sales orders. So it is ignoring the other parameters in select VTWEG = 'Z1', SPART = 'Z1', KUNNR = '0230001039', ERDAT = GT '01.09.2017' for which I should get only 77 results.

TYPES: BEGIN OF lw_odv_vbak,
        vbeln TYPE vbak-vbeln,
        vkorg TYPE vbak-vkorg,
        vtweg TYPE vbak-vtweg,
        spart TYPE vbak-spart,
        netwr TYPE vbak-netwr,
        kunnr TYPE vbak-kunnr,
        erdat type vbak-erdat,
      END OF lw_odv_vbak.


DATA: it_odv_vbak TYPE STANDARD TABLE OF lw_odv_vbak INITIAL SIZE 0.



SELECT vbeln
     FROM vbak
      INTO TABLE it_odv_vbak
      WHERE vkorg = 'Z326'
      AND vtweg = 'Z1'
      AND spart = 'Z1'
      AND kunnr = '0230001039'
      AND auart = 'YC01' OR
          auart = 'YI01'
        AND erdat GT '01.09.2017'.

Thanks a lot for your help

3

There are 3 answers

0
Oguz On

You can do two things: 1) Use parentheses around OR 2) Change date format to '20170901'. In db, dates are stored like YYYYMMDD.

SELECT vbeln
 FROM vbak
  INTO TABLE it_odv_vbak
  WHERE vkorg = 'Z326'
  AND vtweg = 'Z1'
  AND spart = 'Z1'
  AND kunnr = '0230001039'
  AND ( auart = 'YC01' OR auart = 'YI01') 
  AND erdat GT '20170901'.
0
vwegert On

AND has a higher priority than OR, so you're effectively selecting for

( vkorg = 'Z326' AND vtweg = 'Z1' AND spart = 'Z1' AND kund = '0230001039' AND auart = 'YC01' )
  OR
( auart = 'YI01' AND erdet GT '01.09.2017' )

which is most certainly not what you intended to do. Use parentheses around your OR clause.

0
András On

Use parentheses around the OR

SELECT vbeln
     FROM vbak
      INTO TABLE it_odv_vbak
      WHERE vkorg = 'Z326'
      AND vtweg = 'Z1'
      AND spart = 'Z1'
      AND kunnr = '0230001039'
      AND ( auart = 'YC01' OR
          auart = 'YI01' )
        AND erdat GT '01.09.2017'.