Group and count by month

You need to use the $month keyword in your group. Your new Date().getMonth() call will only happen once, and will try and create a month out of the string “$bookingdatetime”.[ {$group: { _id: {$month: “$bookingdatetime”}, numberofbookings: {$sum: 1} }} ]);

Group by field name in Java

There’s probably a library that can do this more simply, but it’s not too hard to do it manually: List<Person> allPeople; // your list of all people Map<String, List<Person>> map = new HashMap<String, List<Person>>(); for (Person person : allPeople) { String key = person.getName(); if (map.get(key) == null) { map.put(key, new ArrayList<Person>()); } map.get(key).add(person); } … Read more

Grouping JSON by values

Assume , the JSON output is outJSON = [ { team: “TeamA”, name: “Ahmed”, field3:”val3″ }, { team: “TeamB”, name: “Ahmed”, field3:”val43″ }, { team: “TeamA”, name: “Ahmed”, field3:”val55″ }, ] Then see the groupBy function in the DEMO below: DEMO : outJSON = [{ team: “TeamA”, name: “Ahmed”, field3: “val3” }, { team: “TeamB”, … Read more

How to count the consecutive duplicate values in an array?

It can be done simply manually: $arr = array(1,1,1,2,2,3,3,1,1,2,2,3); $result = array(); $prev_value = array(‘value’ => null, ‘amount’ => null); foreach ($arr as $val) { if ($prev_value[‘value’] != $val) { unset($prev_value); $prev_value = array(‘value’ => $val, ‘amount’ => 0); $result[] =& $prev_value; } $prev_value[‘amount’]++; } var_dump($result);

LINQ query — Data aggregation (group adjacent)

You can use Linq’s GroupBy in a modified version which groups only if the two items are adjacent, then it’s easy as: var result = classes .GroupAdjacent(c => c.Value) .Select(g => new { SequenceNumFrom = g.Min(c => c.SequenceNumber), SequenceNumTo = g.Max(c => c.SequenceNumber), Value = g.Key }); foreach (var x in result) Console.WriteLine(“SequenceNumFrom:{0} SequenceNumTo:{1} Value:{2}”, … Read more

Group rows in a 2d array and count number of rows in each respective group

Just iterate over the array and use another array for the groups. It should be fast enough and is probably faster than the overhead involved when using sqlite or similar. $groups = array(); foreach ($data as $item) { $key = $item[‘key_to_group’]; if (!isset($groups[$key])) { $groups[$key] = array( ‘items’ => array($item), ‘count’ => 1, ); } … Read more

Merge 2d array rows with same column value and sum another column [duplicate]

Use function array_reduce() to combine the items having the same city: $input = array( array(‘city’ => ‘NewYork’, ‘cash’ => ‘1000’), array(‘city’ => ‘Philadelphia’, ‘cash’ => ‘2300’), array(‘city’ => ‘NewYork’, ‘cash’ => ‘2000’), ); $output = array_reduce( // Process the input list $input, // Add each $item from $input to $carry (partial results) function (array $carry, … Read more

Group 2d array’s row data by one column and sum another column within each group to produce a reduced 2d array

Just loop trough the rows, and add up the quantities in a different array indexed by the dd values. $in = array(array()); // your input $out = array(); foreach ($in as $row) { if (!isset($out[$row[‘dd’]])) { $out[$row[‘dd’]] = array( ‘dd’ => $row[‘dd’], ‘quantity’ => 0, ); } $out[$row[‘dd’]][‘quantity’] += $row[‘quantity’]; } $out = array_values($out); // … Read more

Group array data on one column and sum data from another column to form a flat associative array

So, first you need $amountsArray to get assigned the values you listed, somehow. Then: $bankTotals = array(); foreach($amountsArray as $amount) { $bankTotals[$amount[‘name’]] += $amount[‘amount’]; } Output: (Demo) Warning: Undefined array key “Bank BRI” Warning: Undefined array key “Bank BCA” Warning: Undefined array key “Bank CIMB Niaga” Warning: Undefined array key “Bank BNI” Warning: Undefined array … Read more