好吧,我可能也早些发布了这个,但到目前为止无法找到答案,所以请帮我解决这个问题。
我的数据库结构:
ATT (Activity
表)
Emp_ID
)Project_ID
)Product_ID
)New, OnHold, Cancel, Closed
)Product_Table
Project_Table
Employee_Table
约束
Act_ID
)Status
可以是任何给定值现在在我的SQL查询中我想要做的是加载检查::
为员工分配的活动(由Act_ID
表示)由Assigned_To_ID
,项目(Project_ID
中的ATT_Table
)和特定状态表示。我需要从我的SQL查询中输出5个值:
现在可能存在一个条件,即员工让E1说正在处理项目P1,P2。所以我的表输出看起来像这样::
A B C D E
E1 P1
E1 P2
所以这里C,D,E将具有与该项目(P1或P2)的活动相对应的值
到目前为止我已尝试过这个::
SELECT
F_Name AS A,
Project_Name AS B,
Count(Act_ID) AS C,
Count(Act_ID) AS D
FROM
Employee_Table, ATT_Table, Project_Table
WHERE
ATT_table.[Assigned_To_ID] = Employee_Table.[Emp_ID]
AND Project_Table.[Project_ID] = ATT_Table.[Project_ID]
AND Count(Act_ID) IN (SELECT Count(Act_ID)
FROM ATT_Table
WHERE ATT_Table.[Status] <> 'Closed'
AND ATT_Table.[Status] <> 'OnHold')
GROUP BY
F_Name, Project_Name;
我正在获取A,B,C。但是当我尝试查找状态检查的活动时,我无法执行此查询,因为它给了我一条消息,无法在WHERE子句中写入计数。请帮我解决这个问题。 谢谢..
答案 0 :(得分:1)
SELECT
E1.F_Name AS A,
P1.Project_Name AS B,
Count(A1.Act_ID) AS C,
sum( IF( A1.Status IN ( 'Closed', 'OnHold' ), 0, 1 )) as D,
(Count(A1.Act_ID) / sum( IIF( A1.Status IN ( 'Closed', 'OnHold' ), 0, 1 ))) * 100 as E
FROM
ATT_Table A1
JOIN Employee_Table E1
ON A1.Assigned_To_ID = E1.Emp_ID
JOIN Project_Table P1
ON A1.Project_ID = P1.Project_ID
GROUP BY
F_Name, Project_Name;
答案 1 :(得分:1)
也许是这样的。试试吧......
Select Emp.F_Name As A,
Proj.Project_Name As B,
Count(ATT.Act_ID) As C,
sum( IIF( ATT.Status IN ( 'Closed', 'OnHold' ), 0, 1 )) as D,
(Count(ATT.Act_ID) / sum( IIF( ATT.Status IN ( 'Closed', 'OnHold' ), 0, 1 ))) * 100 as E
From Employee_Table As Emp Inner Join
(Product_Table As Prod Inner Join
(ATT_Table As ATT Inner Join Project_Table As Proj On Proj.Project_ID=ATT.Project_ID)
On Prod.Product_ID=ATT.Product_ID)
On Emp.Emp_ID=ATT.Assigned_To_ID
Group By Emp.F_Name,Proj.Project_Name
答案 2 :(得分:1)
您提到Product_Table
,但我不知道您的问题如何涉及该表格。对于其余部分,我将从子查询中提取列A,B,C和D,并在外部查询中计算E.
SELECT
sub.F_Name AS A,
sub.Project_Name AS B,
sub.C,
sub.D,
((sub.C / sub.D) * 100) AS E
FROM
(
SELECT
Emp.F_Name,
Proj.Project_Name,
Count(att.Act_ID) AS C,
Sum(IIf(att.Status IN ('New', 'InProcess'), 1, 0)) AS D
FROM
(ATT_Table AS att
INNER JOIN Employee_Table AS Emp
ON Emp.Emp_ID = att.Assigned_To_ID)
INNER JOIN Project_Table AS Proj
ON Proj.Project_ID = att.Project_ID
GROUP BY
Emp.F_Name,
Proj.Project_Name
) AS sub;