Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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 将行值拆分为列?_Sql_Sql Server_Sqlite_Tsql_Tsqlt - Fatal编程技术网

Sql 将行值拆分为列?

Sql 将行值拆分为列?,sql,sql-server,sqlite,tsql,tsqlt,Sql,Sql Server,Sqlite,Tsql,Tsqlt,输入表 Id 1,2,3 6,8,1,2,5 4,9,2,1 6,7,8 我需要这样的输出 Id1 id2 id3 id4 id5 1 2 3 Null Null 6 8 1 2 5 4 9 2 1 Null 6 7 8 Null Null 如果无法按照Gordon的建议重新导入,请尝试以下操作: Select A.ID ,B.* From YourTable A Cross Apply (

输入表

Id 
1,2,3
6,8,1,2,5
4,9,2,1
6,7,8
我需要这样的输出

Id1 id2 id3 id4   id5
1    2   3  Null  Null
6    8   1   2    5
4    9   2   1    Null
6    7   8  Null  Null

如果无法按照Gordon的建议重新导入,请尝试以下操作:

Select A.ID
      ,B.*
 From  YourTable A
 Cross Apply (
                Select Pos1  = xDim.value('/x[1]','int')
                      ,Pos2  = xDim.value('/x[2]','int')
                      ,Pos3  = xDim.value('/x[3]','int')
                      ,Pos4  = xDim.value('/x[4]','int')
                      ,Pos5  = xDim.value('/x[5]','int')
                From  (Select Cast('<x>' + replace([Id],',','</x><x>')+'</x>' as xml) as xDim) as A 
             ) B
返回


为什么标签中有sqlite和sql server?删除不相关的标记。不要像这样存储分隔数据。使用它很糟糕,因为它违反了1NF。导出数据并重新导入数据可能存在重复。到目前为止,您尝试了什么,得到了什么结果?您使用的数据库管理系统是SQL Server还是SQLite?