2012-02-07 286 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

相關問題