简体   繁体   中英

Selecting the lowest value of a character string in a grouped data

I'm trying to create a SAP Query (SQ02).

My dataset is grouped based on a value of a field (example below). I need to select and output the lowest value of another field, but its a mixed character - Letter + a number, I need the lowest number.

I have this piece of code right now, but it only brings back the value attached to the master account and the logic fails in the master account does not have the lowest value.

Select MIN( KLABC )
  FROM KNVV
  INTO CLS2
  WHERE KUNNR IN
    ( SELECT KUNNR
      FROM KNB1
      WHERE EKVBD = KNB1-EKVBD ).

IF cls2+1(1) > KNVV-KLABC+1(1) AND KNVV-KLABC+0(1) = 'R'.
  clear CLS2.
  Select MIN( KLABC )
    FROM KNVV
    INTO CLS2
    WHERE KLABC LIKE 'R%'
      AND KUNNR IN
        ( SELECT KUNNR
          FROM KNB1
          WHERE EKVBD = KNB1-EKVBD ).
ENDIF.

Example dataset:

Customer  Class  Group
1001      R1     1001
301048    R2     1001
10015     A1     10015
306069    A2     10015
6042482   R1     10025
10027     R1     10027
303226    R2     10027
10028     A4     10028
306070    A4     10028
10034     A2     10034
303724    A5     10034
403558    A7     10034
5042      A1     10047
302673    A3     10047

The intermediate result would correspond to something like SELECT MIN( class ) AS Class2, group FROM abovetable GROUP BY group would give:

Class2 Group
R1     1001
A1     10015
R1     10025
R1     10027
A4     10028
A2     10034
A1     10047

By merging the intermediate result, the final result would look something like this:

Customer  Class  Group  Class2
1001      R1     1001   R1
301048    R2     1001   R1
10015     A1     10015  A1
306069    A2     10015  A1
6042482   R1     10025  R1
10027     R1     10027  R1
303226    R2     10027  R1
10028     A4     10028  A4
306070    A4     10028  A4
10034     A2     10034  A2
303724    A5     10034  A2
403558    A7     10034  A2
5042      A1     10047  A1
302673    A3     10047  A1

Just the number would suffice too.

The following code will bring back the lowest valued entry in the group.

Select MIN( KLABC )
  FROM KNVV
  INTO CLS2
  WHERE KLABC LIKE 'R%'
    AND KUNNR IN
      ( SELECT KUNNR
        FROM KNB1
        WHERE EKVBD = KNB1-EKVBD 
          AND KUNNR IN 
            ( SELECT KUNNR
              FROM KNA1
              WHERE LOEVM NE 'X' ) ).
IF CLS2 = ''.
  Select MIN( KLABC )
    FROM KNVV
    INTO CLS2
    WHERE KLABC LIKE 'A%'
      AND KUNNR IN
        ( SELECT KUNNR
          FROM KNB1
          WHERE EKVBD = KNB1-EKVBD 
            AND KUNNR IN 
              ( SELECT KUNNR
                FROM KNA1
                WHERE LOEVM NE 'X' ) ).
  IF CLS2 = ''.
    Select MIN( KLABC )
      FROM KNVV
      INTO CLS2
      WHERE KUNNR IN
        ( SELECT KUNNR
          FROM KNB1
          WHERE EKVBD = KNB1-EKVBD 
            AND KUNNR IN 
              ( SELECT KUNNR
                FROM KNA1
                WHERE LOEVM NE 'X' ) ).
  ENDIF.
ENDIF.

As another requirement I also had to make the sure Customer class 1st letter matches with the result field, I've created an extra field with this code. It will match the original Class Letter and the value picked for the group by the above code.

CLEAR CLS1.
IF CLS2 NE ' '
      AND KNVV-KLABC+0(1) = 'R' OR KNVV-KLABC+0(1) = 'A'.
  CONCATENATE KNVV-KLABC+0(1) CLS2+1(1)
    INTO CLS1.
ELSE.
  CLS1 = CLS2.
ENDIF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM