Concat 案例陈述

Concat case statement

我是编码的新手,但我正在尝试弄清楚如何(或者如果我可以)连接下面的代码,理想情况下排除任何用逗号分隔的空 returns。

所以一个例子是:-

德国、拉脱维亚、挪威

英国、法国、拉脱维亚

如有任何帮助和解释,我们将不胜感激。

Select 
*,

CASE 
WHEN PT_ID1 != 0 THEN 'Germany' 
WHEN PT_ID2 != 0 THEN 'Great Britain'
WHEN PT_ID3 != 0 THEN 'France'
WHEN PT_ID4 != 0 THEN 'Latvia'
WHEN PT_ID5 != 0 THEN 'Norway'
END AS Country

FROM      CTE2 C1
FULL JOIN CTE  C ON  C.PT_ID1 = C1.PT_ID2
FULL JOIN CTE3 D ON  D.PT_ID3 = C1.PT_ID2
FULL JOIN CTE4 E ON  E.PT_ID4 = C1.PT_ID2
FULL JOIN CTE5 F ON  F.PT_ID5 = C1.PT_ID2

order by MRN asc

每个国家/地区使用不同的CASE 语句

Select 
*,

 STUFF(CASE WHEN PT_ID1 != 0 THEN ', Germany' ELSE '' END
+ CASE WHEN PT_ID2 != 0 THEN ', Great Britain' ELSE '' END
+ CASE WHEN PT_ID3 != 0 THEN ', France' ELSE '' END
+ CASE WHEN PT_ID4 != 0 THEN ', Latvia' ELSE '' END
+ CASE WHEN PT_ID5 != 0 THEN ', Norway' ELSE '' END, 1, 2, '') AS Country

FROM      CTE2 C1
FULL JOIN CTE  C ON  C.PT_ID1 = C1.PT_ID2
FULL JOIN CTE3 D ON  D.PT_ID3 = C1.PT_ID2
FULL JOIN CTE4 E ON  E.PT_ID4 = C1.PT_ID2
FULL JOIN CTE5 F ON  F.PT_ID5 = C1.PT_ID2