Question

I want to summarize rows from one end of a relationship tree with a table on the other side. Is "correlate" the correct term? Really just knowing the terms would help me solve this problem.

I am using MySQL and am extending an existing DB structure - though would have the liberty to rearrange data if needed. I'm getting better at creating "filtering" queries using JOINs, and I'm sure this next piece will be straight-forward once I understand it (without performing tons of queries : )

I made a simplified schema (and theme!) for this example, but the idea is the same.

Say there are many DietPlans, which is related to a bunch of MenuItems and each MenuItem has an ItemType (such as 'Healthy','Fast','Normal', etc.) On the other side of DietPlan there are Persons, who each store how many DailyCalories they consume, and another table MenuAllocations, where a Person stores how much percent of their daily intake is from what MenuItem.

As examples of scale, There could be 1000 MenuItems, and 50 of those associated with each of 200 DietPlans. Also, each DietPlan might have 10,000 Persons, who each will have 5-10 MenuAllocations of various types.

Table layout in MySQL Workbench

What I'd like to do feels complex to me. I want to create a dashboard for each DietPlan (there could be many), gathering data from the Persons of that DietPlan, and tabulating the number of calories for each item type.

The math is simple: tblPerson.dailyCalories * tblMenuAllocations.percent. But I want to do that for each Person in the DietPlan, for each ItemType.

I understand the JOINs required to 'filter' from tblItemType around to tblMenuAllocation and think it would be similar to this:

SELECT *
  FROM tblMenuAllocation
 INNER JOIN tblPerson
    on personId = PersonId
 INNER JOIN tblDietPlan
    on tblPerson.dietPlanId = tblDietPlan.DietPlanId
 INNER JOIN tblMenuItem
    on tblMenuItem.dietPlanId = tblDietPlan.DietPlanId
 INNER JOIN tblItemTyp
    on ItemTypeId = itemTypeId
 WHERE ItemTypeId = 2

It feels like one query for each tblItemType, which could be a LOT of Person and MenuAllocation data to sort through, and doing that many consecutive queries feels like I'm missing something. Also, I think math can be handled in the query to sum values, but I've never done that. Where can I begin?

EDIT: The final results would be something like this:

 ----------------------------------------------
 ItemId   |  ItemDesc  |   TotalCalories     
 ----------------------------------------------
 1          Healthy       450,876
 2          Fast          1,987,948
 3          Vegan         349,123
 etc.

I would be willing to accept some manipulation of data outside the query, but the Person's specific dailyCalories is very important to the tblMenuAllocation.percent calculation. Some tblMenuAllocation rows might be of the same ItemType!

Was it helpful?

Solution

I think you are looking for these topics : Aggregate Functions and Group By Modifiers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top