开发者

WHERE Something IN (CASE WHEN statement)? [duplicate]

This question already has an answer here: Closed 12 years ago.

Possible Duplicate:

WHERE Something IN (CASE WHEN statement) ??!!

i want ro write a "select-where" cluase with cinditonal condition.

i used "case-when" in "where ID IN" clause But I have the following error:

开发者_C百科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.

here is my simplified code:

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

Please Look At "WHERE UnitsAllocationDetails.ExpenseID IN ..."

How can I fix that?


Instead of

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) use the following

WHERE UnitsAllocationDetails.ExpenseID IN (SELECT Id FROM bdg_Expenses WHERE ParentId = 1 and 1 = 1 UNION ALL SELECT Id FROM bdg_Expenses WHERE Id = 1 and 1 <> 1 )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜