Sunday, September 6, 2009

70-433 SQL Server GROUP BY ... HAVING COUNT(*)

70-433 SQL Server GROUP BY ... HAVING COUNT(*) 


You will need to be familiar with standard format/syntax of queries of the format: 


  SELECT ... FROM ... JOIN ... ON ... GROUP BY .. HAVING COUNT(*)


To retrieve the number of widgets from each widget category that has more than 5 widgets, you could do this:


SELECT WidgetCategory, count(*)
FROM Widgets
GROUP BY WidgetCategory
HAVING count(*) > 5

The "having" clause is something people often forget about, instead opting to retrieve all their data to the client and iterating through it there.

http://stackoverflow.com/questions/2702/how-do-i-use-t-sql-group-by

No comments: