我想将列转换为行,有没有办法实现这一点。这是我的截图enter image description here
我的输出就像这样
+----------------------+
| 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 :(得分: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
...