2015-12-26 73 views
1

我正在使用SQL Server Compact Edition 4.0,並且有兩個表稱爲借方和貸方,如下所示。如何將2列作爲一個並按日期排序?

tbl_debit

invoice | dealer | price| purchasedate 
========================================= 
    001 | AAA | 1000 | 2/9/2016 8:46:38 PM 
    002 | AAA | 1500 | 2/20/2016 8:46:38 PM 

tbl_credit

dealer | settlement| purchasedate 
========================================= 
    AAA | 800 | 2/12/2016 8:46:38 PM 
    AAA | 400 | 2/22/2016 8:46:38 PM 

我想創建一個表,其中應包括4列..

Invoice, Dealer, Amount, date 

量應包括settlementtbl_creditpricetbl_debit,需要按日期排序。

我真的很感謝有人能幫助我。

+0

向我們展示你的** **的努力迄今! –

+2

您需要解釋組合表的邏輯。最終的結果應該是什麼樣子? –

+0

SELECT量,而purchaseDate FROM tbl_debit UNION ALL SELECT 金額,日期 FROM tbl_credit ORDER BY而purchaseDate – Sachith

回答

1

這裏有一個腳本邏輯接近基於呈現給我們的信息有限的問題:

SELECT A.invoice, A.dealer, A.amount, A.purchasedate 
     FROM (SELECT A.invoice, A.dealer, A.price [amount], A.purchasedate 
     WHERE tbl_debit A 
     UNION 
     SELECT ' ', B.dealer, B.settlement, B.purchasedate 
     FROM tbl_credit B) A 
     ORDER BY 4