Sql视图组合列

时间:2015-12-22 22:59:18

标签: sql sql-server-2008 join union

我将如何创建一个视图或计算表来获取此信息。 表1

location   send1T   state send2TF   state 
west         T       WA     F        OR
east         F       FL     T        NY
central      T       ND     T        TX

如果发送1或发送2为真,则会创建一个看起来像这样的视图或表。

location  state
west        WA
east        NY
central     ND
central     TX

1 个答案:

答案 0 :(得分:3)

只需执行以下操作(Postgres):

create view myview (location, state) as
(select location, state1 from table1 where send1T = true)
union
(select location, state2 from table1 where send2tf = true);

请注意,您的原始表包含状态两次,我分别将其称为state1和state2。用于测试的创建/插入:

create table table1 (location varchar, send1T boolean, state1 varchar, send2tf boolean, state2 varchar);
insert into table1 values ('west', true, 'WA', false, 'OR');
insert into table1 values ('east', false, 'FL', true, 'NY');
insert into table1 values ('central', true, 'ND', true, 'TX');