0
a project has_many tasks
a task can be assigned to many members

例如:

Project   Task  Member
-------   ----  ------
   P1      T1     M1
   P1      T2     M2
   P2      T1     M1
   P2      T2     M2
   P2      T3     M1
   P3      T1     M2

我想获得以下摘要,即按成员划分的项目的最后一项任务:

 ------ ---
 T1      3
 - M1    2
 - M2    1
 T2      2
 - M2    2
 T3      1
 - M1    1

如何构建 SQL?

4

1 回答 1

1

使用以下 SQL

   select task, null as member, COUNT(*) as count from yourtable group by task
   union
   select task, member, COUNT(*) from yourtable group by task, member
   order by task, member
于 2012-08-16T11:41:43.603 回答