日期:2014-05-18  浏览次数:20406 次

sql计算列问题
现有一张表有A B C D四个字段都是int类型
D列为计算列
如果A=0并且B=1并且C=1
计算列D=1否则D=0
现在问的是这个计算列怎么写请各位高手指点一下

------解决方案--------------------
create table tb(a int ,b int,c int ,d as case when A=0 and B=1 and C=1 then 1 else 0 end)
go

drop table tb
------解决方案--------------------
SQL code
create table tb(a int ,b int,c int ,d as case when A=0 and B=1 and C=1 then 1 else 0 end)
go

insert into tb (a,b,c) values(0,1,1)
insert into tb (a,b,c) values(0,2,1)
insert into tb (a,b,c) values(0,1,2)
insert into tb (a,b,c) values(1,1,1)
insert into tb (a,b,c) values(2,1,1)

select * from tb

drop table tb

/*
a           b           c           d           
----------- ----------- ----------- ----------- 
0           1           1           1
0           2           1           0
0           1           2           0
1           1           1           0
2           1           1           0

(所影响的行数为 5 行)
*/

------解决方案--------------------

------解决方案--------------------
SQL code

select A,B,case C when A=1 and B=1 then D else C when A=2 and B=2 then E else C end as C,D,E from table 
--前提是D,E都要有数据。。。。