- 
                Notifications
    You must be signed in to change notification settings 
- Fork 689
How to SQL group by non aggregate condition
        Mathias Rangel Wulff edited this page Jun 13, 2015 
        ·
        2 revisions
      
    Source: Stackoverflow.com
s it possible to have non-aggregate condition on groups? For example we have:
    Table1(firstName, lastName, gender)
And we group by firstName and then by lastName, but we want only the groups having at least 5 males in it.
    SELECT * 
        FROM people 
        GROUP BY FirstName, LastName 
        HAVING SUM(CASE WHEN Gender = "M" THEN 1 ELSE 0 END) >= 5 See the working AlaSQL example in jsFiddle
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo