- 
                Notifications
    You must be signed in to change notification settings 
- Fork 689
Having
        Mathias Rangel Wulff edited this page Jun 12, 2015 
        ·
        6 revisions
      
    Filtering groups by expressions.
Syntax:
    SELECT ... GROUP BY gorup-expressions HAVING expression ...For example, select countries with number of cities > 2:
    var res = alasql('SELECT *, COUNT(*) AS cnt FROM City \
                GROUP BY Country \
                HAVING cnt > 2');Expressions can contain aggregators, like below:
    var res = alasql('SELECT * FROM City \
                GROUP BY Country \
                HAVING COUNT(*) > 2');You can use aggregators in the HAVING clause like in this example:
var groups = [{id:4, name:"abcd", id_group:"1"},
              {id:5, name:"efgh", id_group:"1"},
              {id:6, name:"ijkl", id_group:"1"},
              {id:4, name:"abcd", id_group:"2"},
              {id:7, name:"mnop", id_group:"2"}];
               
var res = alasql('select id_group, count(id) as cnt from ? where id in (4,7)\
group by id_group having count(id) = 2',[groups]);  See this example in jsFiddle
See also: WHERE, [GROUP BY](Group By)
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo