For reporting I want to group some counts together but still have access to the row data. I feel like I do this a lot but can't find any of the code today. This is in a B2B video delivery platform. A simplified version of my modelling:
- Video(name)
- Company(name)
- View(video, company, when)
I want to show which companies viewed the most videos, but also show which Videos were the most popular.
+-----------+-----------+-------+
| Company | Video | Views |
+-----------+-----------+-------+
| Company E | Video 1 | 1215 |
| | Video 2 | 5 |
| | Video 3 | 1 |
| Company B | Video 2 | 203 |
| | Video 4 | 3 |
+-----------+-----------+-------+
I can do that by annotating views (to get the right to a company order, most views first) and then looping companies and performing a View query for each company, but how can I do this in in O(1-3) queries rather than O(n)?