2017-02-05 48 views
0

嘿困在另一個SQL問題上....使用非等同連接加入

顯示所有客戶帳戶的事務。解決使用JOIN。按名稱,客戶編號,帳戶類型和交易編號排序。

注意:金額來自非等式連接表。老實說,不知道語法是否正確,還需要得到格式如16-11-25的日期。任何幫助將非常感激。

SELECT 
    First_name as "First", 
    Surname as "Last", 
    Customer_number as "Cust #", 
    wa.Account_type as "Type", 
    Transaction_number as "Trans #", 
    Transaction_type as "T", 
    Date_created('DD-MM-YY') as "Date", 
    Amount as "Amount" 
from 
    WGB_CUSTOMER wc 
    join WGB_ACCOUNT wa using (Customer_number) 
    join WGB_TRANSACTION wt using (Customer_number) 
    join WGB_ASSETS on (Amount>0) 
order by 
    1,2,4,5; 
+0

'加入... USING'是唯一一個語法速記Oracle的PL/SQL。爲了獲得最大的可移植性,您應該避免使用傳統的'JOIN ... ON'語法。這就是說,這看起來像一個家庭作業問題。 – Dai

+0

我們無法回答您的問題,因爲我們不知道您的數據庫表格設計。 – Dai

+0

另外,你應該避免在SQL中做任何日期格式,因爲這意味着你失去了類型安全性。您應該確保儘可能將原始數據類型傳遞給查詢的使用者。並且'yy-MM-dd'在任何國家的系統中都不是有效的日期格式,如果您需要統一的日期表示形式,則應該使用ISO 8601('yyyy-MM-dd'),但是隻能在視圖中進行轉換層,不在查詢中)。 – Dai

回答

0

USING子句不是Oracle獨有的,它是標準的SQL。這真的只是一個捷徑,所以

from 
    WGB_CUSTOMER wc 
    join WGB_ACCOUNT wa using (Customer_number) 

WGB_CUSTOMER wc 
    join WGB_ACCOUNT wa 
     on wc.Customer_number = wa.Customer_number 

走捷徑它依賴於一個事實,即鍵有兩個表中相同的名稱。 通常建議避免該捷徑,因爲它可能會導致錯誤。

回來到您的具體的例子,我想你想加入到WGB_ASSETS然後測試量是什麼,是這樣的:

SELECT 
    First_name as "First", 
    Surname as "Last", 
    Customer_number as "Cust #", 
    wa.Account_type as "Type", 
    Transaction_number as "Trans #", 
    Transaction_type as "T", 
/* Date_created('DD-MM-YY') as "Date", < incorrect syntax */ 
    to_char(date_created, 'DD-MM-YY') as "Date" 
    Amount as "Amount" 
from 
    WGB_CUSTOMER wc 
    join WGB_ACCOUNT wa 
     on wc.Customer_number = wa.Customer_number 
    join WGB_TRANSACTION wt 
     on wc.Customer_number = wt.Customer_number  
    join WGB_ASSETS wg 
     on wc.Customer_number = wg.Customer_number 
    where wg.Amount>0 
order by 
    1,2,4,5; 
+0

@philipxy。好點。我會更新我的答案... – BobC

+0

謝謝大家的所有幫助!我現在唯一的問題是如何使Transaction_number在此查詢中唯一? – DNkG