此MySQL查询缺少哪些边缘/极端情况?

What edge/corner case am I missing with this MySQL query?

提问人:SaucyPandora 提问时间:10/24/2023 最后编辑:SaucyPandora 更新时间:10/24/2023 访问量:22

问:

今天早些时候,这个 leetcode sql 问题给我带来了问题。问题如下:

Table: Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id is the column with unique values for this table.
This table contains information about the temperature on a certain day.

 

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

我现在已经使用 leftjoin 和一些 where 子句解决了它,但我最初的方法是:

SELECT w.id As id
FROM 
(
  SELECT id, recordDate, temperature, LAG(temperature) OVER() as prev_temp, recordDate - LAG(recordDate) OVER() as day_diff
  FROM Weather
  ORDER BY recordDate ASC
) w
WHERE w.temperature > w.prev_temp AND day_diff = 1

这通过了除最终测试用例之外的所有测试用例(太大而无法在此处发布,请参阅链接)

这是我的最终解决方案:

Select w1.id as id
FROM Weather AS w1
LEFT JOIN Weather w2 ON DATEDIFF(w1.recordDate, w2.recordDate) = 1
WHERE w1.temperature > w2.temperature 

谁能确定为什么我的初始解决方案没有通过? 此外,如果有人有调试 SQL 查询的过程,我将不胜感激

mysql 日期 联接 sql-order-by

评论

0赞 SelVazi 10/24/2023
如果您给出的 URL 已损坏,则该问题将毫无价值!请编辑您的问题并添加最小、可重现的示例
0赞 SaucyPandora 10/24/2023
@SelVazi 对不起,链接已修复。我会提供一个最小的示例,但我无法弄清楚测试用例失败的地方,希望 URL 就足够了。

答: 暂无答案