SQL:如何每天为每个组选择一个最大值? [英] SQL: How to select a max value for each group per day?

查看:173
本文介绍了SQL:如何每天为每个组选择一个最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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...

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

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

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

推荐答案

关于

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

请参阅:

  • GROUP BY (Aggregate) Functions
  • MySQL GROUP BY - Aggregate Functions

这篇关于SQL:如何每天为每个组选择一个最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆