日期:2014-05-17  浏览次数:20718 次

从ASP.NET版块跑过来问的一个查询问题。


数据如下:


SELECT HistoryId,Score,CONVERT(VARCHAR(10),Add_Datetime,23)as 'Add_Date' 
from Table Group by Add_Datetime


HistoryId    Score       Add_Date
=====================================
1            12          2013-01-02
2            43          2013-01-02
3            50          2013-01-02
4            23          2013-01-02
5            89          2013-01-03
6            77          2013-01-03
7            63          2013-01-03
8            32          2013-01-04
9            90          2013-01-04
10           88          2013-01-04
=====================================

以上是模拟的,跟元素据也差不多了。。

需要得到的结果

HistoryId    Score       Add_Date
=====================================
3            50          2013-01-02
5            89          2013-01-03
9            90          2013-01-04
=====================================
GroupBy 每天 得到 Max(Score),Max(Score)的HistoryId也要得到。

反正结果要同时包含三列都有。

谢谢了。
sql GroupBy Select

------解决方案--------------------
select * from tb t where score=
(select max(score) from tb where t.Add_Date=Add_Date group by Add_Date)
------解决方案--------------------
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (HistoryId int,Score int,Add_Date datetime)
insert into [TB]
select 1,12,'2013-01-02' union all
select 2,43,'2013-01-02' union all
select 3,50,'2013-01-02' union all
select 4,23,'2013-01-02' union all
select 5,89,'2013-01-03' union all
select 6,77,'2013-01-03' union all
select 7,63,'2013-01-03' union all
select 8,32,'2013-01-04