在SQL Server中将数据从一列拆分为多行
我是SQL的初学者,不幸的是我不能解决问题。我在系统表中有一个列名列表,我想根据列名的序列将其插入到表的单独行中 原始表格: 列名 001000 001100 001200 002000 002100 002200 002300在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
条件聚合应该满足您的要求:
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)
非常感谢你!这起作用了