[英]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:年和月列(
GJAHR
和MONAT
)是数字文本列(从 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 :说明:
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.)编辑:(当然,您也可以分多个步骤选择数据,使用
SELECT
和SELECT ... FOR ALL ENTRIES ...
,或SELECT
和LOOP
。您必须考虑并测试哪种解决方案最有效,易于理解并适合您的问题。)
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.