Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get the count of a subcategories
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mary_itohan
Posting Yak Master

191 Posts

Posted - 11/08/2012 :  09:32:17  Show Profile  Reply with Quote
hi guys,
am a bit confused as a have a table with a PK and FK using categories and sub categories tables.

The fk is the categoryID which is an int ( and not the name )

How do i get the count of the categories in the sub_cat table

here is my code

many thanks

SELECT     dbo.Gifts_Category.CategoryName, COUNT(dbo.Gifts_SubCategory.SCName) AS 'mary'
FROM         dbo.Gifts_Category INNER JOIN
                      dbo.Gifts_SubCategory ON dbo.Gifts_Category.CategoryID = dbo.Gifts_SubCategory.CategoryID
GROUP BY dbo.Gifts_SubCategory.SCName, dbo.Gifts_Category.CategoryName


_____________________


Yes O !

jimf
Flowing Fount of Yak Knowledge

USA
2875 Posts

Posted - 11/08/2012 :  09:45:51  Show Profile  Reply with Quote
If you just want a count of how many sub categories are associated with a CategoryName:

SELECT dbo.Gifts_Category.CategoryName, COUNT(DISTINCT dbo.Gifts_SubCategory.SCName) AS 'mary'
FROM dbo.Gifts_Category INNER JOIN
dbo.Gifts_SubCategory ON dbo.Gifts_Category.CategoryID = dbo.Gifts_SubCategory.CategoryID
GROUP BY dbo.Gifts_Category.CategoryName

You don't ever want to count, sum, etc. things you are grouping by

Jim



Everyday I learn something that somebody else already knew
Go to Top of Page

mary_itohan
Posting Yak Master

191 Posts

Posted - 11/08/2012 :  11:05:19  Show Profile  Reply with Quote
ok tnx

_____________________


Yes O !
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000