SQL Server +比较输出结果

时间:2013-07-23 02:37:13

标签: sql-server

我在尝试比较某些结果时遇到了一些困难,基本上是

  

(SELECT [DishId] FROM [Package_Set_Dish] WHERE [Package_Set_Id] =   COALESCE((SELECT [SetId] FROM [Package_Schedule_Set_Daily] WHERE   [AutoId] = @PkSchSetId),(SELECT [SetId] FROM   [Package_Schedule_Set_Weekly] WHERE [AutoId] = @PkSchSetId)

将返回3条记录(不按顺序):

1
2
10

1
10
2

基本上,如果传入的sproc变量@ Dish_1,@ Dish_2,@ Dish_3恰好包含1,2和10,我将返回1(True)。如果它包含1,2,2或1,10,10则应返回0.

显然,下面的查询不会返回所需的结果。谁能建议。感谢。

SET @TempBit = (SELECT CASE WHEN 
          (@Dish_1 IN (SELECT [DishId] FROM [Package_Set_Dish] WHERE [Package_Set_Id] 
     = COALESCE((SELECT [SetId] FROM [Package_Schedule_Set_Daily] 
         WHERE [AutoId] = @PkSchSetId),
       (SELECT [SetId] FROM [Package_Schedule_Set_Weekly] WHERE [AutoId] = @PkSchSetId))) 
       AND @Dish_2 IN (SELECT [DishId] FROM [Package_Set_Dish] WHERE [Package_Set_Id] 
     = COALESCE((SELECT [SetId] FROM [Package_Schedule_Set_Daily] 
      WHERE [AutoId] = @PkSchSetId)
     , (SELECT [SetId] FROM [Package_Schedule_Set_Weekly] WHERE [AutoId] = @PkSchSetId)))
      AND @Dish_3 IN (SELECT [DishId] FROM [Package_Set_Dish] WHERE [Package_Set_Id] 
          = COALESCE((SELECT [SetId] FROM [Package_Schedule_Set_Daily]
       WHERE [AutoId] = @PkSchSetId)
     , (SELECT [SetId] FROM [Package_Schedule_Set_Weekly] WHERE [AutoId] = @PkSchSetId)))                                    
AND (SELECT [Date] FROM [Customer_Order_SwapSetDish] 
         WHERE [CustOrderId] = @CustOrderId) = @Date) THEN 1 ELSE 0 END AS BIT)

2 个答案:

答案 0 :(得分:0)

这是您的查询:

SELECT [DishId]
FROM [Package_Set_Dish]
WHERE [Package_Set_Id] = COALESCE((SELECT [SetId]
                                   FROM [Package_Schedule_Set_Daily]
                                   WHERE [AutoId] = @PkSchSetId
                                  ),
                                  (SELECT [SetId]
                                   FROM [Package_Schedule_Set_Weekly]
                                   WHERE [AutoId] = @PkSchSetId
                                  ))

您可以使用having子句进行测试

SELECT count(*)
FROM [Package_Set_Dish]
WHERE [Package_Set_Id] = COALESCE((SELECT [SetId]
                                   FROM [Package_Schedule_Set_Daily]
                                   WHERE [AutoId] = @PkSchSetId
                                  ),
                                  (SELECT [SetId]
                                   FROM [Package_Schedule_Set_Weekly]
                                   WHERE [AutoId] = @PkSchSetId
                                  ))
having sum(case when DishId = @DishId1 then 1 else 0 end) > 0 and
       sum(case when DishId = @DishId2 then 1 else 0 end) > 0 and
       sum(case when DishId = @DishId3 then 1 else 0 end) > 0;

如果所有三个记录都匹配,则查询将返回1.如果不匹配,则返回NULL

或者,您可以在select

中执行此操作
SELECT (case when sum(case when DishId = @DishId1 then 1 else 0 end) > 0 and
                  sum(case when DishId = @DishId2 then 1 else 0 end) > 0 and
                  sum(case when DishId = @DishId3 then 1 else 0 end) > 0
             then 1 else 0
        end) as AreAllThere
FROM [Package_Set_Dish]
WHERE [Package_Set_Id] = COALESCE((SELECT [SetId]
                                   FROM [Package_Schedule_Set_Daily]
                                   WHERE [AutoId] = @PkSchSetId
                                  ),
                                  (SELECT [SetId]
                                   FROM [Package_Schedule_Set_Weekly]
                                   WHERE [AutoId] = @PkSchSetId
                                  ))

答案 1 :(得分:0)

如果我正确理解您的问题,您希望将查询结果与一组三个变量的值进行比较。您可以使用exceptunion语句来实现对称差异:

select case when count(*)=0 then 1 else 0 end
from
(
  (
    (SELECT [DishId] as col FROM [Package_Set_Dish] WHERE [Package_Set_Id] = COALESCE((SELECT [SetId] FROM [Package_Schedule_Set_Daily] WHERE [AutoId] = @PkSchSetId), (SELECT [SetId] FROM [Package_Schedule_Set_Weekly] WHERE [AutoId] = @PkSchSetId)))
    except
    (select @Dish_1 as col union @Dish_2 as col union @Dish_3 as col)
  )
  union
  (
    (select @Dish_1 as col union @Dish_2 as col union @Dish_3 as col)   
    except
    (SELECT [DishId] as col FROM [Package_Set_Dish] WHERE [Package_Set_Id] = COALESCE((SELECT [SetId] FROM [Package_Schedule_Set_Daily] WHERE [AutoId] = @PkSchSetId), (SELECT [SetId] FROM [Package_Schedule_Set_Weekly] WHERE [AutoId] = @PkSchSetId)))
  )
) t;