[英]Join two tables using date condition
我在 Biquery 上有兩個事實表,如果 datestart 介於 dateCreate 和 (dateCreate+10) 之間,我想使用 id 加入
表A
ID | 日期創建 | 姓名 |
---|---|---|
2548 | 2020-02-05 | 史密斯 |
6005 | 2020-03-02 | 保羅 |
5485 | 2020-03-02 | 米奇 |
5435 | 2020-02-04 | 南希 |
2485 | 2020-07-05 | 米洛 |
2285 | 2020-12-06 | 珍妮弗 |
表B
id2 | 日期開始 | 工作 |
---|---|---|
2548 | 2020-02-20 | 開發 |
6005 | 2020-05-09 | 它 |
5485 | 2020-04-15 | 開發 |
5435 | 2020-02-10 | 它 |
2485 | 2020-09-20 | 法律 |
2285 | 2020-12-08 | 小時 |
如果 A.dateCreate < B.datestart < (A.dateCreate + 10days) then count= count+1 else 0
這是想要的結果:
| id |dateCreate| name |datestart | job |
|----|----------|------|----------|------|
|2548|2020-02-05| smith|2020-02-20| dev |
|6005|2020-03-02| paul | NULL | NULL |
|5485|2020-03-02| mitch| NULL | NULL |
|5435|2020-02-04| nancy|2020-02-10| it |
|2485|2020-07-05| milo | NULL | NULL |
|2285|2020-12-06| jenif|2020-12-08| hr |
這行得通嗎?
select a.*
from a left join
b
on b.id2 = a.id and
b.datestart > a.dateCreate and
b.datestart < date_add(a.dateCreate, interval 10 day);
BigQuery 有時會出現非等值連接問題,但我認為這會起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.