由于您使用的是 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
或者您可以将 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