由於您使用的是SQL Server 2008+,因此您可以使用CROSS APPLY將列中的數據反轉爲行。
您可以使用VALUES子句與CROSS APPLY:
select distinct t.country,
c.year,
c.totalvolumn
from yourtable t
cross apply
(
values
('2007', 2007),
('2008', 2008),
('2009', 2009)
) c(year, TotalVolumn)
order by t.country, c.year;
見SQL Fiddle with Demo
或者你可以使用UNION ALL與CROSS APPLY:
select distinct t.country,
c.year,
c.totalvolumn
from yourtable t
cross apply
(
select '2007', 2007 union all
select '2008', 2008 union all
select '2009', 2009
) c(year, TotalVolumn)
order by t.country, c.year;
見SQL Fiddle with Demo。
這也可以使用UNION查詢寫着:
select country, '2007' year, 2007 totalVolumn
from yourtable
union
select country, '2008' year, 2008 totalVolumn
from yourtable
union
select country, '2009' year, 2009 totalVolumn
from yourtable
order by country, year;
見SQL Fiddle with Demo
可以發佈更多詳情請,但在()在讀入數據透視先驗貨() – RoughPlace