如果order by子句未應用於我的列[pay_date],我正在獲取列值。下面是創建表並插入虛擬值作爲排雷列值在訂單後爲零
CREATE TABLE [payment_customer]
(
pay_date datetime ,
customer_name text,
pay_amt Memo,
bal_amt Memo
)
CREATE TABLE [report_invoice]
(
inv_no text,
inv_date datetime ,
pname text,
grand_total Memo
)
Insert Into report_invoice Values
('INV/17-17/0001',#2017-08-08 00:00:00#,'Customer 1',1000)
Insert Into report_invoice Values
('INV/17-17/0002',#2017-08-27 00:00:00#,'Customer 1',300)
Insert Into report_invoice Values
('INV/17-17/0003',#2017-08-27 00:00:00#,'Customer 1',2000)
Insert Into report_invoice Values
('INV/17-17/0004',#2017-08-27 00:00:00#,'Customer 2',500)
Insert Into report_invoice Values
('INV/17-17/0005',#2017-08-28 00:00:00#,'Customer 1',3000)
Insert Into report_invoice Values
('INV/17-17/0006',#2017-08-29 00:00:00#,'Customer 3',700)
Insert Into payment_customer Values
(#2017-08-27 00:00:00#,'Customer 1',500,1500)
Insert Into payment_customer Values
(#2017-08-28 00:00:00#,'Customer 2',200,300)
最終查詢
Select * from
(
Select t1.inv_no,t1.inv_date,t1.pname,t1.grand_total , t2.pay_date,t2.customer_name,t2.pay_amt,t2.bal_amt from report_invoice t1
LEFT join payment_customer t2 on t1.inv_date = t2.pay_date and t1.pname = t2.customer_name
UNION ALL
Select t2.inv_no,t2.inv_date,t2.pname,t2.grand_total , t1.pay_date,t1.customer_name,t1.pay_amt,t1.bal_amt from payment_customer t1
LEFT join report_invoice t2 on t1.pay_date = t2.inv_date where t2.inv_date is null
) as v
order by IIF(IsNull(v.inv_date),v.pay_date,v.inv_date)
結果我收到設置相同的情況下查詢,但可以看到CUSTOMER_NAME,pay_amt,bal_amt月28日/ 08/2017是但卻難免重複 inv_no inv_date PNAME grand_total pay_date CUSTOMER_NAME pay_amt bal_amt
INV/17-18/00001 08/08/2017 Veena Industries Ltd. 238.832
INV/17-18/00002 27/08/2017 Excel Plants 514.95 27/08/2017 Excel Plants & Equipment Pvt. Ltd. 300 214.95
INV/17-18/00003 29/08/2017 I- Tech 400
INV/17-18/00004 28/08/2017 VEENA 514.95 28/08/2017 VEENA 300 214.95
INV/17-18/00005 28/08/2017 VEENA 600
您如何看待'日期爲28/08/2017'是重複記錄。請檢查第一欄和第三欄兩個記錄是INV/17-18/00004,514.95和INV/17-18/00005,600哪個是完全不同的。你真的只想爲每個單獨的日期只顯示一條記錄,如果是的話,你需要顯示哪一條。 –
實際上有兩個不同的表,如果條件得到滿足,我只想要連接一次如果還滿足條件多次,我將需要28/08/2017,VEENA,300,214的行和記錄兩次 –
我沒有詳細說明你想告訴你什麼。但是,當我加入上面提到的'[payment_customer]'和'[report_invoice]'表格時,需要輸出的內容是什麼?在上述問題中以文本格式顯示輸出。 –