Aggregating data in two different ways within a single record [duplicate]

﹥>﹥吖頭↗ 提交于 2020-02-07 08:18:51

问题


Possible Duplicate:
Need help for my SQL query

Well I may have posted this earlier also, but my requirements changed a bit, so please help me on this one.

My database structure:

ATT (Activity table)

  • Act_ID(PK)
  • Assigned_To_ID (FK, refer to Emp_ID)
  • Project_ID (FK, refer to Project_ID)
  • Product_ID (FK, refer to Product_ID)
  • Status (can be New, OnHold, Cancel, Closed)

Product_Table

  • Product_ID (PK)
  • Product_Name

Project_Table

  • Project_ID (PK)
  • Project_Name

Employee_Table

  • Emp_ID (PK)
  • F_Name.

Emp_Product_Project_Mapping

(This table basically maps which employee is assigned to which product and which project. Duplicates entry are allowed in all the 3 columns, to map that 1 employee can be assigned to multiple project or products)

  • Emp_IDFK(FK refer to Emp_ID PK)
  • Product_IDFK(FK refer to Product_ID PK)
  • Project_IDFK(FK refer to Project_ID PK)

Constraints

  • In 1 Project --> Multiple employees can work
  • 1 Employee -- > Can be assigned multiple activities (Act_ID)
  • At any given point of time Status can be any of those given values.
  • At any given point an activity can be assigned to 1 product and 1 project.

Now in my SQL query what I want to do is a load check ::

An activity (represented by Act_ID) is assigned to an employee is represented by Assigned_To_ID, for Project (Project_ID in ATT_Table) for product(Product_ID in ATT_table) and a particular status. I need to output 5 values from my SQL query:

  1. Name of Employee
  2. Name of Product
  3. Number of total activities assigned for that Product(in 2) where Status is either New or InProcess.(Here, i am taking count of all the activities that are related to Product (in 2).No matter to whom it is assigned. Its not related to Employee (in 1). That i want in my 4rth part).
  4. Now,i want the count of activities that Employee (in 1) is assigned to(out of total count in i have got from (3)). Hope i am making myself clear. In 3rd it was Total activities assigned to product(in 2). In 4rth its activities out of my result of 3rd, assigned to an employee in 1st.
  5. Load. That will be = (Result in 3)/(result in 4) * 100 ((Result in 3) is a larger value than (result in 4), which is obvious.)

Now There may be a condition That an employee let's say E1 is working on product P1,P2. So my table output will be look like this::

1    2     3    4     5
E1   P1    
E1   P2 

So here 3, 4, 5 will be having values corresponding to activities for that Product(P1 or P2)

Please help me on this one. Using MSaccess database. Thanks..


回答1:


Since you want to "group" your data in two independent ways, you can't do it with a single GROUP BY clause. You must instead count the total number of activities for each product with a subquery; here I'm also using a subquery to count each employee's activities on those products, since I think it's easier to understand than mixing one GROUP BY subquery with a GROUP BY in the parent:

SELECT
  Employee_Table.F_Name,
  Product_Table.Product_Name,
  AllActivities.Count,
  EmployeeActivities.Count,
  EmployeeActivities.Count / AllActivities.Count AS Load
FROM
  Employee_Table, Product_Table
  INNER JOIN (
    SELECT Product_ID, COUNT(*) AS Count
    FROM ATT
    GROUP BY Product_ID
  ) AS AllActivities ON
    AllActivities.Product_ID = Product_Table.Product_ID
  INNER JOIN (
    SELECT Assigned_To_ID, Product_ID, COUNT(*) AS Count
    FROM ATT
    GROUP BY Assigned_To_ID, Product_ID
  ) AS EmployeeActivities ON
        EmployeeActivities.Assigned_To_ID = Employee_Table.Emp_ID
    AND EmployeeActivities.Product_ID     = Product_Table.Product_ID


来源:https://stackoverflow.com/questions/10373043/aggregating-data-in-two-different-ways-within-a-single-record

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!