select t.tag, t.allyears, n13.nsites, n14.nsites, n15.nsites, n16.nsites, n17.nsites, n18.nsites, n19.nsites
from
(
select t.tag, count(distinct t.site) as allyears
from cpu.site_pkg_tag_count t
group by tag) as t

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2013'
group by tag) as n13
on t.tag=n13.tag

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2014'
group by tag) as n14
on t.tag=n14.tag

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2015'
group by tag) as n15
on t.tag=n15.tag

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2016'
group by tag) as n16
on t.tag=n16.tag

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2017'
group by tag) as n17
on t.tag=n17.tag

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2018'
group by tag) as n18
on t.tag=n18.tag

left outer join 
(select t.tag, count(distinct t.site) as nsites
from cpu.site_pkg_tag_count t 
join cpu.pkg_date_created d on t.pkg_id=d.pkg_id
where date_part('year',d.date_created) = '2019'
group by tag) as n19
on t.tag=n19.tag

