简体   繁体   English

选择分组数据中字符串的最小值

[英]Selecting the lowest value of a character string in a grouped data

I'm trying to create a SAP Query (SQ02).我正在尝试创建 SAP 查询 (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:中间结果将对应于类似SELECT MIN( class ) AS Class2, group FROM abovetable GROUP BY group会给出:

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.作为另一个要求,我还必须确保 Customer 类的第一个字母与结果字段匹配,我使用此代码创建了一个额外的字段。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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