SQL Server:选择不同的最大(日期)组

时间:2012-10-05 12:34:41

标签: sql database select distinct

这是我的表offs

date ----------   oid ------   head -----  cnt

2012-1-9 -----13      --------  10   -----------1
2012-1-11 --- 13      --------  6  -----------  2
2012-1-22 --- 13      --------  10   -----------3
2012-1-22 --- 11      --------  10   -----------4

我需要一个select函数,结果是max(date)来自oid和不同的头部,就像那样:

2012-1-11 --- 13      --------  6  -----------  2
2012-1-22 --- 13      --------  10   -----------3
2012-1-22 --- 11      --------  10   -----------4

1 个答案:

答案 0 :(得分:1)

这个查询怎么样:

SELECT a.*, b.cnt from(
SELECT MAX(Date) AS Date, oid, head
from offs group by oid, head
)a inner join offs b on a.Date=b.Date and a.oid=b.oid and a.head=b.head