1. 程式人生 > >sql 列轉行(將一條記錄(橫向)轉為一列顯示)

sql 列轉行(將一條記錄(橫向)轉為一列顯示)

sql 列轉行

將一條查詢結果,本開始橫向的一條記錄,想讓他顯示為一列,此處查詢了130項因子資料,sql語句看著嚇人,只看一兩行就ok

 轉換結果

 sql語句:


select b.c1 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c2 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c3 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c4 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c5 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c6 as climate_value  from climate_index b where tm='195101' UNION ALL
......
......
select b.c125 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c126 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c127 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c128 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c129 as climate_value  from climate_index b where tm='195101' UNION ALL
select b.c130 as climate_value  from climate_index b where tm='195101' ;