【发布时间】:2022-07-19 20:29:17
【问题描述】:
我有以下两张表:
~ 我想要做什么:我想通过加入数量和 YYYY_MM 将表 2 (t2) 中的价格附加到表 1 (t1)。每个 t2.price 在一定的时间范围内(t2.Price_Active_Date_From 和 t2.Price_Active_Date_To)是活跃的,并且 t1.Order_Date 应该在这个范围内。当该订单日期没有有效价格时,我希望结果返回 null。
所以结果应该是这样的:
到目前为止,我在下面尝试过,当某个日期有价格时,它可以获取 Price_Active_At_Order,但是当没有有效价格时它不起作用。如何在联接中添加条件以使其有效?:
select distinct
t1.Product_NR,
t1.Customer,
t1.Quantity,
t2.Price as Price_Active_At_Order,
t1.Order_YYYYMM as Order_Date
from Table_1 t1
join Table_2 t2 on t1.Product_NR = t2.Product_NR
and t1.Quantity = t2.Quantity
and t1.Order_YYYYMM between t2.Price_Active_Date_From and t2.Price_Active_Date_To
【问题讨论】:
-
将
[inner] join更改为left join