2017-10-13 41 views
-1

我想將列轉換爲行,有沒有什麼辦法可以實現這一點。這裏是我的截圖enter image description here將列轉置爲行使用MYSQL

我的輸出這樣的

+----------------------+ 
| Month  | Data | 
+----------------------+ 
| data_july | 130.11 | 
| data_august | 257.28 | 
+----------------------+ 

.....ñ等。

我面對我的查詢來實現這是一個問題,這是我的查詢:

select 
    a.id as milestone_id, 
    a.name_of_work, 
    case when(a.sch_jul>100) then 100 when (a.sch_jul<0) then 0 else a.sch_jul end as data_july, 
    case when(a.sch_aug>100) then 100 when (a.sch_aug<0) then 0 else a.sch_aug end as data_august, 
    case when(a.sch_sep>100) then 100 when (a.sch_sep<0) then 0 else a.sch_sep end as data_september, 
    case when(a.sch_oct>100) then 100 when (a.sch_oct<0) then 0 else a.sch_oct end as data_october, 
    case when(a.sch_nov>100) then 100 when (a.sch_nov<0) then 0 else a.sch_nov end as data_november, 
    case when(a.sch_dec>100) then 100 when (a.sch_dec<0) then 0 else a.sch_dec end as data_december, 
    case when(a.sch_jan>100) then 100 when (a.sch_jan<0) then 0 else a.sch_jan end as data_january, 
    case when(a.sch_feb>100) then 100 when (a.sch_feb<0) then 0 else a.sch_feb end as data_february, 
    case when(a.sch_mar>100) then 100 when (a.sch_mar<0) then 0 else a.sch_mar end as data_march, 
    case when(a.sch_apr>100) then 100 when (a.sch_apr<0) then 0 else a.sch_apr end as data_april 
from 
(
    SELECT 
    distinct w.id, 
    w.name_of_work, 
    s.milestone_id, 
    round(((DATEDIFF(date_format('2017-07-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_jul, 
    round(((DATEDIFF(date_format('2017-08-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_aug, 
    round(((DATEDIFF(date_format('2017-09-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_sep, 
    round(((DATEDIFF(date_format('2017-10-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_oct, 
    round(((DATEDIFF(date_format('2017-11-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_nov, 
    round(((DATEDIFF(date_format('2017-12-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_dec, 
    round(((DATEDIFF(date_format('2018-01-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_jan, 
    round(((DATEDIFF(date_format('2018-02-28', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_feb, 
    round(((DATEDIFF(date_format('2018-03-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_mar, 
    round(((DATEDIFF(date_format('2018-04-30', '%Y-%m-%d'), s.start_date)/DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_apr 
    FROM tbl_scope_of_work w 
    left join tbl_schedule_pre_site_survey s on s.milestone_id = w.id 
    where w.property = 2 
    group by w.id 
    order by w.id asc 
) a 
+0

可能的副本吃了https://stackoverflow.com/questions/7674786/mysql-pivot-table和https://stackoverflow.com/questions/7674786/mysql-pivot-table –

+0

沒有它不同的東西 – user3734149

+0

請參閱https:// stackoverflow.com/questions/16913717/mysql-select-column-name-and-value-as-a-field – KMS

回答

0

您可以通過列,然後聯合查詢列結果:

select name, value 
from 
(
    select 'data_july' as name, data_july as value, 1 as sortkey from mytable 
    union all 
    select 'data_august' as name, data_august as value, 2 as sortkey from mytable 
    union all 
    ... 
) data 
order by sortkey; 

如果你不需要保持排序順序,那麼它只是:

select 'data_july' as name, data_july as value from mytable 
union all 
select 'data_august' as name, data_august as value from mytable 
union all 
... 
+0

其相當不錯,但靜態..你可以改變動態檢索數據 –

+0

@Ankit Agrawal:當我寫我的答案,我期望讀取數據從一張桌子。在這種情況下,我們不能將查詢編寫成靜態的,因爲它是我們讀取的某些列。由於OP現在添加了一個查詢,並且我們看到這些列已經生成,所以可以想辦法重新編寫整個查詢。但正如您從我的意見中可以看到的那樣,查詢看起來似乎有缺陷,因此必須先進行修正。 –