Sql 带有子查询的Case语句

Sql 带有子查询的Case语句,sql,sql-server,tsql,case,Sql,Sql Server,Tsql,Case,我有这个case语句,我的目标是根据变量“equipo”执行不同的查询 我的问题是:这个问题有什么问题?它不断地向我抛出错误: 当子查询未引入EXISTS时,只能在选择列表中指定一个表达式 CASE是一个表达式,而不是一个语句,这正是您遇到的问题:您不能从CASE表达式返回结果,只能返回单个值。当s返回完全相同的值时,您的不同的?但假设您的内部查询实际上是不同的,您可以使用UNION-ALL: select top 100 ReadTime, EquipmentName, ParameterNa

我有这个case语句,我的目标是根据变量“equipo”执行不同的查询

我的问题是:这个问题有什么问题?它不断地向我抛出错误:

当子查询未引入EXISTS时,只能在选择列表中指定一个表达式

CASE
是一个表达式,而不是一个语句,这正是您遇到的问题:您不能从
CASE
表达式返回结果,只能返回单个值。当s返回完全相同的值时,您的不同的
?但假设您的内部查询实际上是不同的,您可以使用
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分钟、天、小时?)。请注意,您知道
之间的
=
似乎您想要的是
IF
语句;SQL Server不支持
Case
Switch
)语句。thx对于您的答案,我需要返回5个不同的查询,具体取决于“camion”的值,因此无法使用“union”解决方案,@DavidMenacho请确保您的问题反映您的实际情况,以避免我们浪费时间!无论客户机如何调用该查询,都能处理不同的结构化数据集吗?
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');
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