在 sql 中旋转 table 数据
Pivot table data in sql
我有这样的数据
id Amount coluName
13 25000 abccol
13 2300 defcol
但我想要这样的数据
id abccol defcol
13 25000 2300
查询
select e.id,e.Amount,i.ColuName
from FixAm e
inner join InCo i on i.CCode=e.CCode
where cid=49
如何通过 pivot 做到这一点?其中不仅有两个名称(abccol,defcol)还有很多其他名称,但我在这里只使用了这两个
这可能对你有用。
用户 @jarlh 在评论中说您需要指定您正在使用的 SQL 服务器的版本是正确的。
declare @cols as nvarchar(max)
, @query as nvarchar(max)
select @cols = stuff((select distinct ',' + quotename(coluName) from #t for xml path(''), type ).value('.', 'nvarchar(max)') ,1,1,'')
set @query = 'select id, ' + @cols + '
from #t
pivot ( max(amount) for coluName in (' + @cols + ') ) p '
select @query
execute sp_executesql @query;
因此,我们的想法是在您的 table 上使用一个简单的 PIVOT
,旋转列 coluName
。您每次都需要动态获取的值,因为您没有 coluName
值的静态列表。
您可以查看关于动态旋转的很好解释 here。
您可以检查此查询的工作版本 here。
EDIT:
要在评论中回答您的问题,可以像这样使用相同的 table #t(指定您正确的数据类型,我输入 int
、numeric
和 [=以 17=] 为例):
if object_id('tempdb..#t') is not null drop table #t
create table #t (id int, amount numeric, coluName varchar(10))
insert into #t
select e.id, e.Amount, i.ColuName
from FixAm e inner join InCo i on i.CCode = e.CCode
where i.cid = 49
然后简单地使用上面的动态枢轴。
IF Object_id('tempdb..#tempTab') IS NOT NULL
Drop table #tempTab
;WITH Cte(id,Amount,coluName )
AS
(
Select 13,25000,'abccol' UNION ALL
Select 13,2300, 'defcol'
)
SELECT * INTO #tempTab FROM cte
Declare @Columns nvarchar(max), @Sql nvarchar(max)
SELECT @Columns=STUFF((SELECT DISTINCT ', '+ QuoteName(coluName) FROM #tempTab
FOR XML PATH ('')),1,1,'') from #tempTab
--SELECT @Columns
SET @Sql='SELECT [id],'+@Columns+'From
(
SELECT id,Amount,coluName
from #tempTab
)
AS Src
Pivot
(
MAX(Amount) For coluName IN ('+@Columns +')
)Pvt '
Print @Sql
EXecute (@Sql)
输出
id abccol defcol
-------------------
13 25000 2300
我有这样的数据
id Amount coluName
13 25000 abccol
13 2300 defcol
但我想要这样的数据
id abccol defcol
13 25000 2300
查询
select e.id,e.Amount,i.ColuName
from FixAm e
inner join InCo i on i.CCode=e.CCode
where cid=49
如何通过 pivot 做到这一点?其中不仅有两个名称(abccol,defcol)还有很多其他名称,但我在这里只使用了这两个
这可能对你有用。 用户 @jarlh 在评论中说您需要指定您正在使用的 SQL 服务器的版本是正确的。
declare @cols as nvarchar(max)
, @query as nvarchar(max)
select @cols = stuff((select distinct ',' + quotename(coluName) from #t for xml path(''), type ).value('.', 'nvarchar(max)') ,1,1,'')
set @query = 'select id, ' + @cols + '
from #t
pivot ( max(amount) for coluName in (' + @cols + ') ) p '
select @query
execute sp_executesql @query;
因此,我们的想法是在您的 table 上使用一个简单的 PIVOT
,旋转列 coluName
。您每次都需要动态获取的值,因为您没有 coluName
值的静态列表。
您可以查看关于动态旋转的很好解释 here。
您可以检查此查询的工作版本 here。
EDIT:
要在评论中回答您的问题,可以像这样使用相同的 table #t(指定您正确的数据类型,我输入 int
、numeric
和 [=以 17=] 为例):
if object_id('tempdb..#t') is not null drop table #t
create table #t (id int, amount numeric, coluName varchar(10))
insert into #t
select e.id, e.Amount, i.ColuName
from FixAm e inner join InCo i on i.CCode = e.CCode
where i.cid = 49
然后简单地使用上面的动态枢轴。
IF Object_id('tempdb..#tempTab') IS NOT NULL
Drop table #tempTab
;WITH Cte(id,Amount,coluName )
AS
(
Select 13,25000,'abccol' UNION ALL
Select 13,2300, 'defcol'
)
SELECT * INTO #tempTab FROM cte
Declare @Columns nvarchar(max), @Sql nvarchar(max)
SELECT @Columns=STUFF((SELECT DISTINCT ', '+ QuoteName(coluName) FROM #tempTab
FOR XML PATH ('')),1,1,'') from #tempTab
--SELECT @Columns
SET @Sql='SELECT [id],'+@Columns+'From
(
SELECT id,Amount,coluName
from #tempTab
)
AS Src
Pivot
(
MAX(Amount) For coluName IN ('+@Columns +')
)Pvt '
Print @Sql
EXecute (@Sql)
输出
id abccol defcol
-------------------
13 25000 2300