简体   繁体   English

不满足条件时连接表 - SQL Oracle

[英]Joining tables when a condition is not met - SQL Oracle

I have below two tables:我有以下两个表:

在此处输入图像描述

在此处输入图像描述

~ What I am looking to do: I want to append the Price from Table 2 (t2) to Table 1 (t1), by joining on Quantity and YYYY_MM. ~ 我想做的事情:我想通过加入数量和 YYYY_MM 将表 2 (t2) 中的价格附加到表 1 (t1)。 Each t2.price was active in a certain time range (t2.Price_Active_Date_From and t2.Price_Active_Date_To), and the t1.Order_Date should fall within this range.每个 t2.price 都在某个时间范围内(t2.Price_Active_Date_From 和 t2.Price_Active_Date_To)处于活动状态,并且 t1.Order_Date 应该在这个范围内。 When there is no active price at that order date, I want the result to return null.当该订单日期没有有效价格时,我希望结果返回 null。

So the result should look like:所以结果应该是这样的:

在此处输入图像描述

What I have tried below so far, which works to get the Price_Active_At_Order when there is a price at a certain date, but it doesn't work when there's no active price.到目前为止,我在下面尝试过的方法是,当某个日期有价格时,它可以获取 Price_Active_At_Order,但是当没有有效价格时它不起作用。 How to add a condition in the join so this works?:如何在联接中添加条件以使其有效?:

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 returns empty if the matching results are not found.如果没有找到匹配的结果,Inner Join 返回空。 You can handle it on code level for setting null value if empty response has been returned.如果返回空响应,您可以在代码级别处理它以设置 null 值。 However in SQL you'll only get empty result.但是在 SQL 中,您只会得到空结果。

If you still want to get NULL values you can use LEFT Join that will return the values for the rows not match with the condition as null for the table on the right side of the Join.如果您仍想获得 NULL 值,您可以使用 LEFT Join 它将与条件不匹配的行的值返回为连接右侧表的 null。

Similarly for Right Join.右连接也是如此。

When using Left Join, Right Join, Full (Outer) Join, it is possible to return NULL value while (inner) join, cross join will not return NULL value.使用左连接、右连接、全(外)连接时,可能返回 NULL 值,而(内)连接、交叉连接不会返回 NULL 值。

For understanding purposes you can go through this link for SQL Join: https://www.geeksforgeeks.org/sql-join-set-1-inner-left-right-and-full-joins/为便于理解,您可以通过此链接进行 SQL 连接: https ://www.geeksforgeeks.org/sql-join-set-1-inner-left-right-and-full-joins/

And answer of the below question: https://docs.microsoft.com/en-us/answers/questions/99875/sql-query-full-join-multiple-tables-but-return-nul.html并回答以下问题: https ://docs.microsoft.com/en-us/answers/questions/99875/sql-query-full-join-multiple-tables-but-return-nul.html

Using the data from your question the result is a bit different because the order date from tbl_1 for product 10334 and tbl_1 quantity=1 is out of the period in tbl_2, so the resulting price is Null.使用您问题中的数据,结果会有所不同,因为产品 10334 和 tbl_1 数量 = 1 的订单日期超出了 tbl_2 的期间,因此结果价格为空。

WITH
    tbl_1 AS
        (
            Select 10023 "PRODUCT_NR", 'X' "CUSTOMER", 20 "QUANTITY", 202004 "ORDER_DATE" From Dual UNION ALL
            Select 10023 "PRODUCT_NR", 'Y' "CUSTOMER", 10 "QUANTITY", 202203 "ORDER_DATE" From Dual UNION ALL
            Select 10334 "PRODUCT_NR", 'X' "CUSTOMER",  1 "QUANTITY", 202204 "ORDER_DATE" From Dual 
        ),
    tbl_2 AS
        (
            Select 10023 "PRODUCT_NR",   1 "QUANTITY", 100 "PRICE", 202101 "DATE_FROM", 999912 "DATE_TO" From Dual UNION ALL
            Select 10023 "PRODUCT_NR",  10 "QUANTITY", 120 "PRICE", 202101 "DATE_FROM", 999912 "DATE_TO" From Dual UNION ALL
            Select 10023 "PRODUCT_NR",  20 "QUANTITY", 300 "PRICE", 201605 "DATE_FROM", 202001 "DATE_TO" From Dual UNION ALL
            Select 10023 "PRODUCT_NR",  20 "QUANTITY", 250 "PRICE", 202101 "DATE_FROM", 999912 "DATE_TO" From Dual UNION ALL
            Select 10023 "PRODUCT_NR", 100 "QUANTITY", 400 "PRICE", 202101 "DATE_FROM", 999912 "DATE_TO" From Dual UNION ALL
            Select 10334 "PRODUCT_NR",   1 "QUANTITY",  25 "PRICE", 202101 "DATE_FROM", 202111 "DATE_TO" From Dual UNION ALL
            Select 10334 "PRODUCT_NR",   1 "QUANTITY",  30 "PRICE", 202101 "DATE_FROM", 202111 "DATE_TO" From Dual 
        )
SELECT
    t1.PRODUCT_NR "PRODUCT_NR",
    t1.CUSTOMER "CUSTOMER",
    t1.QUANTITY "QUANTITY",
    t2.PRICE "PRICE_AT_ORDER",
    t1.ORDER_DATE "ORDER_DATE"
FROM
    tbl_1 t1
LEFT JOIN
    tbl_2 t2 ON(t2.PRODUCT_NR = t1.PRODUCT_NR And t2.QUANTITY = t1.QUANTITY And t1.ORDER_DATE Between t2.DATE_FROM And DATE_TO)
ORDER BY
    t1.PRODUCT_NR,
    t1.ORDER_DATE
--  
--  R e s u l t
--  
--  PRODUCT_NR CUSTOMER   QUANTITY PRICE_AT_ORDER ORDER_DATE
--  ---------- -------- ---------- -------------- ----------
--       10023 X                20           Null     202004 
--       10023 Y                10            120     202203 
--       10334 X                 1           Null     202204

As you can see, all that should be done is change your JOIN into LEFT JOIN.如您所见,应该做的就是将您的 JOIN 更改为 LEFT JOIN。 Join (Inner Join) returns no rows when condition has no match.当条件不匹配时,Join(Inner Join)不返回任何行。 Regards...问候...

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

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