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

时间:2017-07-20 08:33:07

标签: max sap abap opensql

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

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给我带来以下几行

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

3 个答案:

答案 0 :(得分:0)

我认为这件事有两种解决方案。  1)您可以将年度字段添加到数据库表。并将此字段添加到group by语句。

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.

答案 1 :(得分:-1)

您需要的是group by而非dat,但按月和年 - 此条款将有效:

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

答案 2 :(得分:-1)

您好,感谢您的回答。 我通过做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 Time,特别是当每个bukrs,kunnr和date有超过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 ).

再次感谢 利亚