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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Simple grouping query...

Author  Topic 

dang12
Starting Member

12 Posts

Posted - 2008-11-03 : 05:30:34
Hi,

I have the following query:

SELECT TOP(100) Tags.TagText, TaggedBrand.BrandId, TaggedBrand.[Count]
FROM Tags, TaggedBrand
WHERE Tags.TagId = TaggedBrand.TagId AND TaggedBrand.BrandId = '113c56d6-7273-4037-981f-df58d7e0ac9d'

Which returns:

TagText BrandId Count
nice 113C56D6-7273-4037-981F-DF58D7E0AC9D 1
chocolate 113C56D6-7273-4037-981F-DF58D7E0AC9D 2
chocolate 113C56D6-7273-4037-981F-DF58D7E0AC9D 1

My question is, how can I get it to return a sum of the count column?

I.e:

TagText BrandId Count
nice 113C56D6-7273-4037-981F-DF58D7E0AC9D 1
chocolate 113C56D6-7273-4037-981F-DF58D7E0AC9D 3

many thanks!


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-03 : 05:33:31
[code]SELECT TOP(100) Tags.TagText, TaggedBrand.BrandId, SUM(TaggedBrand.[Count]) AS Count
FROM Tags, TaggedBrand
WHERE Tags.TagId = TaggedBrand.TagId AND TaggedBrand.BrandId = '113c56d6-7273-4037-981f-df58d7e0ac9d'
GROUP BY Tags.TagText, TaggedBrand.BrandId
ORDER BY ....
[/code]
also you need to order on some field whithout which top 100 is meaningless
Go to Top of Page

dang12
Starting Member

12 Posts

Posted - 2008-11-03 : 05:36:09
Thanks! - that's exactly what I needed!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-03 : 06:09:23
Cheers
Go to Top of Page
   

- Advertisement -