我有三个CASE陈述:
create view J as
select *,
(CASE WHEN A in ('01', ‘03') then TRUE ELSE FALSE END) as X,
(CASE WHEN A = ‘2' then TRUE ELSE FALSE END) as Y,
(CASE WHEN A in ('02', '03') then TRUE ELSE FALSE END) as Z
from
K;
我希望能够创建一个包含X,Y,Z作为值的字段。我尝试了以下方法,但是创建CASE语句的方式只强制第一个参数为true。我理解这是因为值为'03'的观察可以在X或Z中。同样,观察可以在Y或Z中。
create view J as
(select *,
(CASE
WHEN X = TRUE then 1
WHEN Y = TRUE then 2
WHEN Z = TRUE then 3
ELSE NULL END)
as grouped_field
from select
(CASE WHEN A in ('01', ‘03') then TRUE ELSE FALSE END) as X,
(CASE WHEN A = ‘2' then TRUE ELSE FALSE END) as Y,
(CASE WHEN A in ('02', '03') then TRUE ELSE FALSE END) as Z
from
K));
答案 0 :(得分:0)
这样做你想要的吗?
concat((case when X = TRUE then '1' else '0' end),
(case when Y = TRUE then '1' else '0' end),
(case when Z = TRUE then '1' else '0' end)
)
串联运算符在数据库之间有所不同,但通常是||
或+
。
答案 1 :(得分:0)
也许是这样的
select *,
(CASE WHEN A in ('01', '03') then 'X' ELSE '' END) +
','+ (CASE WHEN A = '2' then 'Y' ELSE '' END) +
','+ (CASE WHEN A in ('02', '03') then 'Z' ELSE '' END) as Field
from
K;