2010-12-11 16 views
1


条件に合わせて「select節」を書いてみたい! 府は、私がエラーを持っている:
WHERE何かIN(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」の場合は常にtrueになります。なぜあなたはそこに 'CASE'を使っていますか?単にそれを取り除くだけではなく、 'SELECT ID FROM bdg_Expenses WHERE ParentId = 1'は式にならないのですか? – outis

+0

私はそれを簡略化しました。元のwhenステートメントは "WHEN @Level = 1 THEN ..." – mahdiahmadirad

答えて

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