SQL:如何每天为每个组选择最大值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8230485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:22:06  来源:igfitidea点击:

SQL: How to select a max value for each group per day?

sqlsql-serverselect

提问by mike

Let's say I have a table that has the following columns...

假设我有一个包含以下列的表...

Name, Date, Number

And say we had the following data inserted into these columns...

假设我们将以下数据插入到这些列中......

Bob, 2011-11-22, 1
Bob, 2011-11-22, 5
Bob, 2011-11-22, 4
Bob, 2011-11-22, 3
Wendy, 2011-11-22, 3
Wendy, 2011-11-22, 4
Wendy, 2011-11-22, 2
Wendy, 2011-11-22, 1
Chris, 2011-11-22, 4
Chris, 2011-11-22, 1
Bob, 2011-11-21, 4
Bob, 2011-11-21, 3
Wendy, 2011-11-21, 2
Wendy, 2011-11-21, 4
Wendy, 2011-11-21, 1
Chris, 2011-11-21, 4
Chris, 2011-11-21, 1

Now what I'd like to do is get the max Number value for each Name, for each date. So my query result would look like this...

现在我想做的是为每个日期获取每个 Name 的最大 Number 值。所以我的查询结果看起来像这样......

Bob, 2011-11-22, 5
Wendy, 2011-11-22, 4
Chris, 2011-11-22, 4
Bob, 2011-11-21, 4
Wendy, 2011-11-21, 4
Chris, 2011-11-21, 4

Any help would be appreciated. I'm using SQL 2005.

任何帮助,将不胜感激。我正在使用 SQL 2005。

回答by Code Magician

What about

关于什么

SELECT [Name], [Date], MAX(Number)
FROM [yourTable]
GROUP BY [Name], [Date] 

See:

看:

回答by flesk

It's not as hard as you'd think.

这并不像你想象的那么难。

select name, date, max(number) from table group by name, date

回答by Joe Stefanelli

SELECT Name, Date, MAX(Number)
    FROM YourTable
    GROUP BY Name, Date;

回答by Marc B

SELECT Name, `Date`, MAX(Number)
FROM yourtable
GROUP BY Name, `Date`