我有以下三张表:
任务表 - task_id,activity_id(FOREIGN KEY),start_date,end_date
活动表 - activity_id,milestone_id(外键),other_fields
里程碑表 - milestone_id,project_id,other_fields
现在我想要像这样的结果集
No_of_task(count) | no_activity | milestone_name
5 : 2 : ABC Milestone
我想计算end_dated任务,根据end_date任务和里程碑名称计算活动。
例如,1个里程碑名称Housing_Construction有5个活动和20个不同的任务...某个任务结束了所以
5个任务结束了3个这样的住房建设活动。
任务表说明
+-----------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+----------------+
| activity_task_id | int(11) | NO | PRI | NULL | auto_increment |
+-----------------------+-------------+------+-----+---------+----------------+
| milestone_activity_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| task | varchar(50) | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| description | text | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| assign_to_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| assign_date | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| task_end_date | date | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| tasktime | varchar(50) | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| status | tinyint(1) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| is_delete | tinyint(1) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| entry_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| edit_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| createddatetime | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| modifydatetime | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| nooftimesedit | smallint(6) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| completion_date | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
ActivityTable说明
+-----------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+----------------+
| milestone_activity_id | int(11) | NO | PRI | NULL | auto_increment |
+-----------------------+-------------+------+-----+---------+----------------+
| project_milestone_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| activityname | varchar(50) | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| activity_end_date | date | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| activity_description | text | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| status | tinyint(1) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| is_delete | tinyint(1) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| entry_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| edit_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| createddatetime | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| modifydatetime | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| nooftimesedit | smallint(6) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
里程碑表格说明
+-----------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+----------------+
| project_milestone_id | int(11) | NO | PRI | NULL | auto_increment |
+-----------------------+-------------+------+-----+---------+----------------+
| project_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| milestone | varchar(50) | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| milestone_description | text | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| milestone_end_date | date | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| status | tinyint(1) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| is_delete | tinyint(1) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| entry_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| edit_employee_id | int(11) | YES | MUL | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| createddatetime | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| modifydatetime | datetime | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
| nooftimesedit | smallint(6) | YES | | 0 | |
+-----------------------+-------------+------+-----+---------+----------------+
| noofdays | int(11) | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+
答案 0 :(得分:0)
试试这个:
select
count(distinct t.activity_task_id) No_of_task,
count(distinct a.milestone_activity_id) no_activity,
m.milestone_description milestone_name
from milestone m
inner join activity a
on m.project_milestone_id = a.project_milestone_id
inner join task t
on a.milestone_activity_id = t.milestone_activity_id
where t.status != 4
and t.task_end_date < curdate()
group by m.project_milestone_id,
m.milestone_description;
答案 1 :(得分:0)
您可以使用JOIN
来实现此目的,请使用以下代码
SELECT COUNT(T.task_id) as no_of_task,
COUNT(A.activity_id) as no_of_activity,
MAX(M.milestone_name) as milestone_name
FROM milestone M
INNER JOIN activity A on M.milestone_id=A.milestone_id
INNER JOIN task T on T.activity_id =A.activity_id
GROUP BY A.activity_id,T.task_id
HAVING T.end_date < curdate()