[英]Writing an SQL query using Aggregate Functions.
Well i have posted this type of ques earlier but now i need to do it in a different way. 好吧,我早些时候已经发布了这类问题,但是现在我需要以不同的方式来做。 I have a table structure :
我有一个表结构:
ATT_Table : Fields : Act_ID, Assigned_To_ID, Status, Product
Act_ID is the primary key, Assigned_To_ID is referenced to Employee_Table Emp_ID. Act_ID是主键,Assigned_To_ID引用到Employee_Table Emp_ID。 Status can be New, In process, Closed or On hold.
状态可以是“新建”,“处理中”,“关闭”或“保留”。 Product can be any XYZ value.
产品可以是任何XYZ值。
Employee_Table : Fields : Emp_ID, F_Name, Product
Here, Emp_ID is primary key. 在这里,Emp_ID是主键。 Product is same as in ATT_Table And will Contain values In ATT_Table for each Assigned_To_ID same as it Contains For that Emp_ID.
乘积与ATT_Table中的相同,并将在ATT_Table中为每个Assigned_To_ID包含值,与在Emp_ID中包含的值相同。 Its like if Emp_ID is 1 and Product is X,then in ATT_Table also for Assigned_To_ID 1 the Product value will be X. I know its a replica and i can avoid that.
就像如果Emp_ID为1并且乘积为X,那么在ATT_Table中,对于Assigned_To_ID 1,乘积值也将为X。我知道它是一个副本,我可以避免这种情况。
Now what i want to do is. 现在我想做的是。 First I want to find the Total number of employees for a Each product.
首先,我想查找每个产品的员工总数。 Let's say this value be A. Now I want to count the number of Employees to whom a particular activity is assigned grouped by Product and where status is Either New Or In process.
假设此值为A。现在,我要计算按产品分组分配了特定活动的雇员数,并且其状态是“新建”或“进行中”。 Let's say ie B. Like.
比方说B.喜欢。 If employees having F_Name C(Emp_ID = 1), D(Emp_ID = 2), E(Emp_ID = 3), F(Emp_ID = 4) belongs to product X. So my A according to this is 4. Now in ATT_Table Assigned_To_ID are 1 and 3 and there Product is same ie X and status is 1(new) 3(In process).
如果具有F_Name C(Emp_ID = 1),D(Emp_ID = 2),E(Emp_ID = 3),F(Emp_ID = 4)的雇员属于产品X。那么根据我的A是4。现在在ATT_Table Assigned_To_ID中是1和3,乘积相同,即X,状态为1(新)3(处理中)。 For 2 and 4 its closed or on hold.
对于2和4,其关闭或暂停。 Now my B according to this is 2. Finally A/B will give me my 3rd value let's say H. In my query table I want to values of A, B and H. Can u please tell me how i can do this.
现在我的B根据这是2。最后,A / B将给我第三个值,我们说H。在我的查询表中,我想输入A,B和H的值。你能告诉我我该怎么做。 Its a bit tricky and wasn't able to get on this one.
这有点棘手,无法继续进行下去。 Please help.
请帮忙。 Thanks.
谢谢。
Something like this should do it: 这样的事情应该做到:
SELECT g.product,
a,
b,
g.a / g.b h
FROM ( SELECT e.product,
COUNT (DISTINCT assigned_to_id) a,
COUNT (
DISTINCT CASE
WHEN status IN ('new', 'in process')
THEN
assigned_to_id
END)
b
FROM Employee_Table e
LEFT OUTER JOIN
ATT_Table a
ON (e.emp_id = a.assigned_to_id)
GROUP BY e.product) g;
You can drop LEFT OUTER JOIN and use INNER join if you do not want to get products which do not have corresponding records in ATT_Table 如果您不想获得ATT_Table中没有相应记录的产品,则可以删除LEFT OUTER JOIN并使用INNER join
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.