求两语句,马上结贴
下面是表数据
a b c d Re
1 1 1 1 1
1 2 1 1 2
2 2 1 2 1
3 3 1 1 1
3 1 2 1 2
b本来没有RE列 我想吧B,c,d排序后 如果a有1两条的话 依次得到Re数据1,2
有两条数据怎么不用临时表把其中一条删除
数据为
a b c
1 1 1
1 1 1
在线等!
------解决方案--------------------create table ta(a int,b int,c int, d int)
insert ta
select 1 , 1 , 1 , 1
union select 1 , 2 , 1 , 1
union select 2 , 2 , 1 , 2
union select 3 , 3 , 1 , 1
union select 3 , 1 , 2 , 1
go
select a,b,c,d,RE=(select count(1) from ta where a=t.a and b<=t.b) from ta t
go
drop table ta
/* 结果
a b c d RE
----------- ----------- ----------- ----------- -----------
1 1 1 1 1
1 2 1 1 2
2 2 1 2 1
3 1 2 1 1
3 3 1 1 2
(所影响的行数为 5 行)
*/
------解决方案--------------------SQL code
create table tb(a int, b int, c int, d int)
insert into tb values(1, 1, 1, 1)
insert into tb values(1, 2, 1, 1)
insert into tb values(2, 2, 1, 2)
insert into tb values(3, 3, 1, 1)
insert into tb values(3, 1, 2, 1)
go
select * , re=(select count(1) from tb where a=t.a and (c<t.c or (c=t.c and b<t.b)) )+1 from tb t
drop table tb
/*
a b c d re
----------- ----------- ----------- ----------- -----------
1 1 1 1 1
1 2 1 1 2
2 2 1 2 1
3 3 1 1 1
3 1 2 1 2
(所影响的行数为 5 行)
*/