then 中带有子查询的 Case 语句
Case statement with subquery in then
我有这个case语句,我的目标是根据变量“equipo”执行不同的查询
declare @equipo nvarchar(30)
set @equipo='CA10'
SELECT CASE
when @equipo in ('CA10','CA11') then
(select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment=@equipo AND readtime between GETDATE()-15 AND GETDATE())
when @equipo='CA62' then
(select top 100 a.ReadTime, a.EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment=@equipo AND readtime between GETDATE()-15 AND GETDATE())
else 'nothing'
end
我的问题是:这个查询有什么问题?它一直向我抛出错误:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
CASE
是一个 表达式 而不是 语句 - 这正是你 运行 犯规的: 你不能 return 来自 case
表达式的结果,只有一个值。你不同的 when
和 return 的值是完全一样的吗?但假设您的内部查询实际上不同,您可以使用 UNION ALL
:
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate()
and @equipo in ('CA10','CA11')
union all
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate()
and @equipo in ('CA62');
注意:您应该真正使用 dateadd()
而不是 getdate()-15
- 以确保正确的结果(是 15 分钟、几天、几小时?)。请注意,您知道 between
是 >=
和 <=
,因此包含两个边界。这可能会产生意想不到的结果,尤其是考虑到时间因素。
编辑:由于您已经阐明了您的要求,因此您可以使用 IF
语句(如 Larnu 建议的那样),例如
if @equipo in ('CA10','CA11') begin
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate();
end; else if @equipo in ('CA62') begin
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate();
end; -- etc
我有这个case语句,我的目标是根据变量“equipo”执行不同的查询
declare @equipo nvarchar(30)
set @equipo='CA10'
SELECT CASE
when @equipo in ('CA10','CA11') then
(select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment=@equipo AND readtime between GETDATE()-15 AND GETDATE())
when @equipo='CA62' then
(select top 100 a.ReadTime, a.EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment=@equipo AND readtime between GETDATE()-15 AND GETDATE())
else 'nothing'
end
我的问题是:这个查询有什么问题?它一直向我抛出错误:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
CASE
是一个 表达式 而不是 语句 - 这正是你 运行 犯规的: 你不能 return 来自 case
表达式的结果,只有一个值。你不同的 when
和 return 的值是完全一样的吗?但假设您的内部查询实际上不同,您可以使用 UNION ALL
:
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate()
and @equipo in ('CA10','CA11')
union all
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate()
and @equipo in ('CA62');
注意:您应该真正使用 dateadd()
而不是 getdate()-15
- 以确保正确的结果(是 15 分钟、几天、几小时?)。请注意,您知道 between
是 >=
和 <=
,因此包含两个边界。这可能会产生意想不到的结果,尤其是考虑到时间因素。
编辑:由于您已经阐明了您的要求,因此您可以使用 IF
语句(如 Larnu 建议的那样),例如
if @equipo in ('CA10','CA11') begin
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate();
end; else if @equipo in ('CA62') begin
select top 100 ReadTime, EquipmentName, ParameterName, ParameterFloatValue
from table
where Equipment = @equipo
and readtime between getdate()-15 and getdate();
end; -- etc