简体   繁体   English

SQL WHERE子句,如JOIN查询

[英]SQL WHERE clause like JOIN query

I have the below procedure which fetches the booking count and total price for each month. 我有以下程序,它取每个月的预订计数和总价。

Now I need to pass additional parameter @HasObservation as an additional parameter. 现在我需要传递额外的参数@HasObservation作为附加参数。 The logic to filter is not just a normal field filter. 过滤的逻辑不仅仅是普通的场过滤器。 It has to be filtered by joining with other table PatientXObservation. 必须通过加入其他表PatientXObservation进行过滤。 If this table has entries for value 1,2,4 then only filter those bookings. 如果此表具有值1,2,4的条目,则仅过滤那些预订。

@HasObservation=1 mean, FETCH RECORDS SATISFIES THIS CONDITION @ HasObservation = 1表示,FETCH记录满足此条件

PatientXObservation O ON B.PatientId = O.PatientId
        AND O.ObservationId IN (1,2,4)

How to add this filter in the below SQL? 如何在下面的SQL中添加此过滤器? I am not sure about how to add this here 我不知道如何在这里添加它

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

I have tried with TEMP table but the query is running very very slow . 我试过TEMP表,但查询运行速度非常慢 Other issue here is if @HasObservation is 0 i need to avoid this filter , but unfortunately it chooses observation=0 :( 这里的其他问题是如果@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

You can use the IN clause to filter only if your @HasObservation parameter is non-zero: 仅当@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

You could try using an IF statement like the following. 您可以尝试使用如下所示的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

Your join should be: 你的加入应该是:

LEFT JOIN PatientXObservation O 
          ON B.PatientId = O.PatientId

Then add the predicate 然后添加谓词

WHERE  (@HasObservation = 1 AND O.ObservationId IN (1,2,4)) OR (@HasObservation = 0 AND O.ObservationId < 0)

Change your WHERE to have the two versions of @HasObservation return what is needed. 更改您的WHERE以使@HasObservation的两个版本返回所需内容。 If I understand your question, then if @HasObservation = 1 then there is a further filter. 如果我理解你的问题,那么如果@HasObservation = 1那么还有一个过滤器。 The SQL would be as follows: 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

Your @HasObservation = 0 could have a different filter if needed; 如果需要,你的@HasObservation = 0可以有不同的过滤器; you would just need to adjust the code to include some exclusionary logic. 您只需要调整代码以包含一些排除逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM