Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当不同列中有标签时,如何在sql server中设置列以显示严格标签或null_Sql_Sql Server_Sql Server 2017 - Fatal编程技术网

当不同列中有标签时,如何在sql server中设置列以显示严格标签或null

当不同列中有标签时,如何在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

我在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     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个,而且这种方式对我来说似乎不是最好的。我想知道你能否推荐更好的方法。任何建议都将不胜感激。

您的方法很好。另一种方法将使用: