Sql 查看组合列

Sql view combining columns

我将如何进行视图或计算 table 将采用此方法。 table 1

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

如果 send 1 或 send 2 为真,将创建一个视图或 table 看起来像这样。

location  state
west        WA
east        NY
central     ND
central     TX

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

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

请注意,您的原始 table 包含两次状态,我分别将其称为 state1 和 state2。 creates/inserts 用于测试:

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');