gpt4 book ai didi

sql - 根据特定条件加入 Snowflake 中的表

转载 作者:行者123 更新时间:2023-12-05 05:38:25 32 4
gpt4 key购买 nike

尝试根据特定条件在 Snowflake 中连接两个表 A 和 B。我想加入 Person_id,但表 B 中的 Person_id 行必须是表 A 中的 1+行。

Table: A
|Person_id | Name |
|----------|----------|
| 0 | John |
| 1 | Patel |
| 2 | Aaron |
Table: B
|Person_id | Hourly |
|----------|----------|
| 1 | 20 |
| 2 | 30 |
| 3 | 25 |

我希望表 A 在连接后看起来像这样:

Table A: 
|Person_id | Name | Hourly |
|----------|----------|--------|
| 0 | John | 20 |
| 1 | Patel | 30 |
| 2 | Aaron | 25 |

最佳答案

join条件允许计算,所以只需要在表A的PERSON_ID上加一即可:

create table TABLE_A (PERSON_ID int, NAME string);
create table TABLE_B (PERSON_ID int, HOURLY int);

insert into TABLE_A (PERSON_ID, NAME) values
(0, 'John'),
(1, 'Patel'),
(2, 'Aaron');

insert into TABLE_B (PERSON_ID, HOURLY) values
(1, 20),
(2, 30),
(3, 25);

select A.PERSON_ID, A.NAME, B.HOURLY
from TABLE_A A
left join TABLE_B B
on A.PERSON_ID + 1 = B.PERSON_ID
;

关于sql - 根据特定条件加入 Snowflake 中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72916954/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com