[英]mysql join left order by date
I just have a table 'chart' which needs a query to order the output for some chart plot purpose.我只有一个表“图表”,需要查询才能为某些图表 plot 目的订购 output。
select fecha1,COALESCE(data,0) as qty
from
(
select DATE_FORMAT(date,'%m/%Y') as fecha1
from charts
where project_code='CEU92'
group by year(date),month(date)
)
as table1
left join
(
select DATE_FORMAT(date,'%m/%Y') as fecha2,zone, sum(qty) as data
from charts
where project_code='CEU92' and zone='Muro transformadores'
group by year(date),month(date)
)
as table2 on table2.fecha2 = table1.fecha1
Table 1 subquery is:表 1 子查询为:
| fecha1 |
| 08/2016 |
| 09/2016 |
| 10/2016 |
| 11/2016 |
| 12/2016 |
| 01/2017 |
| 02/2017 |
| 03/2017 |
| 08/2017 |
...........
| 04/2019 |
| 05/2019 |
| 06/2019 |
| 07/2019 |
| 08/2019 |
| 09/2019 |
| 10/2019 |
| 11/2019 |
Table 2 subquery looks like this:表 2 子查询如下所示:
fecha2 zone data
| 04/2019 | Muro Transformadores | 39 |
| 05/2019 | Muro Transformadores | 44 |
| 06/2019 | Muro Transformadores | 94 |
| 07/2019 | Muro Transformadores | 20 |
| 08/2019 | Muro Transformadores | 168.5|
| 09/2019 | Muro Transformadores | 935 |
| 10/2019 | Muro Transformadores | 1762 |
| 11/2019 | Muro Transformadores | 157.5|
The result of the lef join query looks like this:左连接查询的结果如下所示:
fecha1 qty
| 04/2019 | 39
| 05/2019 | 44
| 06/2019 | 94
| 07/2019 | 20
| 08/2019 | 168.5
| 09/2019 | 935
| 10/2019 | 1762
| 11/2019 | 157.5
| 08/2016 | 0
| 09/2016 | 0
| 10/2016 | 0
| 11/2016 | 0
| 12/2016 | 0
| 01/2017 | 0
| 02/2017 | 0
| 03/2017 | 0
| 04/2017 | 0
What I need is the result table order by fecha1我需要的是 fecha1 的结果表顺序
I´ve tried to add to the end of the clause:我试图添加到条款的末尾:
order by table1.fecha1
and the result becomes even more messed up order by table1.fecha1
,结果变得更加混乱
This snippet works fine in 'Mariadb' but it doesn´t work form me in 'mysql'这个片段在'Mariadb'中工作正常,但在'mysql'中对我不起作用
Any suggestions?有什么建议么?
If you fix your date format to be YYYY-MM, then this will be easy:如果您将日期格式固定为 YYYY-MM,那么这将很容易:
select fecha1,COALESCE(data, 0) as qty
from (select DATE_FORMAT(date,'%Y-%m') as fecha1
from charts
where project_code = 'CEU92'
group by DATE_FORMAT(date,'%Y-%m')
) table1 left join
(select DATE_FORMAT(date, '%Y-%m') as fecha2, zone, sum(qty) as data
from charts
where project_code = 'CEU92' and zone = 'Muro transformadores'
group by DATE_FORMAT(date, '%Y-%m'), zone
) table2
on table2.fecha2 = table1.fecha1
order by table1.fecha1;
However, conditional aggregation is probably a simpler approach:但是,条件聚合可能是一种更简单的方法:
select DATE_FORMAT(date, '%m/%Y') as fecha2,
'Muro transformadores' as zone,
sum(case when zone = 'Muro transformadores' then qty end) as data
from charts
where project_code = 'CEU92'
group by DATE_FORMAT(date, '%m/%Y')
order by min(date);
I still prefer YYYY-MM, but this is an alternative.我还是更喜欢 YYYY-MM,但这是另一种选择。
Why two sub-queries?:为什么有两个子查询?:
SELECT
DATE_FORMAT(c1.fecha1,'%m/%Y'),
COALESCE(c2.data,0) as qty
FROM charts c1
LEFT JOIN charts c2 ON
c2.date = c1.date
AND c2.project_code=c1.project_code
AND c2.zone='Muro transformadores'
WHERE
project_code='CEU92'
GROUP BY
year(fetcha1),month(fetcha1)
ORDER BY 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.