The GROUP BY clause groups rows with the same values in specified columns into summary rows. The HAVING clause filters those groups based on conditions — similar to WHERE, but applied after grouping.
Basic Syntax
SELECT column, Aggregate_Function(column)
FROM table
GROUP BY column
HAVING condition;
Why Important in Healthcare Analytics?
- Summarizing large datasets (claims, members, costs)
- Finding patterns like high-cost providers or frequent diagnoses
- Filtering aggregated results
Healthcare Examples
1. Basic GROUP BY – Average RAF Score by Age Group
SELECT
AgeGroup,
COUNT(*) AS MemberCount,
AVG(RAF_Score) AS AvgRAF
FROM Risk_Adjustment_Members
GROUP BY AgeGroup
ORDER BY AvgRAF DESC;
2. GROUP BY + HAVING – Providers with High Denial Rates
SELECT
ProviderID,
ProviderName,
COUNT(*) AS TotalClaims,
SUM(CASE WHEN Status = 'Denied' THEN 1 ELSE 0 END) AS DeniedClaims
FROM Claims
GROUP BY ProviderID, ProviderName
HAVING SUM(CASE WHEN Status = 'Denied' THEN 1 ELSE 0 END) > 50;
Finds providers who had more than 50 denied claims.
Key Takeaways
- GROUP BY is used with aggregate functions (COUNT, SUM, AVG, MAX, MIN)
- WHERE filters individual rows before grouping
- HAVING filters groups after grouping
- Very useful for risk adjustment reporting, cost analysis, and quality metrics