http://bbs.csdn.net/topics/390358384key   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这样子的结果哪位大神给提供下思路

解决方案 »

  1.   


    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
      

  2.   

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