简体   繁体   中英

inner join select only one row from second table base on date

I have a users table. Each record has one or more prices by date in payments table. I'm just going to show a record that start_date column is less than or equal to today's?

users table

╔════╦══════════════╗
║ id ║  name        ║
╠════╬══════════════║
║  1 ║ Jeff         ║
║  2 ║ Geoff        ║
╚════╩══════════════╝

payments table

╔═══════════════════════════════════╗
║ user_id         start_date  price ║
╠═══════════════════════════════════╣
║ 1               2019-10-14  1000  ║
║ 1               2019-10-11  3500  ║
║ 1               2019-10-16  2000  ║
║ 2               2019-10-13  3500  ║
║ 2               2019-10-12  6500  ║
╚═══════════════════════════════════╝

today date => 2019-10-13

What I want:

╔═══════════════════════════════════╗
║ user_id         start_date  price ║
╠═══════════════════════════════════╣
║ 1               2019-10-11  3500  ║
║ 2               2019-10-13  3500  ║
╚═══════════════════════════════════╝
where date_column <= sysdate

or, eventually

where date_column <= trunc(sysdate)

depending on whether there is a time component involved or not.

[EDIT, after you included sample data]

As "today" is 2019-10-13 , then see if this helps; you'll need lines from #14 onwards as you already have those tables. BTW, it seems that USERS doesn't play any role in desired result.

SQL> with
  2  users (id, name) as
  3    (select 1, 'Jeff' from dual union all
  4     select 2, 'Geoff' from dual
  5    ),
  6  payments (user_id, start_date, price) as
  7    (select 1, date '2019-10-14', 1000 from dual union all
  8     select 1, date '2019-10-11', 3500 from dual union all
  9     select 1, date '2019-10-16', 2000 from dual union all
 10     select 2, date '2019-10-13', 3500 from dual union all
 11     select 2, date '2019-10-12', 6500 from dual
 12    ),
 13  --
 14  temp as
 15    (select p.user_id, p.start_date, p.price,
 16       row_number() over (partition by user_id order by start_date desc) rn
 17     from payments p
 18     where p.start_date <= date '2019-10-13'
 19    )
 20  select user_id, start_date, price
 21  from temp
 22  where rn = 1;

   USER_ID START_DATE      PRICE
---------- ---------- ----------
         1 2019-10-11       3500
         2 2019-10-13       3500

SQL>

One method uses a correlated subquery:

select p.*
from payments p
where p.date = (select max(p2.start_date)
                from payments p2
                where p2.user_id = p.user_id and
                      p2.start_date <= date '2019-10-13'
               );

Or in Oracle, you can use aggregation and keep :

select p.user_id, max(p.start_date) as start_date,
       max(p.price) keep (dense_rank first order by p.start_date desc) as price
from payments p
group by p.user_id;

The keep syntax (in this example) is keeping the first value in the aggregation.

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