A project has many tasks.
A task can be handled by one of many users. For example:
Project Task By
------- -------- ------
P1 T1 U1
T2 U2
P2 T1 U3
T2 U4
T3 U5
P3 T1 U1
T2 U3
T3 U5
P4 T1 U1
T2 U3
How can I query the count of, the last task in projects handled by users? The output report is expected as:
Task By Count
---- ---- -----
T2 U2 1
T2 U3 1
T3 U5 2
UPDATE: (for Deefours’ comment): Here is what I did to get count of last task (but not sure how to get the By column):
self.find_by_sql('select task_id, count(task_id) as "count"
from (
select task_id
from (
select * from tasks order by created_at desc
)
tasks_sorted
group by project_id
)
latest_tasks
group by task_id;')
1 Answer