简体   繁体   中英

How do I do pivoting in this query in SQL?

I have this table like this:

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

I want to display into:

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

BTW ... I am using SQL2K5. All I can think of is CURSOR. Any other solution in elegant way?

Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM