declare @t table(id int,uid int,name varchar(4),province varchar(4),clickcount int)
insert into @t select 1,1001,'aa','广东',100
insert into @t select 2,1002,'bb','广西',120 
insert into @t select 3,1003,'cc','广东',20
insert into @t select 4,1004,'dd','湖南',60
insert into @t select 5,1005,'ee','湖南',50
insert into @t select 6,1006,'ff','湖南',70select 
    t.uid,t.name,t.province,t.clickcount,
    isnull((select count(*) from @t where province=t.province and clickcount>t.clickcount),0)+1 as rankByProvince
from 
    @t t
order by
    province,rankByProvince/*
uid         name province clickcount  rankByProvince 
----------- ---- -------- ----------- -------------- 
1001        aa   广东       100         1
1003        cc   广东       20          2
1002        bb   广西       120         1
1006        ff   湖南       70          1
1004        dd   湖南       60          2
1005        ee   湖南       50          3
*/

解决方案 »

  1.   

    -- SQL 2005中直接用排名函数select 
        t.uid,t.name,t.province,t.clickcount,
    rankByProvince = RANK() OVER(PARTITION BY province ORDER BY clickcount DESC)
    from 
        @t t
    order by
        province,rankByProvince
      

  2.   


    declare @tab table(id int,uid varchar(20),name varchar(20),province  varchar(20),clickcount int)
    insert @tab values(1,'1001','aa','广东',100)
    insert @tab values(2,'1002','bb','广西',120)
    insert @tab values(3,'1003','cc','广东',20)
    insert @tab values(4,'1004','dd','湖南',60)
    insert @tab values(5,'1005','ee','湖南',50)
    insert @tab values(6,'1006','ff','湖南',70)select a.uid,a.name,a.province,a.clickcount,
    [rankByProvince]=(select count(1) from @tab where province=a.province and a.clickcount<=clickcount)
    from @tab a order by province,clickcount desc
      

  3.   

    declare @t1 table(id int, uid int,   name varchar(10),province varchar(10),clickcount int)insert into @t1 select 1,   1001,   'aa',   '广东',    100
    insert into @t1 select 2,   1002,   'bb',   '广西',    120
    insert into @t1 select 3,   1003,   'cc',   '广东',    20
    insert into @t1 select 4,   1004,   'dd',   '湖南',    60
    insert into @t1 select 5,   1005,   'ee',   '湖南',    50
    insert into @t1 select 6,   1006,   'ff',   '湖南',    70select * , rankByProvince=(select count(1) from @t1 where province=a.province and clickcount>a.clickcount)+1 from @t1 a
    order by province,rankByProvinceid          uid         name       province   clickcount  rankByProvince 
    ----------- ----------- ---------- ---------- ----------- -------------- 
    1           1001        aa         广东         100         1
    3           1003        cc         广东         20          2
    2           1002        bb         广西         120         1
    6           1006        ff         湖南         70          1
    4           1004        dd         湖南         60          2
    5           1005        ee         湖南         50          3(所影响的行数为 6 行)
      

  4.   

    zjcxc(邹建)  sql server 2000 中可以吗?有2000中可以运行的sql语句吗?
      

  5.   

    要求TotalclickCount相同的时候,rankbyprovince的排名不会重复,各位大狭们怎么实现,
      

  6.   

    要求TotalclickCount相同的时候,rankbyprovince的排名不会重复,只须把计算排名的where语句修改一下
    where province=a.province and (clickcount>a.clickcount or (clickcount=a.clickcount and id<a.id)
      

  7.   

    declare @t table(id int,uid int,name varchar(4),province varchar(4),clickcount int)
    insert into @t select 1,1001,'aa','广东',100
    insert into @t select 2,1002,'bb','广西',120 
    insert into @t select 3,1003,'cc','广东',20
    insert into @t select 4,1004,'dd','湖南',60
    insert into @t select 5,1005,'ee','湖南',50
    insert into @t select 6,1006,'ff','湖南',70--若先按地名分组排,再按同组内记录排
    select a.* from @t a
    inner join(select province,sum(clickcount) sc from @t group by province) b
    on a.province=b.province
    order by sc,a.province,clickcount desc--若只按同组内记录排
    select a.* from @t a order by province,clickcount desc,id
      

  8.   

    declare @t table(id int,uid int,name varchar(4),province varchar(4),clickcount int)
    insert into @t select 1,1001,'aa','广东',100
    insert into @t select 2,1002,'bb','广西',120 
    insert into @t select 3,1003,'cc','广东',20
    insert into @t select 4,1004,'dd','湖南',60
    insert into @t select 5,1005,'ee','湖南',60
    insert into @t select 6,1006,'ff','湖南',60select 
        t.uid,
        t.name,
        t.province,
        t.clickcount,
        isnull((select 
                    count(*) 
                from @t 
                where 
                    province=t.province and (clickcount>t.clickcount or (clickcount=t.clickcount and uid<t.uid))),0)+1 as rankByProvince
    from 
        @t t
    order by
        province,rankByProvince/*
    uid         name province clickcount  rankByProvince 
    ----------- ---- -------- ----------- -------------- 
    1001        aa   广东       100         1
    1003        cc   广东       20          2
    1002        bb   广西       120         1
    1004        dd   湖南       60          1
    1005        ee   湖南       60          2
    1006        ff   湖南       60          3
    */
      

  9.   

    create table tb(id int, uid int, name char(2), province nvarchar(10), clickcount int)
    insert tb select 1,   1001,   'aa',   '广东',    100
    union all select 2,   1002,   'bb',   '广西',    120 
    union all select 3,   1003,   'cc',   '广东',    20
    union all select 4,   1004,   'dd',  '湖南',    60
    union all select 5,   1005,   'ee',   '湖南',    50
    union all select 6,   1006,   'ff',   '湖南',    70select A.* , rankByProvince=(select count(1) from tb where province=A.province and clickcount>=A.clickcount)  
    from tb as A
    order by province, clickcount desc--result
    id          uid         name province   clickcount  rankByProvince 
    ----------- ----------- ---- ---------- ----------- -------------- 
    1           1001        aa   广东         100         1
    3           1003        cc   广东         20          2
    2           1002        bb   广西         120         1
    6           1006        ff   湖南         70          1
    4           1004        dd   湖南         60          2
    5           1005        ee   湖南         50          3(6 row(s) affected)
      

  10.   

    原来楼主还要排名,我看错了.declare @t table(id int,uid int,name varchar(4),province varchar(4),clickcount int)
    insert into @t select 1,1001,'aa','广东',120
    insert into @t select 2,1002,'bb','广西',120 
    insert into @t select 3,1003,'cc','广东',20
    insert into @t select 4,1004,'dd','湖南',60
    insert into @t select 8,1006,'ff','湖南',110
    insert into @t select 5,1005,'ee','湖南',60
    insert into @t select 6,1006,'ff','湖南',60
    insert into @t select 7,1003,'cc','广东',120select a.*,1+(select count(*) from @t b where b.province=a.province and 
    b.uid<
    case when b.clickcount=a.clickcount then
    a.uid
    else 1000000
    end
    and
    b.clickcount>
    case when b.clickcount=a.clickcount then
    b.clickcount-1
    else
    a.clickcount
    end
    ) cnk from @t a
    order by province,cnk
      

  11.   

    libin_ftsafe(子陌红尘:当libin告别ftsafe) ( ) 接分 谢谢你对这个问题的帮助