如何解决以下代码的“列定义不明确”

问题描述 投票:0回答:1

我正在尝试合并两个表。但是每个表里面都有很多聚合、条件。 错误消息显示在第 3 行。我已经删除了 case 语句的主要部分,因为它们不应该是问题所在。

SELECT "Org_ID", "Vendor_Name", "Vendor_Number", "PO_Project", "PO_Number", "PO_Distribution_ID" -- Not sure about PO date, differnt lines under same PO has different Line_create_date
    , "Invoice_Date", "First_Due_Date", "Invoice_ID", "Company_Code", Adj2020_Estimated_Payment_Date 
    , Inv_Line_Amt_USD_Dist, "Payment_Date", Paid_Amt_Dist_PO_USD, "Unpaid_USD_Abs", First_layer, Second_layer, Third_layer
FROM
    (SELECT  t2."Org_ID", t2."Vendor_Name", t2."Vendor_Number", t2."PO_Project", t2."PO_Number", t2."PO_Distribution_ID" -- Not sure about PO date, differnt lines under same PO has different Line_create_date
    , t2."Invoice_Date", t2."First_Due_Date", t3."Invoice_ID", t3."Company_Code", t2.Line_Amt_USD, t2.Inv_Total_Amt_USD, t2.Adj2020_Estimated_Payment_Date
    , ROUND((t2.Inv_Total_Amt_USD - t2.Inv_Paid_Amt_USD), 2) AS Unpaid_USD
    , ROUND(t2.Line_Amt_USD/COUNT(t2."PO_Distribution_ID") OVER (PARTITION BY t2."PO_Distribution_ID", t2."Invoice_ID"), 2) AS Inv_Line_Amt_USD_Dist
    , t3."Payment_Date", ROUND(t3."Paid_Amt_USD"*(t2.Line_Amt_USD/t2.Inv_Total_Amt_USD), 2) AS Paid_Amt_Dist_PO_USD, t3."Paid_Amt_USD" AS Total_Paid_Amt_USD
    , Case 
        WHEN ROUND((t2.Inv_Total_Amt_USD - t2.Inv_Paid_Amt_USD), 2) < 0
            THEN 0
            ELSE ROUND((t2.Inv_Total_Amt_USD - t2.Inv_Paid_Amt_USD), 2)
    END AS "Unpaid_USD_Abs"
    ,Case
        WHEN 
        TEHN
        ELSE
    END AS First_layer
    , Case 
        WHEN 
        TEHN
        ELSE
    END AS Second_layer
    , Case 
        WHEN 
        TEHN
        ELSE
    END AS Third_layer
FROM
(
    SELECT t1."Org_ID", t1."PO_Project", t1."PO_Number", t1."PO_Distribution_ID", t1."Invoice_ID", t1."Vendor_Name", t1."Vendor_Number", t1."Invoice_Date" 
    , t1."First_Due_Date", ROUND(SUM(t1."Line_Amt_USD"),2) AS Line_Amt_USD, ROUND(AVG(t1."Inv_Total_Amt"/NVL(t1."Conv_Rate_USD", 1)), 2) AS Inv_Total_Amt_USD,
    ROUND(AVG(t1."Inv_Paid_Amt"/ NVL(t1."Conv_Rate_USD", 1)),2) AS Inv_Paid_Amt_USD
    ,CASE
        WHEN '01-JAN-20' <  TO_DATE(("First_Due_Date"), 'DD-MON-YY') AND  TO_DATE(("First_Due_Date"), 'DD-MON-YY') < '01-JAN-24'
        THEN '01-JAN-24'
        ELSE TO_CHAR(TO_DATE(("First_Due_Date"), 'DD-MON-YY'), 'DD-MON-YY')
    END AS Adj2020_Estimated_Payment_Date
    FROM "RPT_VIEW"."RPT_AP_INVOICE_DIST" t1
    WHERE t1."PO_Number" IS NOT NULL 
    AND t1."Line_Amt_USD" <> 0 
    AND t1."Line_Amt_USD" IS NOT NULL 
    AND t1."Inv_Total_Amt" <> 0
    AND t1."Inv_Total_Amt" IS NOT NULL 
    AND t1."PO_Glbl_Acct" in ('12145', '12155')
    GROUP BY t1."PO_Distribution_ID", t1."Invoice_ID", t1."Org_ID", t1."PO_Project", t1."PO_Number", t1."Vendor_Name", t1."Vendor_Number", t1."Invoice_Date",t1."First_Due_Date"
) t2
LEFT JOIN "RPT_VIEW"."RPT_AP_INVOICE_PAYMENTS" t3
ON t2."Invoice_ID" = t3."Invoice_ID"
WHERE extract(YEAR from t2."Invoice_Date") in (2020, 2021, 2022, 2023, 2024)
AND t2.Inv_Total_Amt_USD  <> 0
AND t2.Inv_Total_Amt_USD  IS NOT NULL) t4 
LEFT JOIN (
SELECT
    "Hdr_Closed_Code",
    "Shipment_Create_Date",
    "Operating_Unit",
    "Ship_To_Location",
    "Global_Account",
    "Promise_Date",
    "Supplier_Number",
    "Supplier_Name",
    "PO_Number",
    "Project_Number",
    "Ship_Approved_Flag",
    "Payment_Term_Days",
    "USD_Unbilled_Abs",
    ("Payment_Term_Days" + "Promise_Date") AS "Estimated_Payment_Date",
    "PO_Distribution_Id",
    CASE
        WHEN '01-JAN-20' <  TO_DATE(("Payment_Term_Days" + "Promise_Date"), 'DD-MON-YY') AND  TO_DATE(("Payment_Term_Days" + "Promise_Date"), 'DD-MON-YY') < '01-JAN-24'
        THEN '01-JAN-24'
        ELSE TO_CHAR(TO_DATE(("Payment_Term_Days" + "Promise_Date"), 'DD-MON-YY'), 'DD-MON-YY')
    END AS Adj2020_Estimated_Payment_Date
FROM
    (SELECT
        "Hdr_Closed_Code",
        "Ship_Canceled_Flag",
        "Shipment_Create_Date",
        "Operating_Unit",
        "Ship_To_Location",
        "Global_Account",
        "Payment_Term",
        "Promise_Date",
        "Supplier_Number",
        "Supplier_Name",
        "PO_Number",
        "Project_Number",
        "USD_Ordered_Amt",
        "USD_Billed_Amt",
        ROUND("Quantity_Canceled" * NVL("Conv_Rate", 1), 2) AS "USD_Quantity_Canceled",
        "USD_Ordered_Amt" - "USD_Billed_Amt" - ROUND("Quantity_Canceled" * NVL("Conv_Rate", 1), 2) AS "USD_Unbilled",
        "Ship_Approved_Flag",
        "Ship_Canceled_Flag",
        "PO_Distribution_Id",
        CASE
            WHEN ("USD_Ordered_Amt" - "USD_Billed_Amt" - ROUND("Quantity_Canceled" * NVL("Conv_Rate", 1), 2)) < 0
            THEN 0
            ELSE ("USD_Ordered_Amt" - "USD_Billed_Amt" - ROUND("Quantity_Canceled" * NVL("Conv_Rate", 1), 2))
        END AS "USD_Unbilled_Abs",
        CASE
            WHEN "Payment_Term" ='Pay in Zero Day'THEN 0
            WHEN "Payment_Term" ='Pay in One Day'THEN 1
            WHEN "Payment_Term" ='Pay in One Day from Receipt'THEN 1
            WHEN "Payment_Term" ='Pay in 10 days'THEN 10
            ELSE 0
        END AS "Payment_Term_Days"
    FROM "RPT_VIEW"."RPT_PO_INFO" 
    WHERE "Global_Account" IN ('12145', '12155'))) t5
ON t4."PO_Distribution_ID" = t5."PO_Distribution_Id"

请给我一些建议。我真的需要帮助

sql oracle syntax-error
1个回答
0
投票

当您从多个嵌套子查询中进行选择时,最好ALWAYS在您的

SELECT
语句中使用表别名,使用正确的表别名将帮助您避免此错误。

根据错误消息:

ORA-00918: column ambiguously defined 00918. 
00000 - "column ambiguously defined" 
*Cause: *Action: Error at Line: 3 Column: 71

不明确的列可能是

Adj2020_Estimated_Payment_Date
,尽管这是您粘贴的查询中的第 2 行,但它确实从第 71 列开始。

除此之外,我们还可以看到 2 个联接查询

t4
t5
都有一个名为
Adj2020_Estimated_Payment_Date
的字段,因此您需要选择在主查询中返回哪一个字段,或者如果您想要两者,或者如果该字段有某种适当的coalesce逻辑,那将是合适的。


如果您无法按照错误消息中的LineColumn引用来识别导致错误的字段引用,那么一种简单的故障排除技术是注释掉所有字段并逐个取消注释 -一个并在每次尝试之间执行查询,然后通过反复试验,您将在查询第一次失败时找到有问题的字段,您添加的最后一个字段将是罪魁祸首。

© www.soinside.com 2019 - 2024. All rights reserved.