当前位置: 代码迷 >> Sql Server >> sql计算列有关问题
  详细解决方案

sql计算列有关问题

热度:38   发布时间:2016-04-27 15:17:00.0
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)goinsert 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 tbdrop table tb/*a           b           c           d           ----------- ----------- ----------- ----------- 0           1           1           10           2           1           00           1           2           01           1           1           02           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都要有数据。。。。
  相关解决方案