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 |
|
dannylister
Starting Member
9 Posts |
Posted - 2007-10-24 : 18:57:41
|
| Hi all,I've got a slight problem with a stored procedure I'm trying to create. I'll talk you through the story so far. I currently have the following tables within my database:Categories------------------IDNamePosts------------------IDDateTitleAnd a many to many relationship table where each ID below links to the respective table above:PostsCategory------------------PostIDCategoryIDWhat I want to do is to retrieve a list of categories and the total amount of posts in each category. So far, my SQL statement for doing this is:SELECT DISTINCTCategories.NameCOUNT(PostsCategory.CategoryID) AS TotalPostsFROMCategoryINNER JOINPostsCategory ON Categories.ID = PostsCategory.CategoryIDGROUP BYCategory.NameORDER BYCategory.NameThis works perfectly except for when a category has no posts. Ie: there's no links in the PostsCategory table. I would like the statement to retreive all categories no matter how many posts have been made. For example, the above SQL statement returns the following:Name, TotalPosts------------------------------------Category 1, 10Category 2, 5Category 4, 47But I want it to be able to return the below example:Name, TotalPosts------------------------------------Category 1, 10Category 2, 5Category 3, 0Category 4, 47Category 5, 0Category 6, 0If anyone could possibly help me then that would be great Sorry for the poor formatting. Tabs won't displayCheers, from Danny.http://www.dannylister.co.uk/ |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-10-24 : 19:10:14
|
| [code]SELECT Id, Name, (Select count(*) FROM PostsCategory WHERE CategoryID = C.CategoryID) As TotalPostsFROM Categories C[/code]Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
dannylister
Starting Member
9 Posts |
Posted - 2007-10-25 : 04:54:57
|
| Thanks for your reply. It worked perfectly. I seemed to have over complicated things when trying to solve the problem. What about returning the same set of results but where the total posts is greater then 0?I presumed I would of just added a where clause to the main body of the statement (see below), but it fails to recognise the 'TotalPosts' aliasWHERE TotalPosts > 0Cheers for your help.Danny.http://www.dannylister.co.uk/ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-25 : 05:00:20
|
[code]SELECT c.ID, c.Name, t.ToalPostsFROM Categories C INNER JOIN ( SELECT CategoryID, TotalPosts = COUNT(*) FROM PostsCategory GROUP BY CategoryID HAVING COUNT(*) > 0 ) t ON c.ID = t.CategoryID[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-25 : 05:05:45
|
quote: I presumed I would of just added a where clause to the main body of the statement (see below), but it fails to recognise the 'TotalPosts' aliasWHERE TotalPosts > 0
You can't reference an column alias in the where clause.One workaround is use a derived tableselect *from( SELECT Id, Name, (Select count(*) FROM PostsCategory WHERE CategoryID = C.CategoryID) As TotalPosts FROM Categories C) awhere TotalPosts > 0 Or write the query using INNER JOIN as in my earlier post. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-25 : 05:08:14
|
Simpler?SELECT c.Name, COUNT(pc.CategoryID) AS TotalPostsFROM Categories AS cLEFT JOIN PostsCategory AS pc ON pc.CategoryID = c.IDGROUP BY c.NameORDER BY c.Name E 12°55'05.25"N 56°04'39.16" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-25 : 05:18:28
|
Yes yes yes. What was i thinking  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
dannylister
Starting Member
9 Posts |
Posted - 2007-10-25 : 05:37:51
|
| Cheers for you help guys :)http://www.dannylister.co.uk/ |
 |
|
|
|
|
|
|
|