首页 新闻 会员 周边 捐助

复杂的SQL语句 请求大家帮忙解决

0
悬赏园豆:10 [已关闭问题]

有一张表Info
             id(Primary key)               name             value
               124                           55                77                 
               33                            78                67            
               234                           33                77                             
               55                            55                67              
               78                            66                89    
               22                            33                77
其中id为主键,根据value列中的数据(删除小值,保留大值),删除name列中重复的数据
如果value列中的值相等,则保留id列中较大的值,并且给主键id列升序排序。

雅雪的主页 雅雪 | 初学一级 | 园豆:190
提问于:2009-05-12 21:25
< >
分享
其他回答(2)
0


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])

格古洛 | 园豆:240 (菜鸟二级) | 2009-05-12 22:38
0

够复杂,还是找个格式化工具更易看明白

winzheng | 园豆:8797 (大侠五级) | 2009-05-15 13:24
0
SELECT id, name, MAX(value) FROM Info GROUP BY name ORDER BY id;//天知道对不对, 呵呵

 

陛下 | 园豆:3938 (老鸟四级) | 2009-05-20 09:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册