Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Poststats #9

wants to merge 3 commits into from

Conversation

Stardagger
Copy link

No description provided.

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)
Copy link
Owner

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

Copy link
Owner

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

,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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cte name is misleading

,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)
Copy link
Owner

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

,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)
Copy link
Owner

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.

,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
Copy link
Owner

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

,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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants