Add count of group that does not exist for the row

44 Views Asked by At

I have a collection of food orders, every object consist of day of the order and chosen food. My goal is to keep a record of the food during the week and count the numbers how many food of each type was ordered.

I've been using AQL for a while, but can not figure it out how add a counting, like add a number zero, for the days in which the specific food wasn´t ordered.

My AQL query is like this:

for fo in foodorders
 collect day = fo.dayOfTheWeek, chosenfood = fo.food with count into length
 return {dateOrder: day, food: chosenFood, cnt: length}

The result is like this:

[
  {
    "dayOfTheWeek": "Monday",
    "food": "Burger",
    "cnt": 400
  },
  {
    "date": "Monday",
    "food": "Salad",
    "cnt": 100
  },
  {
    "dayOfTheWeek": "Tuesday",
    "food": "Fries",
    "cnt": 200
  },
{
    "dayOfTheWeek": "Tuesday",
    "food": "Burger",
    "cnt": 200
  },
  {
    "dayOfTheWeek": "Tuesday",
    "food": "Salad",
    "cnt": 120
  },...
]

But I need something like this:

[
  {
    "dayOfTheWeek": "Monday",
    "food": "Burger",
    "cnt": 400
  },
  {
    "date": "Monday",
    "food": "Salad",
    "cnt": 100
  },
  {
    "date": "Monday",
    "food": "Fries",
    "cnt": 0
  },
  {
    "dayOfTheWeek": "Tuesday",
    "food": "Fries",
    "cnt": 200
  },
{
    "dayOfTheWeek": "Tuesday",
    "food": "Burger",
    "cnt": 200
  },
  {
    "dayOfTheWeek": "Tuesday",
    "food": "Salad",
    "cnt": 120
  },...
]
0

There are 0 best solutions below