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」,因爲它還沒有出現在後面的語句中使用有效的列名稱。
謝謝!第一
歡迎使用StackOverflow:如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)格式和語法突出顯示它! – 2012-02-21 10:44:36
謝謝Marc_s&Arion,我會確保我下次再做。 – TJH 2012-02-21 10:48:37