WHERE Something IN(CASE WHEN 语句)?

发布于 2024-10-07 06:47:53 字数 1489 浏览 0 评论 0原文


我想根据条件条件写一个“select子句”! 我有错误:

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”语句。

I want to write a "select clause" according to conditional condition!
bu I have error:

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.

How can I fix it??
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 "CASE" and "IN" statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2024-10-14 06:47:53

为什么要使用案例?你就不能这样做吗

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

why use a case? can't you just do

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文