我有以下程序来获取每个月的预订计数和总价。
现在我需要传递额外的参数@HasObservation
作为附加参数。过滤的逻辑不仅仅是普通的场过滤器。必须通过加入其他表PatientXObservation进行过滤。如果此表具有值1,2,4的条目,则仅过滤这些预订。
@ HasObservation = 1表示,FETCH记录满足此条件
PatientXObservation O ON B.PatientId = O.PatientId
AND O.ObservationId IN (1,2,4)
如何在下面的SQL中添加此过滤器?我不确定如何在这里添加
CREATE PROCEDURE [dbo].[GetDoctorOperationReportTest]
( @IncludeVAT BIT = 0, @HasObservation BIT = 0 )
AS
BEGIN
SET NOCOUNT ON
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
, C.CategoryId
, CategoryName
FROM Category c
LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
WHERE
C.IncludeReport = 1
GROUP BY YEAR(StartTime), MONTH(StartTime), C.CategoryId, CategoryName
ORDER BY 1, 2, CategoryName
END
我尝试使用TEMP表,但查询运行非常慢。这里的其他问题是如果@HasObservation为0我需要避免这个过滤器,但不幸的是它选择观察= 0 :(
SELECT B.BookingId
,B.StartTime
,B.IsVAT
,B.Price
,c.CategoryId
,c.CategoryName
,c.IncludeReport
,observation = (
CASE
WHEN isnull(o.PatientId, 0) = 0
THEN 0
ELSE 1
END
)
INTO #Temp
FROM Category c
LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
LEFT JOIN PatientXObservation O ON B.PatientId = O.PatientId
AND O.ObservationId IN (1,2,4)
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
,CategoryId
,CategoryName
FROM #Temp
WHERE IncludeReport = 1
AND observation = @HasObservation
GROUP BY YEAR(StartTime)
,MONTH(StartTime)
,CategoryId
,CategoryName
ORDER BY 1
,2
,CategoryName
答案 0 :(得分:3)
只有当@HasObservation参数非零时,才能使用IN子句进行过滤:
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
, C.CategoryId
, CategoryName
FROM Category c
LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
WHERE
C.IncludeReport = 1
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
答案 1 :(得分:0)
您可以尝试使用IF
语句,如下所示。
CREATE PROCEDURE [dbo].[GetDoctorOperationReportTest]
( @IncludeVAT BIT = 0, @HasObservation BIT = 0 )
AS
BEGIN
SET NOCOUNT ON
IF @HasObservation = 1
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
, C.CategoryId
, CategoryName
FROM Category c
LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
LEFT JOIN PatientXObservation O
ON B.PatientId = O.PatientId
AND O.ObservationId IN (1,2,4)
WHERE C.IncludeReport = 1
GROUP BY YEAR(StartTime), MONTH(StartTime), C.CategoryId, CategoryName
ORDER BY 1, 2, CategoryName
END
ELSE
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
, C.CategoryId
, CategoryName
FROM Category c
LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
WHERE
C.IncludeReport = 1
GROUP BY YEAR(StartTime), MONTH(StartTime), C.CategoryId, CategoryName
ORDER BY 1, 2, CategoryName
END
END
答案 2 :(得分:0)
您的加入应该是:
LEFT JOIN PatientXObservation O
ON B.PatientId = O.PatientId
然后添加谓词
WHERE (@HasObservation = 1 AND O.ObservationId IN (1,2,4)) OR (@HasObservation = 0 AND O.ObservationId < 0)
答案 3 :(得分:0)
更改您的WHERE以使@HasObservation的两个版本返回所需内容。如果我理解你的问题,那么如果@HasObservation = 1那么还有一个过滤器。 SQL如下:
CREATE PROCEDURE [dbo].[GetDoctorOperationReportTest]
( @IncludeVAT BIT = 0, @HasObservation BIT = 0 )
AS
BEGIN
SET NOCOUNT ON
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
, C.CategoryId
, CategoryName
FROM Category c
LEFT JOIN Booking B ON C.CategoryId = B.CategoryId
LEFT JOIN PatientXObservation O
ON B.PatientId = O.PatientId
WHERE
C.IncludeReport = 1
and
(
(
@HasObservation = 0
)
or
(
@HasObservation = 1
and O.ObservationId IN (1,2,4)
)
)
GROUP BY YEAR(StartTime), MONTH(StartTime), C.CategoryId, CategoryName
ORDER BY 1, 2, CategoryName
END
如果需要,你的@HasObservation = 0可以有不同的过滤器;你只需要调整代码以包含一些排他性逻辑。