当不同列中有标签时,如何在sql server中设置列以显示严格标签或null
我在sql server中有一个表,如下所示:当不同列中有标签时,如何在sql server中设置列以显示严格标签或null,sql,sql-server,sql-server-2017,Sql,Sql Server,Sql Server 2017,我在sql server中有一个表,如下所示: Name Col1 Col2 Col3 Col4 A din gla gif cla B gla cla gif din C din gif cla null --gla is missing D null cla gla null --din and gif is missing Name Col1 Col2 Col3 Col4 A
Name Col1 Col2 Col3 Col4
A din gla gif cla
B gla cla gif din
C din gif cla null --gla is missing
D null cla gla null --din and gif is missing
Name Col1 Col2 Col3 Col4
A din gla gif cla
B din gla gif cla
C din null gif cla
D null gla null cla
仅当名称的任何列中是否有标签时,这一点才很重要。我需要的是以每列表示定义值的方式返回结果集,如下所示:
Name Col1 Col2 Col3 Col4
A din gla gif cla
B gla cla gif din
C din gif cla null --gla is missing
D null cla gla null --din and gif is missing
Name Col1 Col2 Col3 Col4
A din gla gif cla
B din gla gif cla
C din null gif cla
D null gla null cla
我使用如下查询来管理它:
select name,
col1 = case when col1='din' or col2='din' or col3='din' or col4='din' then 'din' else null end,
...
from table
然而,在原版中有很多专栏,不仅仅是4个,而且这种方式对我来说似乎不是最好的。我想知道你能否推荐更好的方法。任何建议都将不胜感激。您的方法很好。另一种方法将使用: