Oracle Query看不出有什么问题

时间:2013-10-21 18:43:16

标签: oracle

我正在尝试运行此查询,但不知何故标签B的底部部分无法运行。

我是甲骨文的新手,我只是看不出有什么不对。谁能帮帮我吗?非常感谢。

当销售人员没有依赖它时,它不会返回任何数据。无论如何,这仍应返回有关基本客户的信息。

SELECT DISTINCT X.ship_sales_representative_id,
                X.ship_sales_representative_name,
                X.ship_sales_regional_name,
                X.ship_sales_regional_head,
                B.bl_customer_representative_id,
                X.sp_customer_representative_id,
                B.bill_customer_id,
                X.ship_customer_id,
                B.bill_customer_address_suffix,
                X.ship_customer_address_suffix,
                B.bill_customer_name,
                X.ship_customer_name,
                B.bill_customer_address,
                X.ship_customer_address,
                B.bill_customer_city,
                X.ship_customer_city,
                B.bill_customer_statecode,
                X.ship_customer_statecode,
                B.bill_customer_zipcode,
                X.ship_customer_zipcode,
                B.bill_customer_phonenumber,
                X.ship_customer_phonenumber,
                B.bill_customer_faxnumber,
                X.ship_customer_faxnumber,
                B.bill_customer_email,
                X.ship_customer_email,
                B.bill_customer_contact,
                X.ship_customer_contact
FROM   (SELECT DISTINCT S.rep_id      Ship_Sales_Representative_ID,
                        S.rep_name    Ship_Sales_Representative_Name,
                        S.reg_name    Ship_Sales_Regional_Name,
                        S.reg_head    Ship_Sales_Regional_Head,
                        C.rep_id      Sp_Customer_Representative_ID,
                        C.cust_id     Ship_Customer_ID,
                        C.addr_suffix Ship_Customer_Address_Suffix,
                        C.name        Ship_Customer_Name,
                        C.addr_ln_1   Ship_Customer_Address,
                        C.city        Ship_Customer_City,
                        C.state_cd    Ship_Customer_StateCode,
                        C.zip_cd      Ship_Customer_Zipcode,
                        C.phone_nbr   Ship_Customer_PhoneNumber,
                        C.fax_nbr     Ship_Customer_FaxNumber,
                        C.email       Ship_Customer_Email,
                        C.contact     Ship_Customer_Contact
        FROM   mdw.customer C,
               mdw.sales_org S
        WHERE  C.rep_id = To_char(S.rep_id, 'FM000000')
               AND C.cust_id = v_cust_id
               AND C.addr_suffix = v_addr_suffix) X,
       (SELECT DISTINCT C.rep_id      Bl_Customer_Representative_ID,
                        C.cust_id     Bill_Customer_ID,
                        C.addr_suffix Bill_Customer_Address_Suffix,
                        C.name        Bill_Customer_Name,
                        C.addr_ln_1   Bill_Customer_Address,
                        C.city        Bill_Customer_City,
                        C.state_cd    Bill_Customer_StateCode,
                        C.zip_cd      Bill_Customer_Zipcode,
                        C.phone_nbr   Bill_Customer_PhoneNumber,
                        C.fax_nbr     Bill_Customer_FaxNumber,
                        C.email       Bill_Customer_Email,
                        C.contact     Bill_Customer_Contact
        FROM   mdw.customer C
        WHERE  C.cust_id = v_cust_id
               AND C.addr_suffix = '0001') B; 

1 个答案:

答案 0 :(得分:0)

您可能希望在查询结尾添加:

WHERE X.REP_ID = B.REP_ID              

(并删除第一个DISTINCT)或JOIN的其他条件,否则此查询将返回XB中记录的所有记录组合

因为你正在寻找

AND   C.CUST_ID     =v_Cust_ID

你应该在加入中使用它!

由于您不确定X中是否存在这些信息,因此您应该在LEFT JOIN上使用X,您可以在Oracle中执行以下操作:

WHERE B.CUST_ID = X.CUST_ID(+)