简体   繁体   English

SQL 查询返回不正确的值

[英]SQL query returning incorrect value

I have a MariaDB weather database that is updated every 15 minutes with current conditions.我有一个 MariaDB 天气数据库,该数据库每 15 分钟根据当前条件更新一次。 I am trying to query it to return the maximum temperature for a specific month.我正在尝试查询它以返回特定月份的最高温度。 When I query it with "Select temp,datetime from table" I get a listing of all temps and the time as recorded.当我使用“从表中选择临时,日期时间”查询它时,我会得到所有临时时间和记录时间的列表。 I may get 3-4 records of the same temp with 15min time intervals over an hour or so.我可能会在一个小时左右的时间内以 15 分钟的时间间隔获得 3-4 条相同温度的记录。 To get a specific month, I tried "SELECT temp,logdatetime FROM Monthly WHERE logdatetime like '2020-01%' ORDER BY temp desc" I get what I expect.为了得到一个特定的月份,我尝试了“SELECT temp,logdatetime FROM Monthly WHERE logdatetime like '2020-01%' ORDER BY temp desc”我得到了我的期望。 (Datetime format is YYYY-MM-DD HH:mm:ss) (日期时间格式为 YYYY-MM-DD HH:mm:ss)

temp温度 logDateTime日志日期时间
40.2 40.2 2020-01-04 16:15:00 2020-01-04 16:15:00
40.1 40.1 2020-01-04 15:30:00 2020-01-04 15:30:00
40.1 40.1 2020-01-04 17:00:00 2020-01-04 17:00:00
40.1 40.1 2020-01-04 17:30:00 2020-01-04 17:30:00
40.1 40.1 2020-01-31 16:15:00 2020-01-31 16:15:00
40.0 40.0 2020-01-04 16:45:00 2020-01-04 16:45:00
40.0 40.0 2020-01-31 16:00:00 2020-01-31 16:00:00

If I try "SELECT max(temp),logdatetime FROM Monthly WHERE logdatetime like '2020-01%'", results are;如果我尝试“SELECT max(temp),logdatetime FROM Monthly WHERE logdatetime like '2020-01%'”,结果是;

temp温度 logdatetime日志日期时间
40.2 40.2 2020-01-01 00:00:00 2020-01-01 00:00:00

In this example the date has returned incorrectly as Jan 1 rather than Jan 4, and the time is missing.在此示例中,日期错误地返回为 1 月 1 日而不是 1 月 4 日,并且缺少时间。 There is only one entry for 40.2 so shouldn't it return the time. 40.2 只有一个条目,所以它不应该返回时间。 I understand if there are multiple temps, as for 40.1, there may be a problem returning the four dates.我知道如果有多个临时文件,至于 40.1,返回四个日期可能会出现问题。

I've also tried a nested select statement as below, with the same result.我还尝试了如下嵌套的 select 语句,结果相同。 SELECT max(a.temp), LogDateTime FROM (SELECT temp, logdatetime FROM Monthly WHERE logdatetime like '2020-01%') AS a SELECT max(a.temp), LogDateTime FROM (SELECT temp, logdatetime FROM Monthly WHERE logdatetime like '2020-01%') 作为

Any help greatly appreciated.非常感谢任何帮助。 Peter彼得

Use利用

SELECT *
FROM logdatetime 
WHERE logdatetime BETWEEN '2020-01-01 00:00:00' AND '2020-01-31 23:59:59'
ORDER BY temp DESC
LIMIT 1

This query selects a row with maximal temperature in Jan, 2020.此查询选择 2020 年 1 月温度最高的行。

If there exists a lot of rows with the same maximal temperature value then this query will return one of them (and in general you cannot predict what row from all possible ones will be returned).如果存在许多具有相同最大温度值的行,则此查询将返回其中之一(通常您无法预测将返回所有可能行中的哪一行)。 If you need in some definite (for example, the most last) then you may expand sorting expression (for example, till ORDER BY temp DESC, logdatetime DESC ).如果您需要一些明确的(例如,最后一个),那么您可以扩展排序表达式(例如,直到ORDER BY temp DESC, logdatetime DESC )。

If you need all rows with maximal temperature, then you must firstly get this maximal value in subquery then use it in the query for rows filtering.如果您需要所有具有最高温度的行,那么您必须首先在子查询中获取该最大值,然后在查询中使用它进行行过滤。 Or, in MySQL 8+ version, you may use RANK() function in CTE.或者,在 MySQL 8+ 版本中,您可以在 CTE 中使用 RANK() function。

Akina, That worked great.秋名,效果很好。 Many thanks for your help.非常感谢您的帮助。 Peter彼得

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

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