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
为了简化我所写的内容,我创建了一个临时表,其中列出了数据库上的案例。我应用了一个案例陈述,然后将这些案例分为 3 种类型,1,2 或 3。
在下面的选择语句中,我希望有另一列显示每个类别占总数的百分比。我的问题是我不能使用前面 case 语句中的别名“FPCategory”,因为在后面的语句中使用的有效列名还没有发生。
谢谢!