2
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”,因为在后面的语句中使用的有效列名还没有发生。

谢谢!

4

1 回答 1

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 进行分组意味着您的结果每个 FPCategory 会有多个 casCaseID?我不是 100% 确定你想要平均多少?但现在它是一个 CTE,您可以将 EventCounts 和 ClientToCaseCounts 重新加入到最终查询(递归)中。希望这会为您指明正确的方向,如果没有,请提供一些示例架构和数据示例,我会相应地调整我的答案。

于 2012-02-21T12:45:55.950 回答