Posts with most upvotes, downvotes, total votes, highest and lowest total score

1

Please login or register to vote for this query.

(click on this box to dismiss)

Drupal Answers

Q&A for Drupal developers and administrators

with down_votes as (
  select top 20 'down votes' as type, 
    count(*) as votes, postid as [Post Link]
  from votes
  where votetypeid = 3 -- downvotes!
  group by postid
  order by votes desc
),

total_votes as (
  select top 20 'total votes' as type, 
    count(*) as votes, postid as [Post Link]
  from votes
  where votetypeid = 2 or votetypeid = 3 -- up/down-votes!
  group by postid
  order by votes desc
),

up_votes as (
  select top 20 'up votes' as type, 
    count(*) as votes, postid as [Post Link]
  from votes
  where votetypeid = 2 -- upvotes!
  group by postid
  order by votes desc
),

high_score as (
  select top 20 'highest score' as type,
    score as votes, id as [Post Link]
  from posts
  order by score desc
),

low_score as (
  select top 20 'lowest score' as type,
    score as votes, id as [Post Link]
  from posts
  order by score asc
)

select * from(
  select * from down_votes
union
  select * from total_votes
union 
  select * from up_votes
union
  select * from high_score
union
  select * from low_score) a
left join posts
  on a.[Post Link] = posts.id
left join posttypes
  on posts.posttypeid = posttypes.id
order by type, votes desc

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results