Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
Tsql 如何将文本列分组为一列_Tsql_Sql Server 2008 - Fatal编程技术网

Tsql 如何将文本列分组为一列

Tsql 如何将文本列分组为一列,tsql,sql-server-2008,Tsql,Sql Server 2008,我需要像这样的东西: SELECT dt, (???) AS C FROM (SELECT dt,c1,c2,c3,c4 ... dt-datetime,顺便提一下,我有一些奇怪的结构,例如: 24.02.2011 null null null "text" 25.02.2011 null "te" null "text" 26.02.2011 null "te" null null 它一定是 24.02.2011 "text" 25.02.2011 "te" 25.02.2011 "te

我需要像这样的东西:

SELECT dt, (???) AS C FROM (SELECT dt,c1,c2,c3,c4  ...
dt-datetime,顺便提一下,我有一些奇怪的结构,例如:

24.02.2011 null null null "text"
25.02.2011 null "te" null "text"
26.02.2011 null "te" null null
它一定是

24.02.2011 "text"
25.02.2011 "te"
25.02.2011 "text"
26.02.2011 "te"
或许

24.02.2011 "c4" "text"
25.02.2011 "c2" "te"
25.02.2011 "c4 "text"
26.02.2011 "c2" "te"
如果我从选择dt、c1、c2、c3、c4中选择dt、C 1+C 2+C 3+C 4作为C

会有

25.02.2011  "tetext"
这个输出对我来说是不正确的

一定是

25.02.2011 "te"
25.02.2011 "text"
所以我需要知道如何将行+列交换到行

多谢各位

MS SQL Server 2008


我目前的想法是用我需要的结构创建临时表,甚至可能是静态表,然后插入数据,然后从中进行选择。

我不知道在MS SQL server中是如何实现的
但在mysql中,您可以看到这些函数 ,

最简单的可能是使用UNION ALL:

SELECT dt,'c1' as Col,c1 from table1 where c1 is not null
UNION ALL
SELECT dt,'c2' as Col,c2 from table1 where c2 is not null
UNION ALL
SELECT dt,'c3' as Col,c3 from table1 where c3 is not null
UNION ALL
SELECT dt,'c4' as Col,c4 from table1 where c4 is not null
在示例数据的中间一行(这意味着要生成两个输出行),这意味着使用COALESCE/ISNULL的解决方案可能会失败