mysql - combining two aggregate queries into one view -


i'm not able wrap brain around one. have 2 queries use periodic reporting:

mysql> select year(a.creation) year, count(*) 'built year'     -> hosts     -> year(a.creation) > 2013     -> group year(a.creation); +------+---------------+ | year | built year | +------+---------------+ | 2014 |           372 | | 2015 |          1115 | | 2016 |           779 | | 2017 |           268 | +------+---------------+ 4 rows in set (0.00 sec)  mysql> select year(b.decom) year, count(*) 'decomed year'     -> hosts b     -> year(b.decom) > 2013     -> group year(b.decom); +------+-----------------+ | year | decomed year | +------+-----------------+ | 2015 |              68 | | 2016 |             816 | | 2017 |              27 | +------+-----------------+ 3 rows in set (0.00 sec) 

i'd able report both in single view.

i've tried few things closest i've come results in nice cartesian join. fortunately, db tiny:

mysql> select year(a.creation) year, count(a.hostname) 'built year', count(b.hostname) 'decomed year'     -> hosts     ->   left join hosts b on year(a.creation) = year(b.decom)     -> year(a.creation) > 2013 group year(a.creation), year(b.decom); +------+---------------+-----------------+ | year | built year | decomed year | +------+---------------+-----------------+ | 2014 |           372 |               0 | | 2015 |         75820 |           75820 | | 2016 |        635664 |          635664 | | 2017 |          7236 |            7236 | +------+---------------+-----------------+ 4 rows in set (3.59 sec) 

you need join 2 query table below

  selecgt a.year,a.built_by_year,b.decomed_by_year from(                   select year(a.creation) year, count(*) 'built_by_year'                   hosts                   year(a.creation) > 2013                   group year(a.creation)                  )             left join (                  select year(b.decom) year, count(*) 'decomed_by_year'                  hosts b                  year(b.decom) > 2013                  group year(b.decom)                ) b               on a.year=b.year 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -