多个CASE WHEN语句仅输出单行

时间:2018-09-24 18:49:40

标签: sql oracle case

我正在尝试创建一个输出20行的Oracle查询。每行(name_x)只能在col_a1和col_a2之外具有一个值。每次我运行下面的查询时,我只会得到1行。和'name_1'到'name_20'的结果为零,但Name_T已成功发布。

SELECT col_a1, col_a2, 
Coalesce (max(name_1),0) name_1,
Coalesce (max(name_2),0) name_2,
Name_t
...

FROM(
SELECT 
CASE WHEN b.col_b1 = 'Y' and a.col_a3 = 'X' THEN a.col_a4 ELSE NULL END name_1,
CASE WHEN b.col_b1 = 'Y' and a.col_a3 = 'X2' THEN a.col_a4 ELSE NULL END name_2,
…
CASE WHEN a.col_A3 = 'Z' then a.col_a4 else null name_T
FROM dbA.TbA a  
LEFT OUTER JOIN (SELECT * FROM dbB.TbB Where X) b
ON a.col_W = b.col_W
)
Group by col_a1, col_a2

好吧,在阅读评论之后,我回过头来修复了由于我的子查询无法正常工作而导致的一些错误。我能够第一次正确发布子查询!现在,我无法“通过col_a1,col_a2进行分组”并获得无效的标识符错误,该错误现在正在处理。

1 个答案:

答案 0 :(得分:0)

我认为,您忘记在子查询中添加col_a1, col_a2

SELECT col_a1, col_a2, 
Coalesce (max(name_1),0) name_1,
Coalesce (max(name_2),0) name_2,
Name_t
...

FROM(
SELECT 
col_a1, col_a2,
CASE WHEN b.col_b1 = 'Y' and a.col_a3 = 'X' THEN a.col_a4 ELSE NULL END name_1,
CASE WHEN b.col_b1 = 'Y' and a.col_a3 = 'X2' THEN a.col_a4 ELSE NULL END name_2,
…
CASE WHEN a.col_A3 = 'Z' then a.col_a4 else null name_T
FROM dbA.TbA a  
LEFT OUTER JOIN (SELECT * FROM dbB.TbB Where X) b
ON a.col_W = b.col_W
)
Group by col_a1, col_a2