答案 0 :(得分:1)
您可以通过以下查询
来达到预期的结果SELECT *
FROM cardata
PIVOT(AVG(Price) FOR YEAR IN ([2010], [2011], [2012])) AS PivotTable;
所以在这里你必须使用聚合函数来使用Pivot。
答案 1 :(得分:0)
您可以使用PIVOT / UNPIVOT。解决方案是here
答案 2 :(得分:0)
您需要使用PIVOT
:SQL Fiddle
数据强>
create table cars(brand varchar(100),myear int,price bigint)
insert into cars
select 'audi',2010,5000000 union all
select 'audi',2011,5340000 union all
select 'audi',2012,5890000 union all
select 'bmw',2010,6000000 union all
select 'bmw',2011,6780000 union all
select 'bmw',2012,4450000 union all
select 'maruti',2010,4540000 union all
select 'maruti',2011,7800000 union all
select 'maruti',2012,9000000
<强>查询强>
SELECT * FROM
cars
PIVOT
(
MAX(PRICE) FOR MYEAR IN ([2010],[2011],[2012])
)P