gpt4 book ai didi

sql - Oracle:VLOOKUP 等效项

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

我有这样的观点:

col_1  col_2  my_date
----- ----- -------
1 5 2011
2 6 2014
3 7 2012
4 8 2011

还有一个像这样的表格:

date_1  date_2  the_value
------ ------ ---------
2010 2012 v1
2013 2015 v2

我想要类似 Excel VLOOKUP 函数的函数来查找 the_value 的值 ( my_date )介于date_1之间和date_2 ,所以我可以得到这样的结果:

col_1  col_2  my_date  the_value
----- ----- ------- ---------
1 5 2011 v1
2 6 2014 v2
3 7 2012 v1
4 8 2011 v1

日期列的类型为DATE 。为简单起见,这些是示例数据。

最佳答案

这是 SQL 中的连接 between而不是相等连接条件。

select t1.col_1, t1.col_2, t1.my_date, t2.the_value
from table_one t1
join table_two t2 on t1.my_date between t2.date_1 and t2.date_2;

请注意between 包括边界,因此它也会返回 my_date 所在的行是 2010 。如果您不想这样做,则需要使用 > 的连接条件和< :

select t1.col_1, t1.col_2, t1.my_date, t2.the_value
from table_one t1
join table_two t2 on t1.my_date > t2.date_1
and t1.my_date < t2.date_2;

这还要求您的“日期”范围不重叠,否则您会得到一些奇怪的结果。

关于sql - Oracle:VLOOKUP 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460823/

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