我有像这样的SQL查询
SELECT TOP 1
ROW_NUMBER() OVER (ORDER BY column_date DESC) AS ROWID, *
FROM
table_report
WHERE
column_date = datepart(year,date)
如果我错了,请更正。我希望从datetime获取年份,然后逐年选择前1行
答案 0 :(得分:0)
您可以通过向窗口函数添加分区来选择每年的第一行(按照您提供的顺序),然后过滤每年的第一行:
SELECT * FROM
(
SELECT ROW_NUMBER() OVER
(PARTITION BY datepart(year,date) ORDER BY column_date DESC) AS ROWID,
*
FROM table_report
) X
WHERE ROWID = 1;