简体   繁体   English

具有多个Where子句的SQL Server查询

[英]SQL Server Query with Multiple Where Clauses

I hope you might be able to help. 我希望你能提供帮助。 I'm trying to write a SQL query which requires 2 where conditions. 我正在尝试编写一个SQL查询,其中需要2个条件。 This is what I've come up with but getting errors: 这是我想出来但却得到错误:

SELECT *
FROM Table
WHERE (Datediff(dd,TimeStampUTC,dateadd(dd,-1,getdate())))
AND (MeterTagId Between 416 AND 462)

Essentially I have a table with data being collected every 15 minutes for a range of tags. 基本上我有一个表,每15分钟收集一系列标签的数据。 What I'm trying to do is return "Previous Days" data for a range of tags. 我要做的是返回一系列标签的“Previous Days”数据。

Any help you can give would be extremely appreciated as my SQL knowledge is still quite limited. 由于我的SQL知识仍然非常有限,因此您可以给予任何帮助。

Thanks in advance. 提前致谢。

You do not have a condition here: 你这里没有条件:

WHERE (Datediff(dd,TimeStampUTC,dateadd(dd,-1,getdate())))

You need to add an = or <= or >= or < or > or between or something that will provide true or false 您需要添加=或<=或> =或<或>或之间或将提供true或false的内容

This seems ok: 这似乎没问题:

AND (MeterTagId Between 416 AND 462)

Please also post your errors. 还请发布您的错误。

If you want to use an index on TimeStampUTC , then you should try to do this with no functions on that column: 如果要在TimeStampUTC上使用索引,那么您应该尝试在该列上没有函数的情况下执行此操作:

SELECT *
FROM Table
WHERE TimeStampUTC between cast(getdate() - 1 as date) and cast(getdate() as date) and
      MeterTagId Between 416 AND 462;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM