表:
symbol baojia1 baojia2 qixian
1123 12 13 1a
1123 22 23 2a
2123 24 86 1a
2123 24 46 2a
……………………………………
希望(高效)得到:
symbol baojia1_1a baojia2_1a baojia1_2a baojia2_2a
1123 12 13 22 23
2123 24 86 24 46
………………………………………………………………
------解决方案--------------------
select symbol,max(case when qixian= '1a ' then baojia1 end) as baojia1_1a ,
max(case when qixian= '1a ' then baojia2 end) as baojia2_1a ,
max(case when qixian= '2a ' then baojia1 end) as baojia1_2a ,
max(case when qixian= '2a ' then baojia2 end) as baojia2_2a ,
[date]
from #tab
group by symbol,[date] order by symbol,[date]