Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created August 11, 2009 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamSaffron/165561 to your computer and use it in GitHub Desktop.
Save SamSaffron/165561 to your computer and use it in GitHub Desktop.
create view UserTotals
as
select UserId, Reputation, Questions, [Answers], Upvotes, Downvotes from
(
select
UserId = u.Id,
u.Reputation as Reputation,
sum(case when p.ParentId is null then 1 else 0 end) as Questions,
sum(case when p.ParentId is not null then 1 else 0 end) as [Answers]
from Users u
join Posts p on p.OwnerUserId = u.Id
where p.CommunityOwnedDate is null and p.ClosedDate is null
group by u.Id, u.Reputation
) as t
join
(
select
p.OwnerUserId,
Upvotes = SUM(case when VoteTypeId = 2 then 1 else 0 end),
Downvotes = SUM(case when VoteTypeId = 3 then 1 else 0 end)
from Votes v
join Posts p on p.Id = v.PostId
where VoteTypeId in (2,3) and p.CommunityOwnedDate is null and p.ClosedDate is null
group by OwnerUserId
) as v on v.OwnerUserId = t.UserId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment