- 
                Notifications
    You must be signed in to change notification settings 
- Fork 689
How to Group By and Sum
        agershun edited this page Dec 29, 2014 
        ·
        2 revisions
      
    Source: StackOverflow.com
There is a JSON like this:
[
  {
     platformId: 1,
     payout: 15,
     numOfPeople: 4
  },
  {
     platformId: 1,
     payout: 12,
     numOfPeople: 3
  },
  {
     platformId: 2,
     payout: 6,
     numOfPeople: 5
  },
  {
     platformId: 2,
     payout: 10,
     numOfPeople: 1
  },
]
And I want to Group it by platformId with sum of payout and numOfPeople. I.e. in result I want JSON like this:
[
  "1": {
     payout: 27,
     numOfPeople: 7
   },
  "2": {
     payout: 16,
     numOfPeople: 6
  }
] 
    var res = alasql('SELECT INDEX platformId, {platformId:platformId, numOfPeople:numOfPeople} \
         FROM (SELECT platformId, SUM(payout) AS payout, SUM(numOfPeople) AS numOfPeople \
             FROM ? GROUP BY platformId)',[data]);Try this example at jsFiddle
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo