How to get the max date per month

烂漫一生 提交于 2021-01-08 09:46:36

问题


I have a table like

kunnr        date            posnr
30001        28/5/2017       1
30001        25/5/2017       2
30001        15/5/2017       3
30001        25/4/2017       4
30001        20/4/2017       5
30002        15/5/2017       6
30002        25/4/2017       7

I want for every new kunnr to get the record with the max date per month, namely the max for May and max for April etc.

OK, I will sort the table loop at it and for every new kunnr .... how I will get the record for max date for each month?

Thanks in advance Elias

PS: sth went wrong and I realise that I do not get what I want. I have the following lines in a 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

and I want to select the last date of each month namely I want the select to bring me the following lines

0000527746  1000    11.06.2017  20170718100906.3337170
0000527746  1000    24.07.2017  20170718100757.4970390

I use the following 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.

but it displays the following lines

0000527746  1000    11.06.2017  20170718100906.3337170
0000527746  1000    11.06.2017  20170718100906.3337170
0000527746  1000    24.07.2017  20170718100757.4970390

What to do in order to have 1 line per month?

Thanks Elias


回答1:


Why not to use aggregate functions just during select from DB? It is more efficient that looping internal table.

SELECT k~bukrs_vf, k~kunnr, k~erdat, MAX( p~posnr ) AS T
  FROM vbak AS k
  JOIN vbap AS p
    ON k~vbeln = p~vbeln
  INTO TABLE @DATA(lt_result)
 GROUP BY bukrs_vf, kunnr, k~erdat
 ORDER BY bukrs_vf.

Note, that the above query will select only those kunnrs which have corresponding positions in vbap.



来源:https://stackoverflow.com/questions/45001280/how-to-get-the-max-date-per-month

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!