我想根据条件条件写一个“选择条款”!
我有错误:
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”声明。
答案 0 :(得分: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))