如何创建具有多个列的 select 查询以查询具有多个条件的单个列
How to create a select query with multiple columns querying a single column with multiple conditions
我想从我的 db2 数据库中获取结果列表,查询在同一列上具有多个条件的 table。这是一个示例场景:
表 1:
col1, col2, col3, col4, col5
sample, user, adam, ggg, hdh
sample, source, online, urt, loe
random, user, henr, uuu, kkk
crew, user, mike, nhg, kik
crew, dummy, nothing, irr, wer
ment, maker, hts, ret, opp
我正在尝试编写一个 select 查询,它将按以下方式列出详细信息:
col1, col2, col3, col4
sample, adam, online, ggg
random, henr, , uuu
crew, mike, nothing, irr
ment, , , ret
因此,当 col2 的值为 user 时,我希望相应的 col3 值作为我的 select 语句的列之一。而且,当 col2 在 source 或 dummy 中具有值时,我希望相应的 col3 值作为我的 select 语句的另一列。通过这种方式,我最终可以得到一个包含 4 列的列表,它为我提供了我需要的详细信息的独特组合。
我有一份此类查询的草稿 sql。我不确定如何重构它以获得查询单个列的两个输出列。
select col1 , col3 as col2, col3 as col3, col4 from Table1
where col2='user' or col2 in ('source','dummy')
如果我这样做,我将以这种方式获得输出
col1, col2, col3, col4
sample, adam, adam, ggg
sample, online, online, ggg
random, henr, henr, uuu
crew, mike, mike, irr
crew, nothing, nothing, irr
我想你想要这个:
select col1,
max(case when col2 = 'user' then col3 end) as col2,
max(case when col2 in ('source', 'dummy') then col3 end) as col3,
max(col4) as col4
from t
group by col1;
可能您需要写 select col1 , col3 as col2, col2 as col3, col4 from Table1...
然后添加相应的条件,就像您的情况一样。输出中的第二列和第三列相同,因为您在
中选择了 col3
我不确定为什么您在输入中提到了 col5,因为您的输出中没有提到它?它只会把水搅浑 ;-)
无论如何,根据 input/output 示例,SQL 这是您需要获得的结果:
WITH
USER AS
( SELECT COL1
, COL3 AS COLA
, COL4 AS COLB
FROM TABLE1
WHERE COL2 = 'USER'
)
, SOURCE AS
( SELECT COL1
, COL2 AS COLC
, COL3 AS COLD
, COL4 AS COLE
FROM TABLE1
WHERE COL2 IN ( 'SOURCE'
, 'DUMMY'
)
)
SELECT U.COL1
, U.COLA AS COL2
, COALESCE(S.COLD,'') AS COL3
, COALESCE
( CASE
WHEN S.COLC = 'DUMMY'
THEN S.COLE
ELSE U.COLB
END
, U.COLB
) AS COL4
FROM USER U
LEFT OUTER JOIN SOURCE S
ON S.COL1 = U.COL1
WITH UR FOR FETCH ONLY;
我想从我的 db2 数据库中获取结果列表,查询在同一列上具有多个条件的 table。这是一个示例场景:
表 1:
col1, col2, col3, col4, col5
sample, user, adam, ggg, hdh
sample, source, online, urt, loe
random, user, henr, uuu, kkk
crew, user, mike, nhg, kik
crew, dummy, nothing, irr, wer
ment, maker, hts, ret, opp
我正在尝试编写一个 select 查询,它将按以下方式列出详细信息:
col1, col2, col3, col4
sample, adam, online, ggg
random, henr, , uuu
crew, mike, nothing, irr
ment, , , ret
因此,当 col2 的值为 user 时,我希望相应的 col3 值作为我的 select 语句的列之一。而且,当 col2 在 source 或 dummy 中具有值时,我希望相应的 col3 值作为我的 select 语句的另一列。通过这种方式,我最终可以得到一个包含 4 列的列表,它为我提供了我需要的详细信息的独特组合。
我有一份此类查询的草稿 sql。我不确定如何重构它以获得查询单个列的两个输出列。
select col1 , col3 as col2, col3 as col3, col4 from Table1
where col2='user' or col2 in ('source','dummy')
如果我这样做,我将以这种方式获得输出
col1, col2, col3, col4
sample, adam, adam, ggg
sample, online, online, ggg
random, henr, henr, uuu
crew, mike, mike, irr
crew, nothing, nothing, irr
我想你想要这个:
select col1,
max(case when col2 = 'user' then col3 end) as col2,
max(case when col2 in ('source', 'dummy') then col3 end) as col3,
max(col4) as col4
from t
group by col1;
可能您需要写 select col1 , col3 as col2, col2 as col3, col4 from Table1...
然后添加相应的条件,就像您的情况一样。输出中的第二列和第三列相同,因为您在
col3
我不确定为什么您在输入中提到了 col5,因为您的输出中没有提到它?它只会把水搅浑 ;-)
无论如何,根据 input/output 示例,SQL 这是您需要获得的结果:
WITH
USER AS
( SELECT COL1
, COL3 AS COLA
, COL4 AS COLB
FROM TABLE1
WHERE COL2 = 'USER'
)
, SOURCE AS
( SELECT COL1
, COL2 AS COLC
, COL3 AS COLD
, COL4 AS COLE
FROM TABLE1
WHERE COL2 IN ( 'SOURCE'
, 'DUMMY'
)
)
SELECT U.COL1
, U.COLA AS COL2
, COALESCE(S.COLD,'') AS COL3
, COALESCE
( CASE
WHEN S.COLC = 'DUMMY'
THEN S.COLE
ELSE U.COLB
END
, U.COLB
) AS COL4
FROM USER U
LEFT OUTER JOIN SOURCE S
ON S.COL1 = U.COL1
WITH UR FOR FETCH ONLY;