select sum(added_links), sum(deleted_links), year, month, week
from (
select
YEAR(url.created_at) as year,
MONTH(url.created_at) as month,
WEEK(url.created_at) as week,
1 as added_links,
0 as deleted_links,
from url
where url.created_at IS NOT NULL
UNION ALL
select
YEAR(url.deleted_at) as year,
MONTH(url.deleted_at) as month,
WEEK(url.deleted_at) as week,
0 as added_links,
1 as deleted_links,
from url
where url.deleted_at IS NOT NULL
)
group by year, month, week