首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问个SQL语句,去重复的

2013-03-01 
请教个SQL语句,去重复的!http://bbs.csdn.net/topics/390358384keyuser----------------------------天津1

请教个SQL语句,去重复的!
http://bbs.csdn.net/topics/390358384

key   user  
----------------------------
天津  1
上海  1
北京  1
湖北  1
天津  2
广州  2
湖南  1
北京  2
安徽  1
湖南  1
北京  3
北京  3
北京  3
北京  3

------------------------------
每个用户 顺序 至多3条
得到
天津  1
上海  1
北京  1
天津  2
广州  2
北京  2
北京  3
北京  3
北京  3

这样子的结果

哪位大神给提供下思路
[解决办法]


with tb(a,b)as(
select '天津',  1 union all
select '上海',  1 union all
select '北京',  1 union all
select '湖北',  1 union all
select '天津',  2 union all
select '广州',  2 union all
select '湖南',  1 union all
select '北京',  2 union all
select '安徽',  1 union all
select '湖南',  1 union all
select '北京',  3 union all
select '北京',  3 union all
select '北京',  3 union all
select '北京',  3)
, tbb as (
select ROW_NUMBER() over(partition by a,b order by a)number,* from tb
)
delete tbb where number>3

[解决办法]
select [key],[user] from (select *,row=row_number()over(partition by [user] order by getdate()) from  tb)t where row<4

[解决办法]
引用:
SQL code?1select [key],[user] from (select *,row=row_number()over(partition by [user] order by getdate()) from  tb)t where row<4



学习了  

热点排行