2013-07-23 61 views
0

我有一定的困難,在嘗試比較了一定的成效,基本的SQL Server +對輸出進行比較的結果

(選擇[DishId] FROM [Package_Set_Dish],其中[Package_Set_Id] = COALESCE((SELECT [SETID ] FROM [Package_Schedule_Set_Daily] WHERE [自動識別] = @PkSchSetId),(SELECT [SETID] FROM [Package_Schedule_Set_Weekly] WHERE [自動識別] = @PkSchSetId)

將返回3條記錄(不按順序):

1 
2 
10 

1 
10 
2 

基本上,如果傳入SPROC變量@ 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) 

回答

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 
           )) 
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;