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

求助,关于分组求最大值的问题, 在线等
id jiquid date price
101 10101 2012-07-10 00:00:00.000 6.71
101 10101 2012-08-10 00:00:00.000 NULL
101 10101 2012-08-13 00:00:00.000 7.01
101 10101 2012-09-01 00:00:00.000 7.03
101 10101 2012-09-11 00:00:00.000 7.46
102 10201 2012-07-10 00:00:00.000 NULL
102 10201 2012-08-10 00:00:00.000 7.02
103 10315 2012-07-10 00:00:00.000 6.93
108 10801 2012-07-11 00:00:00.000 7.28
108 10801 2012-09-11 00:00:00.000 7.72
109 10901 2012-07-11 00:00:00.000 6.88
109 10901 2012-08-10 00:00:00.000 7.19
109 10901 2012-09-11 00:00:00.000 7.63
112 11201 2012-07-31 00:00:00.000 7.05
112 11201 2012-08-01 00:00:00.000 7.36
112 11201 2012-08-10 00:00:00.000 7.36
112 11201 2012-08-11 00:00:00.000 7.80
201 20101 2012-07-10 00:00:00.000 6.96
201 20101 2012-08-10 00:00:00.000 7.27
201 20101 2012-09-11 00:00:00.000 7.71
202 20201 2012-07-10 00:00:00.000 6.94
202 20201 2012-08-10 00:00:00.000 7.25
203 20301 2012-07-10 00:00:00.000 7.07
203 20301 2012-08-10 00:00:00.000 7.38
206 20601 2012-07-10 00:00:00.000 NULL
206 20601 2012-09-10 00:00:00.000 NULL
301 30101 2012-07-11 00:00:00.000 7.26
301 30101 2012-08-10 00:00:00.000 7.58
301 30101 2012-09-11 00:00:00.000 8.02
302 30201 2012-07-10 00:00:00.000 7.16
303 30301 2012-07-10 00:00:00.000 7.96
303 30301 2012-08-10 00:00:00.000 8.27
303 30301 2012-09-11 00:00:00.000 8.71
402 40201 2012-07-10 00:00:00.000 6.97
402 40201 2012-08-10 00:00:00.000 7.28
402 40201 2012-09-11 00:00:00.000 7.72
403 40301 2012-08-10 00:00:00.000 7.28
404 40401 2012-07-11 00:00:00.000 6.94
404 40401 2012-08-10 00:00:00.000 7.25
404 40401 2012-09-11 00:00:00.000 7.68
501 50101 2012-07-10 00:00:00.000 6.92
501 50101 2012-08-10 00:00:00.000 7.23
501 50101 2012-09-11 00:00:00.000 7.67
502 50201 2012-07-24 00:00:00.000 6.90
502 50201 2012-09-11 00:00:00.000 7.65
503 50301 2012-07-10 00:00:00.000 7.26
503 50301 2012-08-10 00:00:00.000 7.26
503 50301 2012-09-11 00:00:00.000 7.70
603 60301 2012-07-10 00:00:00.000 7.03
603 60301 2012-08-10 00:00:00.000 7.34
603 60301 2012-09-11 00:00:00.000 7.78

数据如上。

以id为分组,求出每个id中date最大的行

想了半天没想出来…


谢谢…

------解决方案--------------------
SQL code
--1
select * from tb as t where not exists(select 1 from tb where id=t.id and date>t.date)
--2
select * from tb as t where date(select max(date) from tb where id=t.id )
--3
select * from (select *,row_id=row_number() over(partition by id order by date desc) from tb) as t where row_id=1

------解决方案--------------------
SQL code
--> --> (Roy)生成測試數據


if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go

--Name相同ID最大的记录:
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID)

方法2:
select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID

方法3:
select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID

方法4:
select a.* from #T a join #T b on a.Name=b.Name and a.ID<=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 

方法5:
select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)

方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0

方法7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc)

方法8:
select * from #T a where ID!<all(select ID from #T where Name=