patternsqlMinor
SEDE-Query for Rep-Trivia
Viewed 0 times
triviaqueryrepforsede
Problem
The code should be relatively self-explanatory. I was wondering how much rep the Top Voters on sites did "generate" on a site, in comparison to how much rep they "own".
Therefore I created a little SEDE-Query and I am now wondering, if I follow good SQL-Practices and how the performance of the query could possibly be improved (even though it's rather simple already..)
For anyone who wants to run the newest query, you can do so here: Rep "Created" vs Rep "owned" by topX voters
Therefore I created a little SEDE-Query and I am now wondering, if I follow good SQL-Practices and how the performance of the query could possibly be improved (even though it's rather simple already..)
WITH Voters AS(
SELECT DISTINCT UpVotes, DownVotes, Id, Reputation
FROM Users
WHERE Reputation != 101 AND Reputation != 1 --Exclude users without repchanges
)
SELECT TOP ##topX:int?20##
Users.Id as [UserLink],
Users.DisplayName as [SortName],
Voters.Upvotes * 8 as [EstimatedAddedRep],
--as there is no split between question and answer votes
-- we take 8 as median in accordance to the tendency that there are more
-- answer votes (10 rep) than question votes (5 rep)
Voters.Downvotes * 3 as [EstimatedDestroyedRep],
Users.Reputation as [OwnedRep],
Voters.Upvotes * 8 / Users.Reputation as [Ratio]
FROM Users, Voters
WHERE users.id = Voters.Id
GROUP BY Users.Id, Users.DisplayName, Users.Reputation, Voters.UpVotes, Voters.DownVotes
ORDER BY EstimatedAddedRep DESCFor anyone who wants to run the newest query, you can do so here: Rep "Created" vs Rep "owned" by topX voters
Solution
First of all, when you are selecting and want to perform mathematics on it you should directly specify what you want done
instead of
it should be
And
Should be
you should always specify with parenthesis when performing arithmetic in code, because it doesn't always do what you think it will do.
I don't do this with my
I would do this
and leave out the WHERE clause.
For the issue brought up in Chat you would just need to add it in like this (simple query so not an issue to repeat yourself in the select)
Check all your Columns and Tables as well, you have some weird casing issues where sometimes you capitalize and sometimes you don't, it was hard to figure out what was right
instead of
Voters.Upvotes * 8 as [EstimatedAddedRep],it should be
(Voters.Upvotes * 8) AS [EstimatedAddedRep]And
Voters.Upvotes * 8 / Users.Reputation as [Ratio]Should be
((Voters.Upvotes * 8) / (Users.Reputation)) as [Ratio]you should always specify with parenthesis when performing arithmetic in code, because it doesn't always do what you think it will do.
I don't do this with my
FROM and WHERE StatementsFROM Users, Voters
WHERE users.id = Voters.IdI would do this
FROM Users INNER JOIN Voters ON Users.Id = Voters.Idand leave out the WHERE clause.
For the issue brought up in Chat you would just need to add it in like this (simple query so not an issue to repeat yourself in the select)
((Voters.Upvotes * 8) / Users.Reputation) AS RepCreated --?????Check all your Columns and Tables as well, you have some weird casing issues where sometimes you capitalize and sometimes you don't, it was hard to figure out what was right
Code Snippets
Voters.Upvotes * 8 as [EstimatedAddedRep],(Voters.Upvotes * 8) AS [EstimatedAddedRep]Voters.Upvotes * 8 / Users.Reputation as [Ratio]((Voters.Upvotes * 8) / (Users.Reputation)) as [Ratio]FROM Users, Voters
WHERE users.id = Voters.IdContext
StackExchange Code Review Q#50920, answer score: 4
Revisions (0)
No revisions yet.