如何在SQL中使用此查询进行透视?

时间:2010-05-26 06:47:12

标签: sql sql-server

我有这样的表:

Name; Amount1, Amount, Rate1, Rate2
Test; 1000; 2000; 1.0; 2.0

我想显示:

Parameter; Amount1; Rate1; Total
'Parameter 1'; 1000; 1.0; 1000 
'Parameter 2'; 2000; 2.0; 4000

BTW ......我正在使用SQL2K5。我能想到的只有CURSOR。优雅的任何其他解决方案?

由于

1 个答案:

答案 0 :(得分:1)

select *, Amount*rate Total from( 
select n parameter, case when n='parameter1' then amount1 else amount2 end Amount,
       case when n='parameter1' then rate1 else rate2 end rate
from tests t cross join (select 'parameter1' as n union all select 'parameter2') x
) y