如题:例如有表A如下:
field1 field2
中国 1021
美国 2123
越南 21
英国 2144现在需要增加一列count,并且按照field2的值排序,并将排名的大小值写入count,执行结果如下:field1 field2 count
英国 2144 1
美国 2123 2
中国 1021 3
越南 21 4这个SQL该怎么写呀?????
field1 field2
中国 1021
美国 2123
越南 21
英国 2144现在需要增加一列count,并且按照field2的值排序,并将排名的大小值写入count,执行结果如下:field1 field2 count
英国 2144 1
美国 2123 2
中国 1021 3
越南 21 4这个SQL该怎么写呀?????
go
update t
set count=(select count(1) from t1 where field2!<t.field2)
from t1 t
insert into p select '中国',1021
insert into p select '美国',2123
insert into p select '越南',21
insert into p select '英国',2144select a.field1,a.field2,[count]=(select count(1)+1 from p where p.field2>a.field2) from p a order by field2 desc
set count=(select count(1) from t1 where field2!>t.field2) --改为!>
from t1 t
go
alter table t1 add count int
go
05:
select field1 , field2,[count]=row_number()over(order by field2 desc)
from t1
order by field2 asc
insert into p select '中国',1021
insert into p select '美国',2123
insert into p select '越南',21
insert into p select '英国',2144
insert into p select '小国',nullselect a.field1,a.field2,[count]=(select count(1)+1 from p where isnull(p.field2,'')>isnull(a.field2,'')) from p a order by case when field2 is not null then field2 else 1 end desc
insert into tb values('中国',1021)
insert into tb values('美国',2123)
insert into tb values('越南',21)
insert into tb values('英国',2144)
go
SELECT *,[count] = (SELECT COUNT(DISTINCT field2) FROM tb WHERE field2 >= a.field2)
FROM tb a
ORDER BY [count]drop table tb/*
field1 field2 count
---------- ----------- -----------
英国 2144 1
美国 2123 2
中国 1021 3
越南 21 4(所影响的行数为 4 行)
*/
create table p(field1 varchar(10),field2 int)
insert into p select '中国',1021
insert into p select '美国',2123
insert into p select '越南',21
insert into p select '英国',2144
insert into p select '小国',0
insert into p select '大国',0
insert into p select '哈国',1select a.field1,a.field2,[count]=(select count(1)+1 from p where p.field2>a.field2) from p a
order by isnull(field2,0) desc
insert into @t select '中国 ',1021
insert into @t select '美国 ',2123
insert into @t select '越南 ',21
insert into @t select '英国 ',2144
insert into @t select '小国 ',null select a.field1,a.field2,
[count]=(select count(1)+1 from @t where cast(isnull(field2,'0')as int) >=cast(a.field2 as int))
from @t a order by [count]
declare @t table (field1 varchar(10),field2 varchar(10))
insert into @t select '中国 ',1021
insert into @t select '美国 ',2123
insert into @t select '越南 ',21
insert into @t select '英国 ',2144
insert into @t select '小国 ',null select a.field1,a.field2,
[count]=(select count(1)+1 from @t where cast(isnull(field2, '0 ')as int) >=cast(a.field2 as int))
from @t a order by [count]
/*
field1 field2 count
---------- ---------- -----------
小国 NULL 1
英国 2144 2
美国 2123 3
中国 1021 4
越南 21 5
*/
你的结果不对,正确的应该为:
field1 field2 count
---------- ---------- -----------
英国 2144 1
美国 2123 2
中国 1021 3
越南 21 4
小国 NULL 5
insert into p select '中国 ',1021
insert into p select '美国 ',2123
insert into p select '越南 ',21
insert into p select '英国 ',2144
insert into p select '小国 ',null select a.field1,a.field2,[count]=(select count(1)+1 from p where isnull(p.field2, ' ') >isnull(a.field2, ' ')) from p a order by case when field2 is not null then field2 else 1 end desc 或者:
create table p(field1 varchar(10),field2 int)
insert into p select '中国 ',1021
insert into p select '美国 ',2123
insert into p select '越南 ',21
insert into p select '英国 ',2144
insert into p select '小国 ',0 select a.field1,a.field2,[count]=(select count(1)+1 from p where p.field2 >a.field2) from p a
order by isnull(field2,0) desc