Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
myksdsu
Starting Member
13 Posts |
Posted - 2007-06-04 : 15:58:41
|
I have three tables:Category: category id, category name, more…Topic: topic id, topic name, category id, more…Post: post id, post text, topic id, more…I need help with a query to display the following:Category name, # of topics, # of postsExample:Category.........................Topics.....PostsSQL Stored Procedures.........12........562It’s coming along but there are some problems, ASP.NET actually has 2 posts not 1. And the java totals are correct but it should be Java, 3, 10 (all in one line)Category.....Topics...PostsASP.NET.........2........1C#................1........1Java..............1........1Java..............1........2Java..............1........7Overview: use category id to get count of topics then use the topic id to get the count of posts.SELECT C.CategoryName, T.ThreadCount AS Threads, T.PostCount AS PostsFROM Category AS C LEFT OUTER JOIN (SELECT tt.CategoryID, PostID.PostCount, COUNT(tt.ThreadName) AS ThreadCount FROM Thread AS tt LEFT OUTER JOIN (SELECT ThreadID, COUNT(PostID) AS PostCount FROM Post AS P GROUP BY ThreadID) AS PostID ON tt.ThreadID = PostID.ThreadID GROUP BY tt.CategoryID, PostID.PostCount) AS T ON C.CategoryID = T.CategoryIDWHERE (C.CategoryID = T.CategoryID)GROUP BY C.CategoryName, T.ThreadCount, T.PostCountORDER BY C.CategoryName Thanks in advance |
|
|
sagi
Starting Member
1 Post |
Posted - 2007-06-04 : 16:19:35
|
| This is happening because you are using T.ThreadCount, T.PostCount in your GROUP BY. remove them from there and it will show your results...SELECT C.CategoryName, T.ThreadCount AS Threads, T.PostCount AS PostsFROM Category AS C LEFT OUTER JOIN (SELECT tt.CategoryID, PostID.PostCount, COUNT(tt.ThreadName) AS ThreadCount FROM Thread AS tt LEFT OUTER JOIN (SELECT ThreadID, COUNT(PostID) AS PostCount FROM Post AS P GROUP BY ThreadID) AS PostID ON tt.ThreadID = PostID.ThreadID GROUP BY tt.CategoryID, PostID.PostCount) AS T ON C.CategoryID = T.CategoryIDWHERE (C.CategoryID = T.CategoryID)GROUP BY C.CategoryNameORDER BY C.CategoryName |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-04 : 16:47:24
|
| [code]SELECT c.CategoryName , TheCounts.PostCount , TheCounts.ThreadCountFROM Category AS c LEFT OUTER JOIN--Count of Threads & Posts (SELECT t.CategoryID , COUNT(p.PostID) as PostCount , COUNT(*) AS ThreadCount FROM dbo.Thread AS t LEFT OUTER JOIN dbo.Post AS p ON p.ThreadID = t.ThreadID GROUP BY t.CategoryID) AS TheCountsON TheCounts.CategoryID = c.CategoryIDORDER BY c.CategoryName[/code] |
 |
|
|
myksdsu
Starting Member
13 Posts |
Posted - 2007-06-04 : 16:54:10
|
| Thank you for your patience and help it is much appreciated!!! Pootle that solution worked great. I will spend some time and understand what you have shown me. |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-04 : 16:55:33
|
quote: Originally posted by sagi This is happening because you are using T.ThreadCount, T.PostCount in your GROUP BY. remove them from there and it will show your results...
I am afraid you will find that it will fail Short of a rejig (a la my query), the problem is with the grouping rather than summing of PostID.PostCount in the middle query. The group by in the outer query is just plain old unnecessary. As is the where clause too. |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-04 : 16:56:28
|
quote: Originally posted by myksdsu When I remove T.ThreadCount or T.PostCount from the group by, it says that T.ThreadCount and T.PostCount are invalid because they are not contained in either an aggregate function or Group by clause. any ideas on overcoming this?
|
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-04 : 17:08:57
|
quote: Originally posted by myksdsu Thank you for your patience and help it is much appreciated!!! Pootle that solution worked great. I will spend some time and understand what you have shown me.
Lol - no probs. My sniped piccy looks a bit odd now!You don't actually need a derived table. Figure out how to get rid of it and you will know you've cracked it |
 |
|
|
|
|
|
|
|