简体   繁体   English

在SQL查询中使用聚合函数

[英]Using aggregate functions in SQL query

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. 现在,在特定时间点,可以将1个或多个活动分配给同一个人。 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). 例如,如果给人A分配了A1,A2,A3(Act_ID)。 Then corresponding (Percent_Complete values addition)/3. 然后对应(Percent_Complete值加法)/ 3。 Basically an average. 基本上是平均水平。 In my final query result I want: 在我的最终查询结果中,我想要:

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

How do I this? 我该怎么办? Do I have to use a nested WHERE IN clause ? 我是否必须使用嵌套的WHERE IN子句? Thanks. 谢谢。

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

我可能会遗漏一些细微差别,但听起来您可以:联接表,按员工分组, COUNTAVG进行加载。

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

As Dmitri said, something like 正如德米特里(Dmitri)所说,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM