2010-12-11 74 views
0

Possible Duplicate:
WHERE Something IN (CASE WHEN statement) ??!!哪裏有東西(CASE WHEN語句)?

我想ro寫一個「select-where」cindase with cinditonal condition。
我所用 「的情況下,當」 中 「其中ID IN」 的條款,但我有以下錯誤:

Msg 512, Level 16, State 1, Line 1 
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 

這裏是我的簡化代碼:

SELECT 
    UnitsAllocation.UnitID, 
    OrganizationUnits.Title AS UnitTitle, 
    'Title' AS ExpenseTitle1, 
    SUM(UnitsAllocationDetails1.ExpenseAmount1) AS ExpenseAmount1 
FROM 
    [bdg_UnitsAllocation] UnitsAllocation LEFT OUTER JOIN 
(SELECT 
      UnitsAllocationDetails.UnitsAllocationID, 
      SUM(UnitsAllocationDetails.Amount)/1 AS ExpenseAmount1 
    FROM [bdg_UnitsAllocationDetails] UnitsAllocationDetails 
    WHERE UnitsAllocationDetails.ExpenseID IN (CASE 1 
                WHEN 1 THEN (SELECT Id FROM bdg_Expenses WHERE ParentId = 1) 
                ELSE (SELECT Id FROM bdg_Expenses WHERE Id = 1) 
               END) 

    GROUP BY UnitsAllocationDetails.UnitsAllocationID) UnitsAllocationDetails1 ON UnitsAllocationDetails1.UnitsAllocationID = UnitsAllocation.ID LEFT OUTER JOIN 
    [bdg_OrganizationUnits] OrganizationUnits ON UnitsAllocation.UnitID = OrganizationUnits.ID 
GROUP BY UnitsAllocation.UnitID, OrganizationUnits.Title 

請看「WHERE UnitsAllocationDetails.ExpenseID IN ...
我該如何解決這個問題?

+0

你剛剛問了同樣的問題。如果你想改變你的問題,編輯它,不要創建一個新的問題。 – 2010-12-11 09:01:04

+1

不幸在我的地區「XXX」是forbiden pharase!並根據我們的互聯網政策進行過濾。所以我無法打開我的問題查看或編輯,然後我再次問。你在本頁再次重複XXX! – mahdiahmadirad 2010-12-11 09:15:36

+0

rotfl。我編輯了您的原始問題以刪除x。 – 2010-12-11 09:21:40

回答

0

代替

WHERE UnitsAllocationDetails.ExpenseID IN(CASE 1 WHEN 1 THEN(SELECT ID FROM bdg_Expenses WHERE的ParentId = 1) ELSE(SELECT ID FROM bdg_Expenses WHERE ID = 1) END) 使用以下

WHERE UnitsAllocationDetails.ExpenseID IN(SELECT ID FROM bdg_Expenses WHERE的ParentId = 1和l = 1 UNION ALL SELECT ID FROM bdg_Expenses其中id = 1和1 <> 1 )

+0

非常有用的方法 – mahdiahmadirad 2010-12-11 09:56:40