存储过程中的UNION COUNT

时间:2011-06-08 15:39:03

标签: stored-procedures

我正在创建一个存储过程,它将根据输入的时间返回打开案例的结果 - 我想返回案例的计数但是基于不同的办公室。在编辑我的代码后,我也会重新陈述我的问题 - 哪种方式最好返回计数而不是我现在得到的实际结果。

以下是我的存储过程:

SELECT C.CaseId
FROM [Case] C
WHERE C.DateCreated <= @BeginDate
    AND C.CaseId NOT IN (SELECT CaseId FROM CaseStatusChange CSC WHERE CSC.DateClosed < @BeginDate)

UNION

-- Also need the cases that reopened and are currently open
SELECT ReOpened.CaseId FROM
(
                SELECT C.CaseId, MAX(CSC.DateReopened) AS DateReOpened 
                FROM [Case] C 
                INNER JOIN [CaseStatusChange] CSC ON C.CaseId = CSC.CaseId
                WHERE CSC.DateReopened <= @BeginDate
                GROUP BY C.CaseId
) ReOpened 
WHERE ReOpened.CaseId NOT IN 
(
                SELECT CaseId FROM CaseStatusChange 
                WHERE CaseId = ReOpened.CaseId AND 
                CaseStatusChange.DateClosed BETWEEN ReOpened.DateReopened AND @BeginDate
)

2 个答案:

答案 0 :(得分:2)

这意味着SELECT列表中的列数必须匹配。还有他们的数据类型。

像这样:

select
   col1, col2, col3
from
   t1
union
select
   col1, col2, col3
from
   t2

在您的特定情况下,您需要在第二个SELECT列表中添加OfficeID和计数。

答案 1 :(得分:1)

更新:按要求包含OfficeId:

SELECT  OfficeId, SUM(CaseCount)
FROM    (
            SELECT  COUNT(C.CaseId) AS CaseCount,
                    C.OfficeId
            FROM    [Case] C
            WHERE   C.DateCreated <= @BeginDate
                AND C.CaseId NOT IN (SELECT CaseId FROM CaseStatusChange CSC WHERE CSC.DateClosed < @BeginDate)
            GROUP BY C.OfficeId

            UNION ALL

            -- Also need the cases that reopened and are currently open
            SELECT  COUNT(ReOpened.CaseId) As CaseCount,
                    ReOpened.OfficeID
            FROM
                (
                    SELECT C.CaseId, MAX(CSC.DateReopened) AS DateReOpened, C.OfficeId 
                    FROM [Case] C 
                    INNER JOIN [CaseStatusChange] CSC ON C.CaseId = CSC.CaseId
                    WHERE CSC.DateReopened <= @BeginDate
                    GROUP BY C.CaseId, C.OfficeID
                ) ReOpened 
            WHERE ReOpened.CaseId NOT IN 
                    (
                        SELECT CaseId FROM CaseStatusChange 
                        WHERE CaseId = ReOpened.CaseId AND 
                        CaseStatusChange.DateClosed BETWEEN ReOpened.DateReopened AND @BeginDate
                    )
            GROUP BY OfficeID
        ) AS OpenCasesCount
GROUP BY OfficeId

您需要将整个Query包装在内部Query中,如下所示:

SELECT  SUM(CaseCount)
FROM    (
            SELECT  COUNT(C.CaseId) AS CaseCount
            FROM    [Case] C
            WHERE   C.DateCreated <= @BeginDate
                AND C.CaseId NOT IN (SELECT CaseId FROM CaseStatusChange CSC WHERE CSC.DateClosed < @BeginDate)

            UNION ALL

            -- Also need the cases that reopened and are currently open
            SELECT  COUNT(ReOpened.CaseId) As CaseCount 
            FROM
                (
                    SELECT C.CaseId, MAX(CSC.DateReopened) AS DateReOpened 
                    FROM [Case] C 
                    INNER JOIN [CaseStatusChange] CSC ON C.CaseId = CSC.CaseId
                    WHERE CSC.DateReopened <= @BeginDate
                    GROUP BY C.CaseId
                ) ReOpened 
            WHERE ReOpened.CaseId NOT IN 
                    (
                        SELECT CaseId FROM CaseStatusChange 
                        WHERE CaseId = ReOpened.CaseId AND 
                        CaseStatusChange.DateClosed BETWEEN ReOpened.DateReopened AND @BeginDate
                    )
        ) AS OpenCasesCount