2012-02-21 109 views
1
declare @Temp table 
(CaseID int, 
PrimaryClientID int, 
Category int 
) 



INSERT INTO @Temp 

SELECT 
     [casCaseID], 
     [casPrimaryClientID], 


    CASE 
     WHEN (SELECT COUNT(evecaseid) FROM MPS3.dbo.Events where evecaseid=cascaseid and eveworkflowid=960)>0 THEN 1 
     WHEN (SELECT COUNT(CTC2.ctcCaseID)FROM MPS3.dbo.ClientToCase CTC1 INNER JOIN MPS3.dbo.ClientToCase CTC2 ON CTC1.ctcClientID=CTC2.ctcClientID INNER JOIN MPS3.dbo.Events E2 ON CTC2.ctcCaseID=E2.eveCaseID AND E2.eveWorkflowID=960 WHERE CTC1.ctcCaseID=casCaseID AND CTC2.ctcCaseID < CTC1.ctcCaseID)>0 THEN 2 
     ELSE 3 
    END AS [FPCategory] 



FROM [MPS3].[dbo].[Cases] 

    WHERE 

    casRecontactDate BETWEEN '2012-01-01' AND '2012-01-01' 
    AND 
    casCaseType = 'm' 

ORDER BY FPCategory 

SELECT 
Category AS [CategoryType], 
COUNT(Category) AS [CategoryTotal] 

CASE 

WHEN [Category] = 0 or [Category] = 0 THEN 0 

else ([Category]+0.0)/COUNT[Category]) 

END AS [Percentage], 


FROM 
@Temp 

GROUP BY 

Category 

引用一個case語句來簡化我已經把,我創建了一個臨時表,其中列出了一個數據庫的情況。我已經申請了一個案例陳述,然後將這些案例分爲3種類型,1,2或3。從臨時表外

在下面的select語句中,我希望有另一列顯示每個類別的百分比總數。 我的問題是,我不能使用前面的case語句中的別名「FPCategory」,因爲它還沒有出現在後面的語句中使用有效的列名稱。

謝謝!第一

+0

歡迎使用StackOverflow:如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)格式和語法突出顯示它! – 2012-02-21 10:44:36

+1

謝謝Marc_s&Arion,我會確保我下次再做。 – TJH 2012-02-21 10:48:37

回答

0

第一件事,讓我們與那些討厭的子查詢做掉&查詢轉換爲CTE(未經測試,因爲我看不到你的架構):

WITH EventCounts (casCaseID, EventCount) 
      AS (SELECT evecaseid , 
         COUNT(evecaseid) 
       FROM  MPS3.dbo.Events 
       WHERE eveworkflowid = 960 
      ), 
     ClientToCaseCounts (casCaseID, ClientToCaseCount) 
      AS (SELECT CTC1.ctcCaseID , 
         COUNT(CTC2.ctcCaseID) 
       FROM  MPS3.dbo.ClientToCase CTC1 
         INNER JOIN MPS3.dbo.ClientToCase CTC2 ON CTC1.ctcClientID = CTC2.ctcClientID 
         INNER JOIN MPS3.dbo.Events E2 ON CTC2.ctcCaseID = E2.eveCaseID 
                 AND E2.eveWorkflowID = 960 
       WHERE CTC2.ctcCaseID < CTC1.ctcCaseID 
      ), 
     BaseData ([casCaseID], [casPrimaryClientID], [FPCategory]) 
      AS (SELECT [casCaseID] , 
         [casPrimaryClientID] , 
         CASE WHEN EventCounts.EventCount > 0 THEN 1 
          WHEN ClientToCaseCounts.ClientToCaseCount > 0 THEN 2 
          ELSE 3 
         END AS [FPCategory] 
       FROM  [MPS3].[dbo].[Cases] C 
         LEFT OUTER JOIN EventCounts ON C.[casCaseID] = EventCounts.casCaseID 
         LEFT OUTER JOIN ClientToCaseCounts ON C.[casCaseID] = ClientToCaseCounts.casCaseID 
       WHERE casRecontactDate BETWEEN '2012-01-01' 
             AND  '2012-01-01' 
         AND casCaseType = 'm' 
      ) 
    SELECT [FPCategory] , 
      COUNT([casCaseID]) AS TotalCases 
    FROM BaseData 
    GROUP BY [FPCategory] 

現在,我注意到你在哪裏上FPCategory分組在你最後的查詢中,這意味着你的結果會有多個casCaseID的每個FPCategory?我不是100%確定你想要的平均水平?但現在它是一個CTE,您可以將EventCounts & ClientToCaseCounts重新加入最終查詢(遞歸)。希望這點指向正確的方向,如果沒有,請提供一些示例架構&數據示例&我會相應地調整我的答案。

+0

嗨HeavenCore,這是完美的。感謝您的關注,我認識到用新鮮的眼睛來看待它一定很難! 我已經設法讓腳本完美地工作! – TJH 2012-02-21 14:58:12