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
 General SQL Server Forums
 New to SQL Server Programming
 Counting Rows

Author  Topic 

wilkinj
Starting Member

2 Posts

Posted - 2010-06-24 : 11:22:17


Hi Guys
i'm a real newbie to SQL...pretty much at the Hello World stage. I need some help with counting rows from a table where a condition is met. My table looks like this:

site,userName,recordName,month,year
NY, bob, ABC.L, 5, 2010
NY, bob, DEF.L, 5, 2010
NY, bob, GRF.L, 5, 2010
NY, bob, ABC.L, 6, 2010
NY, bob, CDO.L, 6, 2010
NY, bob, REF.L, 7, 2010
NY, fred, ABC.L, 5, 2010
NY, fred, LED.L, 5, 2010
NY, fred, CAD.L, 5, 2010

So, i want to summarize the unique number of recordNames for a given userName in a particular month. So the result of what i'm looking for might be:

NY, bob, 5, 3 (where 5 = month and 3 = the count of distinct recordNames in month 5)

I hope this makes sense. Any assistance would be greatly appreciated.

Thanks, regards


GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-06-24 : 11:33:00
SELECT site, UserName, Month, Year, Count(*)
FROM
SomeTable
GROUP BY site, UserName, Month, Year

If you want to limit to a particular month/year, you'd add a where clause before the Group By

--
Gail Shaw
SQL Server MVP
Go to Top of Page

wilkinj
Starting Member

2 Posts

Posted - 2010-06-24 : 12:36:38
quote:
Originally posted by GilaMonster

SELECT site, UserName, Month, Year, Count(*)
FROM
SomeTable
GROUP BY site, UserName, Month, Year

If you want to limit to a particular month/year, you'd add a where clause before the Group By

--
Gail Shaw
SQL Server MVP


Thank you so much for your prompt response. This works a treat!
Regards
Go to Top of Page
   

- Advertisement -