sql - Why is this votes/posts ratio always 1? -
i'm working sede create graph of ratio of votes posts. having eliminated actual errors, i'm faced new problem: reason, ratio always 1. current sql:
select cast(p.creationdate date) [creationdate], count(cast(v.creationdate date)) / count(cast(p.creationdate date)) [ratio] posts p inner join votes v on v.postid = p.id v.votetypeid = ##votetype:int?2## , p.posttypeid = 1 or p.posttypeid = 2 group cast(p.creationdate date) order ratio the query can found here.
it suggested in chat might because joining tables results in every possible combination, number of votes , posts same (thus n/n = 1). correct, , if should doing instead?
since both sides of in inner join exist, both count(cast(v.creationdate date)) , count(cast(p.creationdate date))will return same number, number of rows in group*.
if count how many new votes per new post you've got on given date, use count(distinct):
select cast(p.creationdate date) [creationdate], count(distinct v.id) / count(distinct p.id) [ratio] posts p inner join votes v on v.postid = p.id v.votetypeid = ##votetype:int?2## , p.posttypeid = 1 or p.posttypeid = 2 group cast(p.creationdate date) order ratio * assuming creationdate not nullable.
Comments
Post a Comment