Transpose a query output
问题 I have a normal select query which results following output. select cid,x1,x2,x3,x4,fy from temp_table; cid x1 x2 x3 x4 fy ---------------------------- 6657 100 0 0 200 2014 6658 300 0 0 400 2015 6659 500 0 0 600 2016 I want it to rewrite it print following output. 2014 2015 2016 ------------------------- x1 100 300 500 x2 0 0 0 x3 0 0 0 x4 200 400 600 How can this be achieved? 回答1: Here is a way to do this with just subqueries and aggregation: select name, sum(case when fy = 2014 then x end)