在SQL查询中使用聚合函数

时间:2021-09-27 22:50:38

I have my Table structure like this ::

我有这样的表结构::

ATT_Table : Fields - Act_ID, Assigned_To_ID, Percent_Complete(Integer value)
Act_ID is primary key, Assigned_To_ID is referenced to Emp_ID in Employee_Table.

Employee_Table : Fields - Emp_ID, F_Name. 
Emp_ID is primary key.

Now at a particular point in time, 1 or more activities can be assigned to same person. My goal is write a query to calculate a person's load. I want to count the number of activities assigned to a particular person (can be more than 1) then take the average of their percent_Complete.

现在,在特定时间点,可以为同一个人分配一个或多个活动。我的目标是编写一个查询来计算一个人的负载。我想计算分配给特定人员的活动数量(可以超过1),然后计算他们的percent_Complete的平均值。

For example if person A is assigned A1, A2, A3(Act_ID). Then corresponding (Percent_Complete values addition)/3. Basically an average. In my final query result I want:

例如,如果人A被分配A1,A2,A3(Act_ID)。然后对应(Percent_Complete值加法)/ 3。基本上是平均水平。在我的最终查询结果中,我想:

Name, Number of activities assigned(Count), load value(Avg). 

How do I this? Do I have to use a nested WHERE IN clause ? Thanks.

我怎么这样?我是否必须使用嵌套的WHERE IN子句?谢谢。

4 个解决方案

#1


1  

try the following:

尝试以下方法:

select Emp_ID, F_Name, count(Act_ID), avg(Percent_Complete)
from ATT_Table, Employee_Table where ATT_Table.Assigned_To_ID = Employee_Table.Emp_ID
group by Emp_ID, F_Name

#2


3  

SELECT  MIN(F_Name) Employee_Name ,
        COUNT(1) Activities_Assigned ,
        AVG(Percent_Complete) Load_Value
FROM    ATT_Table a
        INNER JOIN Employee_Table e ON a.Assigned_To_ID = e.Emp_ID
GROUP BY e.Emp_ID

#3


2  

I may be missing some nuance, but it sounds like you can just: join the tables, group by employee, COUNT and AVG for the load.

我可能会错过一些细微差别,但听起来你可以这样:加入表格,按员工分组,COUNT和AVG加载。

#4


0  

As Dmitri said, something like

正如德米特里所说的那样

SELECT Employee_Table.F_Name, COUNT(*) AS activities, AVG(Percent_Complete) AS load
FROM ATT_Table JOIN Employee_Table ON ATT_Table.Assigned_to_ID = Employee_Table.Emp_ID
WHERE Employee_Table.Emp_ID = 42
GROUP BY Employee_Table.Emp_ID

#1


1  

try the following:

尝试以下方法:

select Emp_ID, F_Name, count(Act_ID), avg(Percent_Complete)
from ATT_Table, Employee_Table where ATT_Table.Assigned_To_ID = Employee_Table.Emp_ID
group by Emp_ID, F_Name

#2


3  

SELECT  MIN(F_Name) Employee_Name ,
        COUNT(1) Activities_Assigned ,
        AVG(Percent_Complete) Load_Value
FROM    ATT_Table a
        INNER JOIN Employee_Table e ON a.Assigned_To_ID = e.Emp_ID
GROUP BY e.Emp_ID

#3


2  

I may be missing some nuance, but it sounds like you can just: join the tables, group by employee, COUNT and AVG for the load.

我可能会错过一些细微差别,但听起来你可以这样:加入表格,按员工分组,COUNT和AVG加载。

#4


0  

As Dmitri said, something like

正如德米特里所说的那样

SELECT Employee_Table.F_Name, COUNT(*) AS activities, AVG(Percent_Complete) AS load
FROM ATT_Table JOIN Employee_Table ON ATT_Table.Assigned_to_ID = Employee_Table.Emp_ID
WHERE Employee_Table.Emp_ID = 42
GROUP BY Employee_Table.Emp_ID