如果值是相似的而不是精确的,如何关联来自不同表的两列?

How to relate two columns from separate tables, if the values are SIMILAR not EXACT?

我有两列要加入,但我得到了一堆空值,因为它不是在两列中说 "SWEATER",而是在一列中说 "SWEATER" 和 "SWEAT" 在另一个。我做了一个FULL OUTER JOIN,虽然匹配了很多,但是还是有一堆null因为上面的问题。

使用 Tableau 对两个查询执行 FULL OUTER JOIN:一个查询提取 "number of items sold",另一个查询提取 "number of items returned",匹配项目 name/item 描述(其中,在一个完美的世界中,会完全匹配。但这是我的问题,他们没有)。

SELECT 
    item_description_1, SUM(quantity_ordered) "units_sold_OCT2019"
FROM
    l_dmw_order_report
WHERE
    quantity_ordered > 0
    AND oms_order_date BETWEEN '2019-10-01' AND '2019-10-31'

GROUP BY item_description_1
    HAVING item_description_1 NOT IN ('Freight')

ORDER BY item_description_1

SELECT
    item_name, SUM(return_qty) "#_of_returns_OCT2019"
FROM 
    l_nvr_ec_returns
WHERE 
    return_created_date BETWEEN '2019-10-01' AND '2019-10-31'
    AND return_status NOT IN ('Cancelled', 'cancelled')

GROUP BY item_name
    HAVING item_name NOT IN ('')
ORDER BY item_name

[这是我卡住的地方的图片,那里的值不匹配,我不知道如何继续才能匹配它们...][1] [1] : https://i.stack.imgur.com/B61Gr.png

以下是我的建议。 l_dmw_order_report.item_description_1 为您提供已售出的商品,您希望将其与 l_nvr_ec_returns.item_name.

匹配

很遗憾,它们不匹配。

第 1 步:检查您的过滤条件(HAVING 语句)。你不认为你也应该保持 NOT IN ('Freight') for table with l_nvr_ec_returns

第二步:两个table可以有不同的命名description/items,你可以通过写CASE语句来匹配它们。

示例:

Item_name 
--------
SWEATER
JACKET
item_description_1
-------
SWEAT
JACK

然后

SELECT 
    (case when item_description_1='SWEAT' then 'SWEATER'
      when item_description_1='JACK' then 'JACKET'
END) as item_descrption_1, SUM(quantity_ordered) "units_sold_OCT2019"
FROM
    l_dmw_order_report
WHERE
    quantity_ordered > 0
    AND oms_order_date BETWEEN '2019-10-01' AND '2019-10-31'

GROUP BY item_description_1
    HAVING item_description_1 NOT IN ('Freight')

ORDER BY item_description_1

试试这个。