简体   繁体   English

在 SELECT 查询中连接两列并获得 MAX

[英]Concatenate two columns in SELECT query and get MAX

I have a table with BUKRS, GJAHR, MONAT and I want to select the MAX GJAHR & MONAT as follow:我有一张包含 BUKRS、GJAHR、MONAT 的表,我想选择 MAX GJAHR 和 MONAT,如下所示:

SELECT MAX( concat( gjahr, monat ) ) AS year_monat
      FROM zfiou_percentage
        INTO data(lv_year_month)
      WHERE bukrs = '5555' AND
            concat( gjahr, monat ) <= '202001'
      GROUP BY year_monat.

So in table we have :所以在表中我们有:

BUKRS  GJAHR  MONAT
5555   2019   09   
5555   2019   10  
5555   2019   11  
5555   2020   02  

How can I get the line 5555 2019 11 ?我怎样才能得到5555 2019 11的线路?

The year and month columns ( GJAHR and MONAT ) are numerical text columns (characters from 0 to 9), but MAX accepts only a numerical (not text) column, so you may use CAST to convert these "texts" to numbers as follows:年和月列( GJAHRMONAT )是数字文本列(从 0 到 9 的字符),但MAX仅接受数字(非文本)列,因此您可以使用CAST将这些“文本”转换为数字,如下所示:

SELECT MAX( CAST( gjahr AS DEC ) * 100 + CAST( monat AS DEC ) ) AS year_monat
      FROM bkpf
      WHERE bukrs = '3001' AND
            concat( gjahr, monat ) <= '202001'
        INTO @DATA(lv_year_month).

It has been tested in an ABAP 7.52 SP 1 system.它已经在 ABAP 7.52 SP 1 系统中进行了测试。

NB: the CAST function accepts the DEC type only since ABAP 7.50.注意: CAST函数仅从 ABAP 7.50 开始接受DEC类型。 I use the table BKPF instead of zfiou_percentage because it exists in all R/3, ECC, S/4 systems.我使用表BKPF而不是zfiou_percentage因为它存在于所有 R/3、ECC、S/4 系统中。

Concatenating the two columns like you are planning to is only possible since version 7.50, as indicated by the documentation .文档所示,仅从 7.50 版开始,就可以像您计划的那样连接两列。 What is your version?你的版本是什么?

If you cannot get it to work or don't have the required ABAP version, you can do the following.如果您无法使用它或没有所需的 ABAP 版本,您可以执行以下操作。 For more information, read up on subqueries .有关更多信息,请阅读子查询

DATA: p_bukrs   TYPE bukrs,
      lt_result TYPE TABLE OF zfiou_percentage.

SELECT *
  FROM zfiou_percentage
  INTO CORRESPONDING FIELDS OF TABLE lt_result
 WHERE bukrs = p_bukrs 
   AND gjahr >= ALL ( SELECT gjahr
                        FROM zfiou_percentage 
                       WHERE bukrs = p_bukrs )
   AND monat >= ALL ( SELECT monat
                        FROM zfiou_percentage 
                       WHERE bukrs = p_bukrs 
                         AND gjahr >= ALL ( SELECT gjahr
                                              FROM zfiou_percentage 
                                             WHERE bukrs = p_bukrs ) ).

Explanation :说明

  • select all datasets选择所有数据集
  • of the z-table z表的
  • into the itab (because there could be multiple rows with the same max gjahr and month)进入 itab(因为可能有多行具有相同的最大 gjahr 和月份)
  • where:在哪里:
    1. normal conditions (make sure to use them in every subquery as well)正常条件(确保在每个子查询中也使用它们)
    2. AND the year is the highest of all years existing in the z-table并且年份是 z 表中存在的所有年份中最高的
    3. AND the month is the highest of all months of the datasets with the highest years AND 月份是所有年份最高的数据集月份中最高的

Edit : (Of course you could also select your data in multiple steps, using SELECT and SELECT ... FOR ALL ENTRIES ... , or SELECT and LOOP . You will have to think about and test which solution will be the most efficient, easy to understand and suited for your problem.)编辑:(当然,您也可以分多个步骤选择数据,使用SELECTSELECT ... FOR ALL ENTRIES ... ,或SELECTLOOP 。您必须考虑并测试哪种解​​决方案最有效,易于理解并适合您的问题。)

SELECT
    gjahr AS year,
    monat AS month
  FROM zfiou_percentage
    INTO TABLE DATA(selected_maxes)
  WHERE bukrs = '5555'
    AND ( gjahr < '2020'
      OR gjahr = '2020' AND monat <= '01' )
  ORDER BY
    gjahr DESC,
    monat DESC
  UP TO 1 ROWS.

DATA(selected_max) = selected_maxes[ 1 ].
DATA(max_date) = |{ selected_max-year }{ selected_max-month }|.

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

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