IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]SQL SERVER 2000/2005/2008 一句话完成分组后加序号--【叶子】

    maco_wang发表于 2012-02-06 14:18:30
    love 0
    --测试数据
    declare @T table (id int identity,col varchar(5))
    insert into @T
    select 'AAA' union all
    select 'AAA' union all
    select 'BBB' union all
    select 'CCC' union all
    select 'CCC'
    
    --SQL SERVER 2005/2008
    select row_number() over (partition by col order by id) as num,col from @T
    /*
    num                  col
    -------------------- -----
    1                    AAA
    2                    AAA
    1                    BBB
    1                    CCC
    2                    CCC
    */
    
    --SQL SERVER 2000
    select (select count(1) from @T where col=t.col and id<=t.id) as num ,col from @T t
    /*
    num         col
    ----------- -----
    1           AAA
    2           AAA
    1           BBB
    1           CCC
    2           CCC
    */




沪ICP备19023445号-2号
友情链接