可以做很多工會。
select MTD.date,MTD.col1,MTD.type,MTD.val as MTD,QTD.val as QTD,YTD.val as YTD
from(
select 'col3' type,date,col1,col3 as val from yourtable where col2 = 'MTD'
union all
select 'col4' type,date,col1,col4 from yourtable where col2 = 'MTD'
union all
select 'col5' type,date,col1,col5 from yourtable where col2 = 'MTD'
union all
select 'col6' type,date,col1,col6 from yourtable where col2 = 'MTD'
union all
select 'col7' type,date,col1,col7 from yourtable where col2 = 'MTD'
union all
select 'col8' type,date,col1,col8 from yourtable where col2 = 'MTD'
) MTD inner join (
select 'col3' type,date,col1,col3 as val from yourtable where col2 = 'QTD'
union all
select 'col4' type,date,col1,col4 from yourtable where col2 = 'QTD'
union all
select 'col5' type,date,col1,col5 from yourtable where col2 = 'QTD'
union all
select 'col6' type,date,col1,col6 from yourtable where col2 = 'QTD'
union all
select 'col7' type,date,col1,col7 from yourtable where col2 = 'QTD'
union all
select 'col8' type,date,col1,col8 from yourtable where col2 = 'QTD'
) QTD on MTD.date = QTD.date and MTD.col1 = QTD.col1 and MTD.type = QTD.type
inner join (
select 'col3' type,date,col1,col3 as val from yourtable where col2 = 'YTD'
union all
select 'col4' type,date,col1,col4 from yourtable where col2 = 'YTD'
union all
select 'col5' type,date,col1,col5 from yourtable where col2 = 'YTD'
union all
select 'col6' type,date,col1,col6 from yourtable where col2 = 'YTD'
union all
select 'col7' type,date,col1,col7 from yourtable where col2 = 'YTD'
union all
select 'col8' type,date,col1,col8 from yourtable where col2 = 'YTD'
) YTD on MTD.date = YTD.date and MTD.col1 = YTD.col1 and MTD.type = YTD.type
謝謝RubahMalam。這工作完美! – Carter