出了点问题,我意识到我没有得到我想要的。我在表格中有以下几行:
0000527746 1000 10.06.2017 20170718100757.5010080
0000527746 1000 10.06.2017 20170718100757.5039300
0000527746 1000 11.06.2017 20170718100839.9209480
0000527746 1000 11.06.2017 20170718100906.3337170
0000527746 1000 24.07.2017 20170718095843.3555610
0000527746 1000 24.07.2017 20170718100209.2203570
0000527746 1000 24.07.2017 20170718100757.4970390
我想选择每个月的最后一天,即我希望选择给我带来以下几行
0000527746 1000 11.06.2017 20170718100906.3337170
0000527746 1000 24.07.2017 20170718100757.4970390
我使用以下sql
select bukrs kunnr dat max( time ) as time
from zcollectoraction into corresponding fields of table it_collectoraction
where bukrs = p_bukrs and
kunnr in so_kunnr and
dat in so_date
group by bukrs kunnr dat.
但它显示以下行
0000527746 1000 11.06.2017 20170718100906.3337170
0000527746 1000 11.06.2017 20170718100906.3337170
0000527746 1000 24.07.2017 20170718100757.4970390
为了每月有 1 条线,该怎么办?
我认为这个问题有两种解决方案。 1( 您可以将年月字段添加到数据库表中。并将此字段添加到按语句分组。
0000527746 1000 10.06.2017 20170718100757.5010080 201706
0000527746 1000 10.06.2017 20170718100757.5039300 201706
0000527746 1000 11.06.2017 20170718100839.9209480 201706
0000527746 1000 11.06.2017 20170718100906.3337170 201706
0000527746 1000 24.07.2017 20170718095843.3555610 201707
0000527746 1000 24.07.2017 20170718100209.2203570 201707
0000527746 1000 24.07.2017 20170718100757.4970390 201707
select bukrs kunnr dat max( time ) as time
from zcollectoraction into corresponding fields of table
it_collectoraction
where bukrs = p_bukrs and
kunnr in so_kunnr and
dat in so_date
group by bukrs kunnr dat yearmonth.
2(您可以选择所有数据并排列在循环语句中。或者你可以使用旧的选择查询根本不重要。
select bukrs kunnr dat time
from zcollectoraction into corresponding fields of table
it_collectoraction
where bukrs = p_bukrs and
kunnr in so_kunnr and
dat in so_date .
loop at it_collectoraction into data(ls_coll).
delete it_collectoraction[] WHERE dat(6) = ls_coll-dat(6)
and dat < = ls_coll-dat
and time < ls_coll-time.
endloop.
你需要的不是dat
group by
,而是按月和年 - 这个子句将起作用:
GROUP BY bukrs, kunnr, MONTH(dat), YEAR(dat)
您好,感谢您的回答。 我通过执行 2 个选择解决了这个问题。 在第一个我得到一个月的最后一天或几天,以下选择
select bukrs kunnr yearmonth max( dat ) as dat
from zcollectoraction into corresponding fields of table it_collectoraction
where bukrs = p_bukrs and
kunnr in so_kunnr and
dat in so_date
group by bukrs kunnr yearmonth.
然后我对内部表进行了循环以填充剩余数据并选择所有记录的 MAX 时间,尤其是当每个 bukrs、kunnr 和日期超过 1 行时。
select single * from zcollectoraction
into corresponding fields of wa_collectoraction
where bukrs = wa_collectoraction-bukrs and
kunnr = wa_collectoraction-kunnr and
dat = wa_collectoraction-dat and
time = ( select max( time ) as time
from zcollectoraction
where bukrs = wa_collectoraction-bukrs and
kunnr = wa_collectoraction-kunnr and
dat = wa_collectoraction-dat ).
再次感谢 伊莱亚斯