简体   繁体   中英

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.

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:

| 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:

 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

I´ve tried to add to the end of the clause:

order by table1.fecha1 and the result becomes even more messed up

This snippet works fine in 'Mariadb' but it doesn´t work form me in 'mysql'

Any suggestions?

If you fix your date format to be YYYY-MM, then this will be easy:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM