2017-10-13 6 views
-1

列を行に変換したいのですが、これを実現する方法はありますか?ここでnはそうで..... enter image description here列を列に変換するUSING 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がクエリを追加し、列が生成されるのを見ると、クエリ全体を書き直す方法を考えることができます。しかし、私のコメントからリクエストまでわかるように、問合せには欠陥があると思われるので、最初に修正する必要があります。 –

関連する問題