datetime | param1 |
---|---|
2018-08-01 12:10 | red |
2018-08-02 15:20 | green |
2018-08-03 08:30 | blue |
datetime | param2 |
---|---|
2018-08-01 12:15 | 123 |
2018-08-01 13:20 | 234 |
2018-08-01 22:30 | 345 |
2018-08-02 16:10 | 456 |
2018-08-02 21:15 | 567 |
datetime | param2 | param1 |
---|---|---|
2018-08-01 12:15 | 123 | red |
2018-08-01 13:20 | 234 | red |
2018-08-01 22:30 | 345 | red |
2018-08-02 16:10 | 456 | green |
2018-08-02 21:15 | 567 | green |
SELECT
t2.datetime
, param2
, (SELECT param1
FROM
(SELECT
t1.param1
, row_number() OVER(ORDER BY t1.datetime DESC) rankparam1
FROM table1 t1
WHERE t2.datetime>=t1.datetime)
WHERE rankparam1 = 1)
as param1
FROM table2 t2