有条件的地方使用Group By时

时间:2016-09-24 15:26:00

标签: sql sql-server tsql group-by sql-server-2014

我有一个工作程序来显示给定时间段的报告。 以下过程查找给定时间段内的所有预订以及每个月组的显示

enter image description here

CREATE PROCEDURE [dbo].[GetSummaryOverviewReport] (
    @StartDate SMALLDATETIME = NULL
    ,@EndDate SMALLDATETIME = NULL
    ,@IncludeVAT BIT = 0
    ,@AddedAfter SMALLDATETIME
    ,@HasObservation BIT = 0
    ,@IncludeCancelledBooking BIT = 0
    )
AS
BEGIN
    SET NOCOUNT ON

    BEGIN
        SELECT YEAR(StartTime) [Year]
            ,MONTH(StartTime) [Month]
            ,COUNT(BookingId) [BookingCount]
            ,SUM(CASE 
                    WHEN IsVAT = 1
                        AND @IncludeVAT = 1
                        THEN (Price / 100) * 80
                    ELSE Price
                    END) AS TotalPrice
            ,PaidCount = sum(CASE 
                    WHEN B.PaymentStatus = 'FullyPaid'
                        THEN 1
                    ELSE 0
                    END)
            ,PaidSum = sum(CASE 
                    WHEN B.PaymentStatus = 'FullyPaid'
                        THEN (
                                CASE 
                                    WHEN IsVAT = 1
                                        AND @IncludeVAT = 1
                                        THEN (Price / 100) * 80
                                    ELSE Price
                                    END
                                )
                    ELSE 0
                    END)
            ,C.CategoryId
            ,CategoryName
        FROM Category c
        LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
        WHERE B.AddedDateTime > @AddedAfter
            AND ISNULL(B.IsCancelled, 0) = 0
            AND (
                (
                    B.StartTime BETWEEN @StartDate
                        AND @EndDate
                    )
                OR (
                    B.StartTime BETWEEN @StartDate
                        AND @EndDate
                    )
                )
            AND (
                @HasObservation = 0
                OR B.PatientID IN (
                    SELECT PatientID
                    FROM PatientXObservation O
                    WHERE ObservationId IN (
                            1
                            ,2
                            ,4
                            )
                    )
                )
        GROUP BY YEAR(StartTime)
            ,MONTH(StartTime)
            ,C.CategoryId
            ,CategoryName
        ORDER BY 1
            ,2
            ,CategoryName
    END
END

现在我们要添加一个过滤器 @IncludeCancelledBooking 参数。因此,用户可以选择是否显示已取消的预订。截至目前,该程序忽略了取消的预订,仅显示未取消的预订..

在分组记录时,我无法找到如何添加此条件。 我需要使用AND ISNULL(B.IsCancelled,0)= 0基于@IncludeCancelledBooking

3 个答案:

答案 0 :(得分:1)

这是你想要的逻辑吗?

(@IncludeCancelled <> 'Y' OR Booking ISNULL(B.IsCancelled, 0) = 0)

答案 1 :(得分:0)

如果IsCancelled也是BIT(如@IncludeCancelled),那么您可以使用:

 AND ISNULL(B.IsCancelled, 0) IN (0, @IncludeCancelled)

如果@IncludeCancelled为0,那么这实际上与您现在的逻辑相同(即= 0)。如果@IncludeCancelled为1,那么这包括IsCancelled = 1。

答案 2 :(得分:0)

假设您为变量@IncludeCancelled传递值1以包含已取消的预订。然后更改WHERE条件,如下所示。

   AND ISNULL(B.IsCancelled,0)= 
        CASE WHEN @IncludeCancelled=1 
                    THEN ISNULL(B.IsCancelled,0)
                     ELSE 0 END