现在我有一张表A,有字段a,b,c
现在我要做得是把c = a +2
有个前提条件是b必须相等,
因为表A是先按照b,再按照a排列的
例如:
a b c
-------------------
1 1
2 1
3 1
4 2
5 3
结果应该为:
a b c
-------------------
1 1 3
2 1 4
3 1 5
4 2 null
5 3 null
现在我要做得是把c = a +2
有个前提条件是b必须相等,
因为表A是先按照b,再按照a排列的
例如:
a b c
-------------------
1 1
2 1
3 1
4 2
5 3
结果应该为:
a b c
-------------------
1 1 3
2 1 4
3 1 5
4 2 null
5 3 null
解决方案 »
- 请教一个很菜的问题--mdf文件的大小问题
- sql里所有记录同一字段的字符串合并的问题,谢谢
- 我想找一个能在XP上安装的SQL2000!谁能告诉我!
- 集合C=集合B-集合A 的查询怎样写?
- 查了很多帖子也无法解决:MDB导入SQL server中总是报错
- 向邹老师请教一个两台sqlserver 2005数据库服务器数据同步的问题,谢谢
- 新事务不能登记到指定的事务处理器中
- sqlserver 2005 大数据插入优化
- 如何看以前版的数据库文件data.dat,想知道文件内的表情况
- 怎么样在erwin里建一个自动增长的integer
- 关于SqlServer 2005的一个分析出错的问题
- 来看看 ..
from a t
declare @s table (a int,b int,c int)
insert into @s
select 1,1,null union all
select 2,1,null union all
select 3,1,null union all
select 4,2,null union all
select 5,3,nullselect a,b,c=case when exists(select 1 from @s where b=a.b and a!=a.a) then a+2 else null end from @s a
--结果
a b c
----------- ----------- -----------
1 1 3
2 1 4
3 1 5
4 2 NULL
5 3 NULL
insert @tb select 1,1,null
union all select 2,1,null
union all select 3,1,null
union all select 4,2,null
union all select 5,3,null update @tb set c=a+2 from @tb a where exists(select 1 from @tb where b=a.b and a<>a.a)
select * from @tb/*
a b c
----------- ----------- -----------
1 1 3
2 1 4
3 1 5
4 2 NULL
5 3 NULL(所影响的行数为 5 行)*/
-------------------
1 1
2 1
3 1
4 2
5 3
结果应该为:
a b c
-------------------
1 1 3
2 1 4
3 1 5
4 2 null
5 3 null select a,b,c=a+2 from tb t where exists(select 1 from tb where b = t.b and a <> t.a)
create table #t(a int, b int, c int)
insert into #t
select 1, 1 ,null union all
select 2, 1 ,null union all
select 3, 1 ,null union all
select 4, 2 ,null union all
select 5, 3 ,nullupdate #t
set c=a+2
from #t a
where exists(select 1 from #t where a.b=b group by b having count(1)>1)select * from #t
/*
1 1 3
2 1 4
3 1 5
4 2 NULL
5 3 NULL(5 行受影响)
*/
drop table tb
go
create table tb(a int,b int,c int)
go
insert into tb(a,b)
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,2 union all
select 5,3
goselect * from tbupdate tb set c=a+2 from tb t1 where (select count(*) from tb where b=t1.b)>1
SET c=aa.a+2
FROM TableA aa INNER JOIN
(
SELECT b
FROM TableA
GROUP BY b
Having COUNT(b)>1
) bb
ON aa.b=bb.b
insert @tb select 1,1,null
union all select 2,1,null
union all select 3,1,null
union all select 4,2,null
union all select 5,3,null select a,b,c=case when exists(select 1 from @tb where b = t.b and a <> t.a) then a+2 else null end from @tb t