Search code examples
sqlcasetemp

Referencing a case statement from outside a Temp Table


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

To simplify what I've put, I've created a temporary table which lists out cases on a database. I've applied a case statement which then categorizes these cases into 3 types, either 1,2 or 3.

In the select statement below that I would like to have another column which shows the percentage of each of those categories from the total. My problem is that I can't use the alias "FPCategory" from the earlier case statement as it hasn't occurred yet for a valid column name to be used in the later statement.

Thanks!


Solution

  • First things first, let's do away with those nasty sub-queries & convert your query to a CTE (untested as i cant see your schema):

    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] 
    

    Now, i noticed you where grouping on FPCategory in your last query wich would imply your results would have multiple casCaseID's per FPCategory? I'm not 100% sure on what you want to average? but now it is a CTE you can rejoin EventCounts & ClientToCaseCounts onto the final query (recursion). Hope this points you in the right direction, if not, please provide some example schema & data examples & i'll tweak my answer accordingly.