如何使用 SQL 获取每月的最大日期

How to get the max date per month using SQL

出了点问题,我意识到我没有得到我想要的。我在 table 中有以下几行:

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

我想 select 每个月的最后一天,即我希望 select 给我带来以下几行

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 行?

您需要的是 group by 而不是 dat,而是按月和年 - 此子句有效:

GROUP BY bukrs, kunnr, MONTH(dat), YEAR(dat)

您好,感谢您的回答。 我通过执行 2 selects 解决了这个问题。 在第一天,我使用以下 selection

获得本月的最后一天或几天
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.

然后我循环到内部 table 以填充剩余数据和 select 所有记录的最大时间,尤其是当每个 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 ).

再次感谢 埃利亚斯

这个问题我觉得有两种解决方法。 1) 您可以将 yearmonth 字段添加到您的数据库 table。并将此字段添加到 group by statement.

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所有的数据并排列在循环语句中。或者您可以使用旧的 select 查询根本没有关系。

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.