欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sql server pivot

程序员文章站 2023-11-15 13:04:16
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[YearSalary]( [year] [int] NULL, [salary] [money] NULL ) ON [PRIMARY] GO select * ... ......
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

 

 

sql server pivot