如何在 SQL 中选择每列值的最大值?

发布于 2024-12-06 03:46:23 字数 1426 浏览 0 评论 0原文

在 SQL Server 中,我有一个整数、日期时间和字符串的列表。例如,

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
5       2011-09-22 3:45:01   nameOne
5       2011-09-22 4:56:01   nameOne
5       2011-09-22 5:01:23   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo
4       2011-09-21 3:45:01   nameTwo
4       2011-09-21 4:56:01   nameTwo
4       2011-09-21 5:01:23   nameTwo

我会编写一条 SQL 语句,仅输出每个字符串的数量为最大值的行。在此示例中,

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo

我知道我可以循环字符串列中的每个字符串,然后获取该字符串的最大值,然后选择与该最大值匹配的行。例如,

declare @max int
declare my_cursor cursor fast_forward for
    select distinct string
    from table
open my_cursor
fetch next from my_cursor into @string
while @@fetch_status = 0
begin
    set @max = (select max(number) from table where string = @string)
    select * from table where number = @max
    fetch next from my_cursor into @string
end
close my_cursor
deallocate my_cursor

但是,我想知道是否有一种方法可以使用循环(例如,通过使用聚合函数和分组)来完成此任务WITHOUT

In SQL Server, I have a list of integers, datetimes, and strings. For example,

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
5       2011-09-22 3:45:01   nameOne
5       2011-09-22 4:56:01   nameOne
5       2011-09-22 5:01:23   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo
4       2011-09-21 3:45:01   nameTwo
4       2011-09-21 4:56:01   nameTwo
4       2011-09-21 5:01:23   nameTwo

I would to write a SQL statement that outputs only those rows whose number is a maximum for each string. In this example,

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo

I know that I could loop over each string in the string column, then get the maximum for that string, then select the rows matching that maximum. For example,

declare @max int
declare my_cursor cursor fast_forward for
    select distinct string
    from table
open my_cursor
fetch next from my_cursor into @string
while @@fetch_status = 0
begin
    set @max = (select max(number) from table where string = @string)
    select * from table where number = @max
    fetch next from my_cursor into @string
end
close my_cursor
deallocate my_cursor

However, I am wondering if there is a way to accomplish this task WITHOUT using a loop (e.g., by using aggregate functions and grouping).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-12-13 03:46:23
;WITH T as
(
SELECT *,
       RANK() OVER (PARTITION BY string ORDER BY number DESC) RN
FROM YourTable
)
SELECT number,
       datetime,
       string
FROM T
WHERE RN=1;
;WITH T as
(
SELECT *,
       RANK() OVER (PARTITION BY string ORDER BY number DESC) RN
FROM YourTable
)
SELECT number,
       datetime,
       string
FROM T
WHERE RN=1;
马蹄踏│碎落叶 2024-12-13 03:46:23
WITH maxes AS (
    SELECT string, MAX(number) AS max_number
    FROM tbl
    GROUP BY string
)
SELECT tbl.*
FROM tbl
INNER JOIN maxes
    ON maxes.string = tbl.string AND maxes.max_number = tbl.number
WITH maxes AS (
    SELECT string, MAX(number) AS max_number
    FROM tbl
    GROUP BY string
)
SELECT tbl.*
FROM tbl
INNER JOIN maxes
    ON maxes.string = tbl.string AND maxes.max_number = tbl.number
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文