0

我在尝试比较一些结果时有点困难,基本上

(从 [Package_Set_Dish] 选择 [DishId] WHERE [Package_Set_Id] = COALESCE((从 [Package_Schedule_Set_Daily] 选择 [SetId] WHERE [AutoId] = @PkSchSetId),(从 [Package_Schedule_Set_Weekly] 选择 [SetId] WHERE [AutoId] = @PkSchSetId )

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

1
2
10

或者

1
10
2

基本上,如果传入的存储过程变量@Dish_1、@Dish_2、@Dish_3 正好包含 1,2 和 10,我将返回 1(真)。如果它包含 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)
4

2 回答 2

0

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

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;
于 2013-07-23T03:14:59.967 回答
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
                                  ))
于 2013-07-23T02:47:53.690 回答