Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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中将数据从一列拆分为多行_Sql_Sql Server_Pivot - Fatal编程技术网

在SQL Server中将数据从一列拆分为多行

在SQL Server中将数据从一列拆分为多行,sql,sql-server,pivot,Sql,Sql Server,Pivot,我是SQL的初学者,不幸的是我不能解决问题。我在系统表中有一个列名列表,我想根据列名的序列将其插入到表的单独行中 原始表格: 列名 001000 001100 001200 002000 002100 002200 002300 条件聚合应该满足您的要求: select max(case when seqnum = 1 then column end), max(case when seqnum = 2 then column end), max(case when

我是SQL的初学者,不幸的是我不能解决问题。我在系统表中有一个列名列表,我想根据列名的序列将其插入到表的单独行中

原始表格:

列名 001000 001100 001200 002000 002100 002200 002300
条件聚合应该满足您的要求:

select max(case when seqnum = 1 then column end),
       max(case when seqnum = 2 then column end),
       max(case when seqnum = 3 then column end),
       max(case when seqnum = 4 then column end)
from (select t.*,
             row_number() over (partition by left(column, 3) order by column) as seqnum
      from t
     ) t
group by left(column, 3)

非常感谢你!这起作用了