简体   繁体   English

内连接 select 仅从第二个表中的一行基于日期

[英]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?我只是要显示一条start_date列小于或等于今天的记录?

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今天日期 => 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;由于“今天”是2019-10-13 ,那么看看这是否有帮助; you'll need lines from #14 onwards as you already have those tables.你需要从 #14 开始的行,因为你已经有了这些表。 BTW, it seems that USERS doesn't play any role in desired result.顺便说一句,似乎USERS在期望的结果中没有任何作用。

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 :或者在 Oracle 中,您可以使用聚合并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. keep语法(在本例中)是保留聚合中的第一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL INNER JOIN select 第二张表只有一行 - MySQL INNER JOIN select only one row from second table INNER JOIN从第二个表中仅选择一行 - INNER JOIN selects only one row from second table 如果所有线索均处于活动状态且引线均为0且primary_email =第二表中的是,则MySQL INNER JOIN仅从第一表中选择一行 - MySQL INNER JOIN select only one row from first table if all leads active = 0 and primary_email = Yes in second table 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 即使ID匹配,SQL INNER JOIN也仅从第二个表中选择最后一行 - SQL INNER JOIN selects only one, last row from second table even if id matches 从第二个表中仅连接表一到多个关系 - Join table one to many relationship only 1 row from second table php JOIN 语句只从第二个表中返回一行 - php JOIN statement only returns one row from second table 如何在不使用别名的情况下将第二个表中的一行仅插入到第一个表中 - How to INNER JOIN only 1 row from second table into the first table WITHOUT using alias 从内部联接仅获得一行 - Get only one row from inner join MySQL连接与子查询从连接表中仅选择一行 - MySQL Join with a subquery to select only one row from joined table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM