2010-12-11 113 views
1


我想根据条件条件写一个“select子句”! BU我有错误:
WHERE有什么东西(CASE WHEN语句)?

Msg 512, Level 16, State 1, Line 2 
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 

请看 “CASE” 和 “IN” 的语句。

+1

'1 = 1'情况将始终成立。你为什么在那里使用'CASE'?为什么不简单地摆脱它,所以'SELECT ID FROM bdg_Expenses WHERE ParentId = 1'不会是一个表达式? – outis 2010-12-11 08:28:39

+0

我简化了它。 when声明最初是“WHEN @Level = 1 THEN ...” – mahdiahmadirad 2010-12-11 09:25:07

回答

1

为什么使用案例?你不能只是做

where (@Level = 1 and ExpenseId in (select id from bdg_expenses where parentid = 1)) or 
     (@Level <> 1 and ExpenseId in (select id from bdg_expenses where id = 1))