在SQL中以ID,Month级别准备数据

问题描述 投票:-1回答:2

我有一个像这样的表:

ID  Date
1   10/04/2015
1   28/04/2015
1   14/07/2015
1   30/07/2015
1   30/08/2015
2   10/04/2016
2   28/04/2016
2   14/05/2016
2   30/05/2016

但我想实现如下:

ID  Date
1   28/04/2015
1   30/07/2015
1   30/08/2015
2   28/04/2016
2   30/05/2016

请你帮助我好吗 。

sql sql-server-2012
2个回答
0
投票

试试这个:

select * from (
    select id,
           [Date],
           row_number() over (partition by id, datepart(month, [date]) order by [Date] desc) [rn]
    from (
    select id, 
           --date convrsion, 103 - British/French - your style
           convert(date, [date], 103) [Date]
    from @MyTable
    ) a
) b where rn = 1 

0
投票

我没有真正得到预期结果的逻辑,但下面的查询将起作用。

SELECT *
FROM yourTable 
WHERE DAY([Date])>=28;

How to get Day, Month and Year Part from DateTime in Sql Server

© www.soinside.com 2019 - 2024. All rights reserved.