sql server pivot

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[YearSalary](
    [year] [int] NULL,
    [salary] [money] NULL
) ON [PRIMARY]
GO


select * from yearsalary;

select 'year' as WorkYear,[2011],[2012],[2013],[2014],[2015],[2016],[2017],[2018],[2019]
from 
(
select year,salary  from yearsalary) as SourceTable
pivot
(
avg(salary)
for year in([2011],[2012],[2013],[2014],[2015],[2016],[2017],[2018],[2019])
) as pivottable

 

 

 

posted @ 2019-09-08 15:16 Fred1987 阅读(...) 评论(...) 编辑 收藏