'How can I sum the data in the nested Room database?

This will probably be the simplest question on SO so first of all I apologize but I couldn't establish the answer so I had to ask it here.

Suppose you have a flowing list like this. I want to sum the dayId0 values, then dayId1 and print them on the upper layer in Room. For example, dayId0 will continue as food_kcal_sum=350 and dayId1 food_kcal_sum=45. How should I create a loop for this?

[
   {
       "dayId": 0,
       "food_kcal": 270.0,
       "id": 1
   },
   {
       "dayId": 0,
       "food_kcal": 60.0,
       "id": 2
   },
   {
       "dayId": 0,
       "food_kcal": 20.0,
       "id": 3
   },
   {
       "dayId": 1,
       "food_kcal": 15.0,
       "id": 4
   },
   {
       "dayId": 1,
       "food_kcal": 30.0,
       "id": 5
   }
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source