declare @table table
(
id int,
[name] varchar(10),
value int
)
insert into @table
select 124,'55',77
union select 33,'78',67
union select 234,'33',77
union select 55,'55',67
union select 78,'66',89
union select 22,'33',77
select * from @table
select * from @table
where (case len([name]) when 10 then [name] else REPLICATE('0', 10-len([name]))+[name] end )+(case len(Convert(varchar(10),value)) when 10 then Convert(varchar(10),value) else REPLICATE('0', 10-len(Convert(varchar(10),value)))+Convert(varchar(10),value) end )+ (case len(Convert(varchar(10),id)) when 10 then Convert(varchar(10),id) else REPLICATE('0', 10-len(Convert(varchar(10),id)))+Convert(varchar(10),id) end ) in (
select max((case len([name]) when 10 then [name] else REPLICATE('0', 10-len([name]))+[name] end )+(case len(Convert(varchar(10),value)) when 10 then Convert(varchar(10),value) else REPLICATE('0', 10-len(Convert(varchar(10),value)))+Convert(varchar(10),value) end )+ (case len(Convert(varchar(10),id)) when 10 then Convert(varchar(10),id) else REPLICATE('0', 10-len(Convert(varchar(10),id)))+Convert(varchar(10),id) end ))
from @table
group by [name])
够复杂,还是找个格式化工具更易看明白
SELECT id, name, MAX(value) FROM Info GROUP BY name ORDER BY id;//天知道对不对, 呵呵