2012-02-07 207 views
2

我有一个SQL-Sever查询,该查询返回任何特定日期某个人可用的容量。 结果将针对该人在该特定日期计划的每项任务返回。将查询结果除以另一个查询的结果

我的问题是,它返回查询结果的每一行当天的总容量。 因此,当您为一个人显示一天的所有结果时,在Excel中的链接数据透视表中(例如),您将获得记录数乘以基本容量。

我写了另一个查询,每个人每天的任务数量。

我想将容量从第一个查询中除以第二个查询返回的记录数,但不知道如何将查询连接在一起。

这是白天容量返回任务列表中查询:

SELECT 
    MSP_EpmAssignmentByDay.TimeByDay, 
    MSP_EpmAssignment.ResourceUID, 
    MSP_EpmAssignmentByDay.AssignmentActualWork, 
    MSP_EpmResourceByDay_UserView.Capacity 
FROM 
    MSP_EpmAssignment MSP_EpmAssignment,  
    MSP_EpmAssignmentByDay MSP_EpmAssignmentByDay, 
    MSP_EpmResourceByDay_UserView 
    MSP_EpmResourceByDay_UserView 
WHERE 
    MSP_EpmAssignmentByDay.AssignmentUID = MSP_EpmAssignment.AssignmentUID 
    AND MSP_EpmResourceByDay_UserView.TimeByDay = MSP_EpmAssignmentByDay.TimeByDay 

这是返回每人每天的任务数量的查询:

SELECT 
    count(TimeByDay) as DayCount 
FROM 
    MSP_EpmAssignmentByDay_UserView 
    LEFT JOIN MSP_EpmAssignment_UserView 
    ON MSP_EpmAssignmentByDay_UserView.AssignmentUID = 
    MSP_EpmAssignment_UserView.AssignmentUID 
GROUP BY ResourceUID, TimeByDay 

任何帮助将不胜感激。

感谢

+0

请格式化你的代码:选中它,然后按-1K-。 TIA。 – wildplasser 2012-02-07 12:29:36

+0

您正在使用哪种SQL(Oracle,SQLServer,MySQL等)? – 2012-02-07 12:48:26

+0

答案可以根据数据库而有所不同,我们需要知道您正在使用哪个RDBMS。 – 2012-02-07 14:05:13

回答

1

假设你唯一需要的领域是那些包含在查询中,请尝试:

SELECT 
    MSP_EpmAssignmentByDay.TimeByDay, 
    MSP_EpmAssignment.ResourceUID, 
    SUM(MSP_EpmAssignmentByDay.AssignmentActualWork) TotalWork, 
    MAX(MSP_EpmResourceByDay_UserView.Capacity) Capacity 
FROM 
    MSP_EpmAssignment,  
    MSP_EpmAssignmentByDay, 
    MSP_EpmResourceByDay_UserView 
WHERE 
    MSP_EpmAssignmentByDay.AssignmentUID = MSP_EpmAssignment.AssignmentUID 
    AND MSP_EpmResourceByDay_UserView.TimeByDay = MSP_EpmAssignmentByDay.TimeByDay 
GROUP BY 
    MSP_EpmAssignmentByDay.TimeByDay, 
    MSP_EpmAssignment.ResourceUID 

或者,尝试改变对容量的汇总操作数据透视表是MAX,而不是SUM 。

编辑:包括在细节同样ResourceUID和TimeByDay记录的计数,请尝试:

SELECT 
    MSP_EpmAssignmentByDay.TimeByDay, 
    MSP_EpmAssignment.ResourceUID, 
    MSP_EpmAssignmentByDay.AssignmentActualWork, 
    MSP_EpmResourceByDay_UserView.Capacity, 
    count(*) over (partition by MSP_EpmAssignmentByDay.TimeByDay, 
           MSP_EpmAssignment.ResourceUID) DayCount 
FROM 
    MSP_EpmAssignment MSP_EpmAssignment,  
    MSP_EpmAssignmentByDay MSP_EpmAssignmentByDay, 
    MSP_EpmResourceByDay_UserView 
    MSP_EpmResourceByDay_UserView 
WHERE 
    MSP_EpmAssignmentByDay.AssignmentUID = MSP_EpmAssignment.AssignmentUID 
    AND MSP_EpmResourceByDay_UserView.TimeByDay = MSP_EpmAssignmentByDay.TimeByDay 
+0

该查询实际上比我发布的代码片段要广泛得多,但如果在此级别解决,则可以轻松应用于整个查询。 我使用数据透视表作为说明性示例,但数据本身需要以其他目的进行结构化,因此应用MAX函数将不起作用。 容量必须针对每个单独的赋值行显示为(容量/该用户每天的赋值数量) 我无法将SUM应用于MSP_EpmAssignmentByDay.AssignmentActualWork字段,因为在主查询中我单独需要这些行。 – VxD021 2012-02-07 14:18:30

+0

@ VxD021:然后你需要告诉我们你正在使用哪个数据库。 – 2012-02-07 14:23:50

+0

这是一个基于SQL-Server 2008的数据库。它的Microsoft Project Server 2010报告数据库 – VxD021 2012-02-07 15:06:29

相关问题