M
M
Mikhail Ksenofontov2022-03-22 11:34:20
PostgreSQL
Mikhail Ksenofontov, 2022-03-22 11:34:20

Next line data?

Hello.

There is a query that displays 4 lines:

select date, open_close from moex_fin m
where open_close BETWEEN 3.15 * 0.985 AND 3.15 * 1.005;

623988b8808e6323078251.png
The task is to display next the values ​​of the next day. The table is the same.
I can't figure out what's wrong, it only outputs two values ​​instead of 4.
select m.date, m.open_close, mo.date, mo.open_close from moex_fin m
  JOIN moex_fin mo ON (m.date + 1 = mo.date)
  where m.open_close BETWEEN 3.15 * 0.985 AND 3.15 * 1.005;

6239895f58443106844977.png
Tell me who knows.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2022-03-22
@KsenoLv

PostgreSQL provides function `lead` for this
case

with d as (
  select 
    date, 
    open_close,
    lead(date) over (order by date asc) next_date,
    lead(open_close) over (order by date asc) next_open_close
  from moex_fin m
) select * from d
where open_close BETWEEN 3.15 * 0.985 AND 3.15 * 1.005
;

PostgreSQL lead window function

F
freeExec, 2022-03-22
@freeExec

Because you have JOIN, and if there is nothing to connect from that side, then it doesn’t output from this one either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question