Complex Query with calculation
Hello, I need help with building a complex query (well, complex for me at least).
I have the following Models with relationships:
User -- (hasMany) --> Items -- (hasOne) --> Inspection
What I need to do: Show a count of all Inspections for each User after doing a simple date calculation on every Inspection to sort it into one of three statuses. So the result could look like this:
User 1 -> 1000 Items -> 300 Status A; 300 Status B, 400 Status C (calculated by using Inspection)
User 2 -> 2000 Items -> 500 Status A; 500 Status B, 1000 Status C (calculated by using Inspection)
I'm looking for an efficient way to query this. I did look into queries with callback function to calculate the result, but I'm not sure if this is the right way. If I do it by looping through each Item, it will result in thousands of queries. How can I optimize this and minimize query duration? Thanks!
Please or to participate in this conversation.