-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Poststats #9
base: main
Are you sure you want to change the base?
Poststats #9
Conversation
DB/dbo/PostStatsView.sql
Outdated
with cte_Users as (select Username from Users) | ||
,cte_Email as (select username, Email from users) | ||
,cte_First_post as (select authorusername, min(CreatedAt) First_Post from articles group by AuthorUsername) | ||
,cte_Last_Post as (select authorusername, max(CreatedAt) Last_Post from articles group by authorusername) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First_post and Last_post can be the same cte bc they pull from the same table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also article_count and distinct_articles
DB/dbo/PostStatsView.sql
Outdated
,cte_First_post as (select authorusername, min(CreatedAt) First_Post from articles group by AuthorUsername) | ||
,cte_Last_Post as (select authorusername, max(CreatedAt) Last_Post from articles group by authorusername) | ||
,cte_Article_Count as (select authorusername, count(id) Article_Count from Articles group by authorusername) | ||
,cte_Distinct_Articles as (select authorusername, count(body)-count(distinct body) Distinct_Articles from Articles group by authorusername) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cte name is misleading
DB/dbo/PostStatsView.sql
Outdated
,cte_First_Comment as (select username, min(createdat) First_Comment from comments group by username) | ||
,cte_Last_Comment as (select username, max(createdat) Last_Comment from Comments group by username) | ||
,cte_Comment_Count as (select username, count(body) Comment_Count from Comments group by username) | ||
,cte_Deleted_Comments as (select username, count(DeletedAt) Deleted_Comments from comments group by username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 ctes that pull only from comments can be 1
DB/dbo/PostStatsView.sql
Outdated
,cte_Comment_Count as (select username, count(body) Comment_Count from Comments group by username) | ||
,cte_Deleted_Comments as (select username, count(DeletedAt) Deleted_Comments from comments group by username) | ||
,cte_Comments_Own_Post as (select comments.username, count(comments.Username) Comments_Own_Post from Comments | ||
join articles on comments.ArticleId=articles.id where comments.username=articles.authorusername group by username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move select to a new line, indented. indent join so it lines up with its select. Put the where and group by both on their own lines with the same indent.
DB/dbo/PostStatsView.sql
Outdated
,cte_Deleted_Comments as (select username, count(DeletedAt) Deleted_Comments from comments group by username) | ||
,cte_Comments_Own_Post as (select comments.username, count(comments.Username) Comments_Own_Post from Comments | ||
join articles on comments.ArticleId=articles.id where comments.username=articles.authorusername group by username) | ||
select users.username, cte_email.Email, cte_first_post.First_Post, cte_Last_Post.Last_Post, cte_Article_Count.Article_Count, cte_Distinct_Articles.Distinct_Articles, cte_First_Comment.First_Comment, cte_Last_Comment.Last_Comment, cte_Comment_Count.Comment_Count, cte_Deleted_Comments.Deleted_Comments, cte_Comments_Own_Post.Comments_Own_Post from Users |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put from on its own line and add an alias
DB/dbo/PostStatsView.sql
Outdated
,cte_Comments_Own_Post as (select comments.username, count(comments.Username) Comments_Own_Post from Comments | ||
join articles on comments.ArticleId=articles.id where comments.username=articles.authorusername group by username) | ||
select users.username, cte_email.Email, cte_first_post.First_Post, cte_Last_Post.Last_Post, cte_Article_Count.Article_Count, cte_Distinct_Articles.Distinct_Articles, cte_First_Comment.First_Comment, cte_Last_Comment.Last_Comment, cte_Comment_Count.Comment_Count, cte_Deleted_Comments.Deleted_Comments, cte_Comments_Own_Post.Comments_Own_Post from Users | ||
left join cte_Email on cte_email.username=users.username |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all these joins should have aliases
No description provided.