简体   繁体   中英

SQL get closest value by date

can't wrap my mind around the next problem I have a table with historical data TableA :

uniq_id item_id item_clust date
11111   1       a          2020-02-12
11112   1       a          2020-01-13
11113   1       b          2020-02-01
11114   2       b          2020-01-01

I also have a table with historical data for clusters TableB :

item_id item_clust item_pos date
1       a          1        2020-01-01
1       a          2        2020-02-01
1       a          3        2020-03-01
1       b          1        2020-01-10

I would like to receive the latest position for every item_id + item_clust for date based on dates in TableB If no rows found, I would like to insert item_pos = 0

Desired result:

uniq_id item_id item_clust date       item_pos
11111   1       a          2020-02-12 2
11112   1       a          2020-01-13 1
11113   1       b          2020-02-01 1
11114   2       b          2020-01-01 0

So, for item 1 in cluster a on 2020-02-12 the latest position is at 2020-02-01 = 2.

This looks like a left join :

select a.*, coalesce(b.item_pos, 0) as item_pos
from a left join
     (select distinct on (b.item_id, b.item_clust) b.*
      from b
      order by b.item_id, b.item_clust, b.date desc
     ) b
     using (item_id, item_clust);

Or a lateral join:

select a.*, coalesce(b.item_pos, 0) as item_pos
from a left join lateral
     (select b.*
      from b
      where b.item_id = a.item_id and
            b.item_clust = a.item_clust
      order by b.date desc
      limit 1
    ) b
    on true;  -- always do the left join even when there are no matches

EDIT:

If you want the most recent position "as of" the date in A , then use the lateral join:

select a.*, coalesce(b.item_pos, 0) as item_pos
from a left join lateral
     (select b.*
      from b
      where b.item_id = a.item_id and
            b.item_clust = a.item_clust and
            b.date <= a.date
      order by b.date desc
      limit 1
    ) b
    on true;  -- always do the left join even when there are no matches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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