合并两个存储过程

时间:2011-06-10 15:47:53

标签: sql-server stored-procedures

所以这就是我想要做的,但现在我得到的错误是:不能在GROUP BY子句列表中使用的表达式中使用聚合或子查询,并且不确定它意味着哪个部分 - 总体代码我试图根据两个不同的级别获取打开的案例,一个是根据传入的日期范围返回案例,另一个是基于开始日期和之前返回案例。

帮助会很棒! :)

代码:

    SELECT
       C.CaseNumber,
       O.OfficeName,
       CT.Description AS CaseType,
       DATEADD(dd, 0, DATEDIFF(dd, 0, C.DateOpened)) AS DateOpened,
       CR.Description AS Court,
       CaseOfficeAppointment.OpenCases,
       CaseOfficeAppointment.CloseCases
FROM 
(
       SELECT  C.CaseId, O.OfficeId, CRT.CourtId,
        (
                   SELECT COUNT(DISTINCT CD.CaseId)
                   FROM [Case] CD
                    INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
                    --INNER JOIN Court CR ON CD.CourtId = CR.CourtId
                    INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
                    LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
                   --WHERE CR.CourtId = CRT.CourtId
                   WHERE OD.OfficeId = O.OfficeId
                   AND
                   ( CD.DateOpened BETWEEN @BeginDate AND @EndDate 
                        OR
                     CSC.DateReopened BETWEEN @BeginDate AND @EndDate
                    ) 
        )AS OpenCases,
        (
                   SELECT COUNT(DISTINCT CD.CaseId)
                   FROM [Case] CD
                    INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
                    --INNER JOIN Court CR ON CD.CourtId = CR.CourtId
                    INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
                    LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
                   --WHERE CR.CourtId = CRT.CourtId
                   WHERE OD.OfficeId = O.OfficeId
                   AND
                   ( CSC.DateClosed BETWEEN @BeginDate AND @EndDate 
                    ) 
        )AS CloseCases
    FROM  [Case] C
        INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
        INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
        INNER JOIN [Court] CRT ON C.CourtId = CRT.CourtId


    WHERE 
           -- Case was open (or reopened) during the date range 
           C.DateOpened BETWEEN @beginDate AND @endDate 
           OR 
           C.CaseId IN (SELECT CaseId FROM CaseStatusChange WHERE DateReopened BETWEEN @beginDate AND @endDate)
           AND 
           -- Office had an appointment sometime during the date range
           A.DateOn < @endDate AND (A.DateOff IS NULL OR A.DateOff BETWEEN @beginDate AND @endDate)

           GROUP BY C.CaseId, O.OfficeId, CRT.CourtId,
        (
                SELECT  OfficeId, SUM(CaseCount)AS Counts
                FROM    (
                            SELECT  COUNT(C.CaseId) AS CaseCount,O.OfficeId
                            FROM    [Case] C
                                INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
                                INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
                            WHERE   C.DateCreated <= @BeginDate
                                AND C.CaseId NOT IN (SELECT CaseId FROM CaseStatusChange CSC WHERE CSC.DateClosed < @BeginDate)
                            --GROUP BY O.OfficeId

                            UNION  

                            -- 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, O.OfficeId 
                                    FROM [Case] C 
                                    INNER JOIN [CaseStatusChange] CSC ON C.CaseId = CSC.CaseId
                                    INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
                                    INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
                                    WHERE CSC.DateReopened <= @BeginDate
                                    --GROUP BY C.CaseId, O.OfficeID
                                ) AS ReOpened 
                            WHERE ReOpened.CaseId NOT IN 
                                (
                                    SELECT CaseId FROM CaseStatusChange 
                                    WHERE CaseId = ReOpened.CaseId AND 
                                    CaseStatusChange.DateClosed BETWEEN ReOpened.DateReopened AND @BeginDate
                                )
                            GROUP BY ReOpened.OfficeId
                        ) AS OpenCasesCount
                GROUP BY OfficeId
        )
)
CaseOfficeAppointment
INNER JOIN [Case] C ON CaseOfficeAppointment.CaseId = C.CaseId
INNER JOIN [Office] O ON CaseOfficeAppointment.OfficeId = O.OfficeId
INNER JOIN [CaseType] CT ON C.CaseTypeId = CT.CaseTypeId
INNER JOIN [Court] CR ON C.CourtId = CR.CourtId

2 个答案:

答案 0 :(得分:0)

如果您调整标题以添加所需的所有参数,则可以根据需要向存储过程添加任意数量的结果集。根据您用于使用数据的语言和平台,使用多个表会有所不同,但只是从一个sproc到另一个sproc的结果集应该可以正常工作,除非底层数据存储(数据库服务器?)有限制。

增加: 根据您的响应,您可以将两个结果集合并到一个存储过程中。要使用它,您有多种选择。使用Reader,您可以转到下一个结果集,因为它是一个firehose游标。但是,使用像DataSet这样的东西并从存储过程生成它可能更容易。然后,您可以使用表适配器来填充存储过程中的数据集。现在只需一次通话即可填写这两个表格。

这有意义吗?

答案 1 :(得分:0)

如果我理解你的话,你需要这样的东西:

CREATE PROCEDURE new_proc
AS
BEGIN
     DECLARE @tmp_proc1 TABLE (// list all fields your first procedure returns );
     DECLARE @tmp_proc2 TABLE (// list fields that your second SP returns);
     INSERT INTO @tmp_proc1 
      EXECUTE Your_First_Procedure ;
     INSERT INTO @tmp_proc2
      EXECUTE Your_Second_Procedure;

     // Finally, join data in @tmp_proc1 and @tmp_proc2 
     //(you probably need FULL JOIN) and return 1 resultset

END;