简体   繁体   English

按历史日期连接两个表

[英]Join two tables by historical date

my difficulty is to join two tables with gmv value.我的困难是加入两个具有 gmv 值的表。

table 1表格1

| id       | historical_date|     status     |
| -------- | -------------- | -------------- |
| 45615266 | 2021-06-02     | Pending        |
| 45615266 | 2021-12-05     | Validated      |

table 2表 2


| id       | grass_date     |     value      |
| -------- | -------------- | -------------- |
| 45615266 | 2021-02-02     | 24.02          |
| 45615266 | 2021-03-17     | 15.48          |
| 45615266 | 2020-12-21     | 1993.85        |
| 45615266 | 2021-06-02     | 74.56          |
| 45615266 | 2021-07-14     | 74.48          |
| 45615266 | 2021-12-04     | 99.48          |

Expected预期的


| id       | historical_date |     status     | grass_date     | value          |
| 45615266 | 2021-06-02      | Pending        | 2021-02-02     | 24.02          |
| 45615266 | 2021-06-02      | Pending        | 2021-03-17     | 15.48          |
| 45615266 | 2021-06-02      | Pending        | 2020-12-21     | 1993.85        |
| 45615266 | 2021-12-05      | Validated      | 2021-06-02     | 74.56          |
| 45615266 | 2021-12-05      | Validated      | 2021-07-14     | 74.48          |
| 45615266 | 2021-12-05      | Validated      | 2021-12-04     | 99.48          |

I use Trino (prestoSQL) and the subquery is limited to one column我使用 Trino (prestoSQL) 并且子查询仅限于一列

You can use a NATURAL JOIN :您可以使用NATURAL JOIN

SELECT * FROM table 1 NATURAL JOIN table 2

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

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