日期:2014-05-17 浏览次数:20365 次
sflbh sj sl
050102 0 4.00
060101 0 1.00
050102 7 4.00
050102 11 2.00
050102 12 5.00
050102 13 2.00
050102 14 1.00
050102 16 1.00
060101 16 1.00
050102 20 2.00
060101 20 1.00
050102 23 4.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