WHERE Something IN (CASE WHEN statement)?(WHERE 东西在(CASE WHEN 语句)?)
本文介绍了WHERE 东西在(CASE WHEN 语句)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据条件写一个select子句"!但我有错误:
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
请看CASE"和IN"语句.
推荐答案
为什么要使用案例?你不能就这样
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))
这篇关于WHERE 东西在(CASE WHEN 语句)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:WHERE 东西在(CASE WHEN 语句)?


基础教程推荐
猜你喜欢
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01