Skip to content

Instantly share code, notes, and snippets.

@joecannatti
Created June 28, 2013 20:23
Show Gist options
  • Save joecannatti/5887806 to your computer and use it in GitHub Desktop.
Save joecannatti/5887806 to your computer and use it in GitHub Desktop.
select COALESCE(a.num + b.num + c.num + d.num, 0) as total, channels.id as master_channel_id
from channels
left join
(select name, channels.id as channel_id, count(*) as num from channels
join `simple_discussions` on simple_discussions.channel_id = channels.id
where simple_discussions.created_at >= '2013-01-01' and simple_discussions.created_at <= '2013-12-30'
group by channels.id) as a on a.channel_id = channels.id
left join
(select name, channels.id as channel_id, count(*) as num from channels
join `video_attachments` on video_attachments.channel_id = channels.id
where video_attachments.attachment_associable_type is null
and video_attachments.created_at >= '2013-01-01' and video_attachments.created_at <= '2013-12-30'
group by channels.id) as b on b.channel_id = channels.id
left join
(select name, channels.id as channel_id, count(*) as num from channels
inner join link_resources on link_resources.channel_id = channels.id
where link_resources.created_at >= '2013-01-01' and link_resources.created_at <= '2013-12-30'
group by channels.id) as c on c.channel_id = channels.id
left join
(select name, channels.id as channel_id, count(*) as num from channels
join `attachments` on attachments.channel_id = channels.id
where attachments.attachment_associable_type is null
and attachments.created_at >= '2013-01-01' and attachments.`created_at` <= '2013-12-30'
group by channels.id) as d on d.channel_id = channels.id
group by master_channel_id
order by total desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment