如果您使用的是SQL Server 2008+,则可以使用CROSS APPLY将数据从列转换为行。
您可以使用CROSS APPLY和VALUES子句:
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。
这也可以使用 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
。