我在 Left Join 處遇到語法錯誤。因此,在嘗試將兩者結合起來時,我使用了左連接和方括號。我不確定問題出在哪里:
SELECT DISTINCT a.order_id
FROM fact.outbound AS a
ORDER BY Rand()
LIMIT 5
LEFT JOIN (
SELECT
outbound.marketplace_name,
outbound.product_type,
outbound.mpid,
outbound.order_id,
outbound.sku,
pbdh.mpid,
pbdh.product_type,
pbdh.validated_exp_reach,
pbdh.ultimate_sales_rank_de,
pbdh.ultimate_sales_rank_fr,
(
pbdh.very_good_stock_count good_stock_count new_Stock_count
) as total_stock
FROM
fact.outbound AS outbound
LEFT JOIN reporting_layer.pricing_bi_data_historisation AS pbdh ON outbound.mpid = pbdh.mpid
AND trunc(outbound.ordered_date) = trunc(pbdh.importdate)
WHERE
outbound.ordered_date > '2022-01-01'
AND pbdh.importdate > '2022-01-01'
LIMIT
5
) AS b ON a.orderid = b.order_id
錯誤:
您的 SQL 語法有錯誤;似乎錯誤就在附近:'LEFT JOIN ( SELECT outbound.marketplace_name, outbound.product_t' 在第 9 行
可能是什么原因?
uj5u.com熱心網友回復:
將第一個限制邏輯放入單獨的子查詢中,然后將兩個子查詢連接起來:
SELECT DISTINCT a.order_id
FROM
(
SELECT order_id
FROM fact.outbound
ORDER BY Rand()
LIMIT 5
) a
LEFT JOIN
(
SELECT
outbound.marketplace_name,
outbound.product_type,
outbound.mpid,
outbound.order_id,
outbound.sku,
pbdh.mpid,
pbdh.product_type,
pbdh.validated_exp_reach,
pbdh.ultimate_sales_rank_de,
pbdh.ultimate_sales_rank_fr,
(pbdh.very_good_stock_count
good_stock_count new_Stock_count) AS total_stock
FROM fact.outbound AS outbound
LEFT JOIN reporting_layer.pricing_bi_data_historisation AS pbdh
ON outbound.mpid = pbdh.mpid AND
TRUNC(outbound.ordered_date) = TRUNC(pbdh.importdate)
WHERE outbound.ordered_date > '2022-01-01' AND
pbdh.importdate > '2022-01-01'
-- there should be an ORDER BY clause here...
LIMIT 5
) AS b
ON a.orderid = b.order_id;
請注意,b子查詢的 select 子句可以簡化為order_id,因為最終實際上沒有選擇來自該子查詢的值。
uj5u.com熱心網友回復:
您可以跳過LEFT JOIN,因為沒有選擇 b 列。(并SELECT DISTINCT確保消除任何重復。)
SELECT DISTINCT order_id
FROM fact.outbound
ORDER BY Rand()
LIMIT 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467745.html
下一篇:在SQLServer中聚合動態列
