Skip to content

How to group

agershun edited this page Dec 28, 2014 · 1 revision

How to group?

Source: StackOverflow.com

Question

For example I have this data :

    {"date":"2013/06/26","name":"A","number":1}
    {"date":"2013/06/26","name":"A","number":5}
    {"date":"2013/06/27","name":"B","number":4}
    {"date":"2013/06/27","name":"A","number":4}

and I want write like this query but with LINQ for node js

    SELECT date, name, SUM(number)
    FROM data
    GROUPBY date, name

Answer

    var data = [{"date":"2013/06/26","name":"A","number":1},
                {"date":"2013/06/27","name":"A","number":4}];

    var res = alasql('SELECT [date], name, SUM(number) AS number \
                          FROM ? GROUP BY [date], name', [data]);

Try this example at jsFiddle.

Comments: [date] field is in square brackets, because "DATE" is a reserved word for SQL.

Clone this wiki locally