LeetCode:. 197 rising temperature

Topic links: https://leetcode-cn.com/problems/rising-temperature/

topic

Given a Weathertable, write a SQL query, to find a higher temperature before Id all dates (yesterday's) date compared.

------------------ ------------------ + --------- + + +
| the above mentioned id (INT) | RecordDate (DATE) | Temperature (INT) |
+ --------- + ------------------ + ----- + -------------
|. 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
|. 3 | 2015-01-03 | 20 is |
|. 4 | 2015-01-04 | 30 |
+ --------- + ------------ + ------------------ + ------
for example, according to the above given tables Weather, returns the following Id:

+----+
| Id |
+----+
| 2 |
| 4 |
+----+

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/rising-temperature
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

answer

The first feeling with oraclethe analysis function, offset function lag, leadbut do not know where not to support window functions, test some say.

---- oracle ----
/* Write your PL/SQL query statement below */
select Id
from
(
    select Id,
           RecordDate,
           Temperature,
           lag(Temperature,1) over(order by RecordDate) as Temperature_2
    from Weather
)
where Temperature > Temperature_2;

Test is not passed, a test case because there is an interval of 1 day, 2 days before and not, so this question can not be solved by shifting the function or have to be connected by two days before and after, if no corresponding not on the associated time difference, so the answer is correct.

In the MySQLenvironment, the use joinand datedifffunction to solve it.

---- MySQL ----
select a.Id as Id
from Weather a
left join Weather b
on datediff(a.RecordDate, b.RecordDate) = 1
where a.Temperature > b.Temperature; ---- 274ms
-- 第一次提交的时候把最后温度的过滤条件写成了and,怪不得提交不通过,改为where之后便可以了。
---- MySQL ----
# Write your MySQL query statement below
select a.Id
from Weather a,
     Weather b
where a.Temperature > b.Temperature
and datediff(a.RecordDate, b.RecordDate) = 1; ---- 275ms

By this way it is? You have to think about. .

---- oracle ----
/* Write your PL/SQL query statement below */
select a.Id as Id
from Weather a
left join Weather b
on a.RecordDate = b.RecordDate - 1
where a.Temperature > b.Temperature; 
---- 没通过

In fact, in itself, a solution like this is no problem, but the data in the test sample is not standard, so the test did not pass.

In addition, by shifting the function really see a kind of answer, and then try something, change it.

---- oracle ----
/* Write your PL/SQL query statement below */
select t.Id as Id
from
(
    select Id,
           RecordDate,
           Temperature,
           lag(Temperature,1) over(order by RecordDate) as Temperature_2,
           lag(RecordDate,1) over(order by RecordDate) as RecordDate_2
    from Weather
) t
where t.Temperature > t.Temperature_2
and round(to_number(t.RecordDate - t.RecordDate_2)) = 1; ---- 537ms

Proof function can also be offset in! ! !

Think

Review some MySQLof the datedifffunctions.

Subtracting the first parameter the second parameter.

datediff('2007-12-31','2007-12-30');   # 1
datediff('2010-12-30','2010-12-31');   # -1

It is also possible by date_addtime subtraction function.

date_add('2019-10-26', interval 1 day)

Using the oracleoffset function is solved.

Guess you like

Origin www.cnblogs.com/hider/p/11746421.html