gpt4 book ai didi

sql - 获取具有 SQL Server 2008 列的 MAX 值的整个 ROW

转载 作者:行者123 更新时间:2023-12-04 20:26:53 25 4
gpt4 key购买 nike

Sample

我想在 SQL Server 2008 中执行上述操作。有什么想法吗?

最佳答案

像这样?

设置:

declare @MyTable table(Year int, Month int, Day int, Total int)

insert @MyTable
values
(2005, 9, 23, 12),
(2005, 9, 26, 5),
(2005, 9, 24, 1),
(2005, 9, 15, 28),
(2005, 9, 21, 1),
(2005, 9, 13, 1),
(2005, 10, 31, 5),
(2005, 11, 18, 115),
(2005, 11, 20, 1),
(2005, 11, 11, 1),
(2005, 11, 19, 1)

查询:

;with cte
as
(
select *,
row_number() over(partition by Year, Month order by Total desc) RowNumber
from @MyTable
)
select Year, Month, Day, Total
from cte
where RowNumber = 1

输出:

Year        Month       Day         Total
----------- ----------- ----------- -----------
2005 9 15 28
2005 10 31 5
2005 11 18 115

关于sql - 获取具有 SQL Server 2008 列的 MAX 值的整个 ROW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com