brunch

You can make anything
by writing

C.S.Lewis

by 동네아밥 Nov 11. 2021

배치 재고 SQL 조회하기

이상하게 길고 복잡하단 말이지

    SELECT matnr,

           werks,

           lgort,

           charg,

           lfgja,

           lfmon,

           clabs

      FROM mchb

      INTO TABLE @DATA(lt_mchb)

     WHERE werks EQ werks

       AND matnr EQ matnr

       AND lgort EQ lgort

       AND charg EQ charg

       AND ( ( lfgja = @sy-datum(4) AND lfmon <= @sy-datum+4(2) )

        OR lfgja < @sy-datum(4) )

       AND ( clabs <> 0 OR cinsm <> 0 OR cspem <> 0 ).

    SELECT matnr,

           werks,

           lgort,

           charg,

           lfgja,

           lfmon,

           clabs

      FROM mchbh

      APPENDING CORRESPONDING FIELDS OF TABLE @lt_mchb

     WHERE werks EQ werks

       AND matnr EQ matnr

       AND lgort EQ lgort

       AND charg EQ charg

       AND ( ( lfgja = @sy-datum(4) AND lfmon >= @sy-datum+4(2) )

        OR lfgja > @sy-datum(4) ).

    SORT lt_mchb BY matnr werks lgort charg lfgja lfmon.

    DELETE: ADJACENT DUPLICATES FROM lt_mchb COMPARING matnr werks lgort charg,

            lt_mchb WHERE clabs = 0.

**************************************************************************************

  SELECT werks, 

     matnr, 

 lgort, 

 labst, 

 insme, 

 speme,

         lfgja, 

 lfmon

    INTO TABLE @DATA(lt_mard)

    FROM mard

   WHERE werks EQ werks

     AND matnr EQ matnr

     AND lgort EQ lgort

     AND ( ( lfgja = @sy-datum(4) AND lfmon <= @sy-datum+4(2) )

      OR lfgja < @sy-datum(4) )

     AND ( labst <> 0 OR insme <> 0 OR speme <> 0 ).

  SELECT werks, 

 matnr, 

 lgort, 

 labst, 

 insme, 

 speme,

         lfgja, 

 lfmon

    APPENDING CORRESPONDING FIELDS OF TABLE @lt_mard

    FROM mardh

   WHERE werks EQ werks

     AND matnr EQ matnr

     AND lgort EQ lgort

     AND ( ( lfgja = @sy-datum(4) AND lfmon >= @sy-datum+4(2) )

      OR lfgja > @sy-datum(4) ).

  SORT lt_mard BY matnr werks lgort lfgja lfmon.

  DELETE ADJACENT DUPLICATES FROM lt_mard

                        COMPARING matnr werks lgort.

  DELETE lt_mard WHERE labst = 0

                   AND insme = 0

                   AND speme = 0.

작가의 이전글 배치특성정보 SQL로 선택하기
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari