patternsqlMinor
Popular questions by view count
Viewed 0 times
countquestionsviewpopular
Problem
I made this query to create a graph of a user's popular questions and the view count on that question. It allows for a minimum of
Finally, here's some sample input (I'm using @Mat'sMug's user ID):
500 views, and a score of 3.DECLARE @allowed_min_views INT = 500;
DECLARE @allowed_min_score INT = 3;
DECLARE @user_id INT = ##UserId:int?-1##;
DECLARE @min_views INT = ##MinimumViews:int?500##;
DECLARE @min_score INT = ##MinimumScore:int?3##;
DECLARE @question INT = 1;
IF (@min_views = @allowed_min_views AND @min_score >= @allowed_min_score)
BEGIN
SELECT
ViewCount
, Score
FROM Posts WHERE
PostTypeId = @question
AND OwnerUserId = @user_id
AND ViewCount >= @min_views
AND Score >= @min_score
ORDER BY ViewCount ASC;
ENDFinally, here's some sample input (I'm using @Mat'sMug's user ID):
@user_id: 23788
@min_views: 500
@min_score: 7
Solution
Good things
You use good local variables, and you are consistent with your naming. The typical naming for T-SQL is using
You validate your values, although I am not quite sure why you chose 500 and 3 as arbitrary minimums (might be worth documenting).
Results are not very useful...
As written, your query returns this:
Which is all well and good, except, it doesn't give much information. Let's say we rewrite it a bit like this:
Notice I changed
Then we get a more sensible result set, e.g.:
You use good local variables, and you are consistent with your naming. The typical naming for T-SQL is using
PascalCase, however there are no standards and snake_case or camelCase work just as good, as long as you are consistent (which you are).You validate your values, although I am not quite sure why you chose 500 and 3 as arbitrary minimums (might be worth documenting).
Results are not very useful...
As written, your query returns this:
ViewCount Score
--------- -----
571 10
629 5
685 6
721 10
728 11
761 12
840 25
849 7
870 17
888 9
1065 10
...Which is all well and good, except, it doesn't give much information. Let's say we rewrite it a bit like this:
IF (@min_views >= @allowed_min_views AND @min_score >= @allowed_min_score)
BEGIN
SELECT
ViewCount
, Score
, [User Link] = @user_id
, [Post Link] = Id
, CreationDate
, [Tags] = Tags
FROM Posts
WHERE
PostTypeId = @question
AND OwnerUserId = @user_id
AND ViewCount >= @min_views
AND Score >= @min_score
ORDER BY ViewCount DESC;
ENDNotice I changed
ORDER BY to DESC, I think it makes more sense to show highest first.Then we get a more sensible result set, e.g.:
Code Snippets
ViewCount Score
--------- -----
571 10
629 5
685 6
721 10
728 11
761 12
840 25
849 7
870 17
888 9
1065 10
...IF (@min_views >= @allowed_min_views AND @min_score >= @allowed_min_score)
BEGIN
SELECT
ViewCount
, Score
, [User Link] = @user_id
, [Post Link] = Id
, CreationDate
, [Tags] = Tags
FROM Posts
WHERE
PostTypeId = @question
AND OwnerUserId = @user_id
AND ViewCount >= @min_views
AND Score >= @min_score
ORDER BY ViewCount DESC;
ENDContext
StackExchange Code Review Q#96703, answer score: 8
Revisions (0)
No revisions yet.