patternsqlMinor
What percentage of down votes are users responsible for?
Viewed 0 times
whataredownforusersresponsiblepercentagevotes
Problem
I put together this simple query to calculate the percentage of downvotes each user is responsible for and displaying them in order of downvote percentage. It's generating the desired results, but I am uncertain about style and best practices.
I read about the capitalization of keywords, and decided it would be useful in case of working with SQL inside strings, where syntax highlighting wouldn't be available.
WITH DownVoteCount AS (
SELECT DownVoteCount = CAST(SUM(DownVotes) AS float)
FROM Users
), DownVoteWeight AS (
SELECT DisplayName, UpVotes, DownVoteWeight = ROUND(DownVotes / (SELECT DownVoteCount from DownVoteCount) * 100, 4)
FROM Users
Where Users.DownVotes > 0
)
SELECT DisplayName, DownVoteWeight
FROM DownVoteWeight
ORDER BY DownVoteWeight DESC, UpVotesI read about the capitalization of keywords, and decided it would be useful in case of working with SQL inside strings, where syntax highlighting wouldn't be available.
Solution
SQL casing shouldn't matter (much)
Every SQL developer I know writes their queries a bit differently. Case/style shouldn't matter much or at all, if the SQL code works but it's a bit sloppy. Favor substance over style. For example, these 3 queries are identical, as far as the database engine cares:
That being said...
...there are a few things we could probably improve in your own query...
Some indentation would feel nice, especially in the subqueries/CTEs. Some indentation will make your code much easier to read:
Lines breaks
Line breaks...also feel nice, especially for breaking down business logic, which
Every SQL developer I know writes their queries a bit differently. Case/style shouldn't matter much or at all, if the SQL code works but it's a bit sloppy. Favor substance over style. For example, these 3 queries are identical, as far as the database engine cares:
/* These are all exactly the same in SQL: */
SELECT FOO, BAR FROM TEST;
Select Foo, Bar From Test;
select foo, bar from test;That being said...
...there are a few things we could probably improve in your own query...
Some indentation would feel nice, especially in the subqueries/CTEs. Some indentation will make your code much easier to read:
WITH foo AS (
...
),
bar AS (
...
)
--etcLines breaks
Line breaks...also feel nice, especially for breaking down business logic, which
DownVoteWeight is (for instance), where it can be broken down into meaningful functions/operations. This one might deserve a bit more documentation.SELECT
DisplayName,
UpVotes,
DownVoteWeight = ROUND(
DownVotes / (SELECT DownVoteCount from DownVoteCount) * 100, 4
)Code Snippets
/* These are all exactly the same in SQL: */
SELECT FOO, BAR FROM TEST;
Select Foo, Bar From Test;
select foo, bar from test;WITH foo AS (
...
),
bar AS (
...
)
--etcSELECT
DisplayName,
UpVotes,
DownVoteWeight = ROUND(
DownVotes / (SELECT DownVoteCount from DownVoteCount) * 100, 4
)Context
StackExchange Code Review Q#118330, answer score: 4
Revisions (0)
No revisions yet.