SQL Simple Having

Today I will dicuss a way to filter your aggregated data, this is done by the use of a not so often used query keyword Having .
The whole solution can be viewed at this RexTester.



I say not so used because it is only maybe about 5% of the time that you will use this keyword, there are many other ways to achieve desired results such as CTE,Subquies and window functions.



Lets pretend we have a dataset such as this:



ID Name
1 Jim
1 Jim
2 Bob
2 Bob
2 Bob



We simple want to know what name has 3 or more entries in the table. We can apply a having clause to achieve this Having clause will go outside of the group by as an addition selectivity measure in our query.



  SELECT [NAME], COUNT(*) AS RecordCount FROM #TEMP_A GROUP BY [NAME] HAVING COUNT(*) >= 3
Ian Fogelman

Ian Fogelman

My name is Ian Fogelman. I like to develop data driven solutions with SQL Server, Python, .NET and predictive analytics.