sql求最大,Orz
sflbh sj sl
050102 04.00
060101 01.00
050102 74.00
050102 112.00
050102 125.00
050102 132.00
050102 141.00
050102 161.00
060101 161.00
050102 202.00
060101 201.00
050102 234.00
select
*
from
TB t
where
not exists(select 1 from TB where sflbh=t.sflbh and sl>t.sl)
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
GO
create table [TB]([sflbh] varchar(6),[sj] int,[sl] numeric(3,2))
insert [TB]
select '050102',0,4.00 union all
select '060101',0,1.00 union all
select '050102',7,4.00 union all
select '050102',11,2.00 union all
select '050102',12,5.00 union all
select '050102',13,2.00 union all
select '050102',14,1.00 union all
select '050102',16,1.00 union all
select '060101',16,1.00 union all
select '050102',20,2.00 union all
select '060101',20,1.00 union all
select '050102',23,4.00
select
sflbh,
[sj]=STUFF((SELECT ','+RTRIM(sj) FROM TB WHERE t.[sflbh]=[sflbh] FOR XML PATH('')),1,1,''),
[sl]=max(sl) from TB t
group by sflbh
drop table [TB]
select sflbh,max(sl),sj from 表1
group by sflbh,sj
select a.sflbh,a.sl,a.sj
from [table] a
inner join
(select sflbh,max(sl) 'maxsl'
from [table] group by sflbh) b
on a.sflbh=b.sflbh and a.sl=b.maxsl