2017-08-28 70 views
-1

如果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   

Result

+0

您如何看待'日期爲28/08/2017'是重複記錄。請檢查第一欄和第三欄兩個記錄是INV/17-18/00004,514.95和INV/17-18/00005,600哪個是完全不同的。你真的只想爲每個單獨的日期只顯示一條記錄,如果是的話,你需要顯示哪一條。 –

+0

實際上有兩個不同的表,如果條件得到滿足,我只想要連接一次如果還滿足條件多次,我將需要28/08/2017,VEENA,300,214的行和記錄兩次 –

+0

我沒有詳細說明你想告訴你什麼。但是,當我加入上面提到的'[payment_customer]'和'[report_invoice]'表格時,需要輸出的內容是什麼?在上述問題中以文本格式顯示輸出。 –

回答

0

您可能需要指定字段的順序,並在Access SQL語法使用真正的日期表達式,使用貨幣的金額:

pay_date datetime, 
customer_name text, 
pay_amt Currency, 
bal_amt Currency 

inv_no text, 
inv_date datetime, 
pname text, 
grand_total Currency 

Insert Into report_invoice (inv_no,inv_date,pname,grand_total) 
Values ('INV/17-17/0001',#2017-08-08 00:00:00#,'Customer 1',1000) 
+0

更新插入查詢的建議 –

0

試試這個

一些修改後
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 cast(t1.pname as varchar(50)) = cast(t2.customer_name as varchar(50)) 
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(v.inv_date is null,v.pay_date,v.inv_date) 

Output - 

inv_no inv_date pname grand_total pay_date customer_name pay_amt bal_amt 
INV/17-17/0001 2017-08-08 00:00:00.000 Customer 1 1000.00 NULL NULL NULL NULL 
INV/17-17/0002 2017-08-27 00:00:00.000 Customer 1 300.00 2017-08-27 00:00:00.000 Customer 1 500.00 1500.00 
INV/17-17/0003 2017-08-27 00:00:00.000 Customer 1 2000.00 2017-08-27 00:00:00.000 Customer 1 500.00 1500.00 
INV/17-17/0004 2017-08-27 00:00:00.000 Customer 2 500.00 NULL NULL NULL NULL 
INV/17-17/0005 2017-08-28 00:00:00.000 Customer 1 3000.00 NULL NULL NULL NULL 
INV/17-17/0006 2017-08-29 00:00:00.000 Customer 3 700.00 NULL NULL NULL NULL 

編輯

檢查它是否是工作或沒有

Select v.inv_no,v.inv_date,v.pname,v.grand_total , v.pay_date,v.customer_name,v.pay_amt,v.bal_amt 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,ROW_NUMBER() over (partition by t2.pay_amt,t2.bal_amt,t1.inv_date order by t1.inv_date) rn from report_invoice t1 
LEFT join payment_customer t2 on t1.inv_date = t2.pay_date and cast(t1.pname as varchar(50)) = cast(t2.customer_name as varchar(50)) 
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,ROW_NUMBER() over (partition by t1.pay_amt,t1.bal_amt,t2.inv_date order by t2.inv_date) rn from payment_customer t1 
LEFT join report_invoice t2 on t1.pay_date = t2.inv_date where t2.inv_date is null 
) as v 
where rn = 1 
order by IIF(v.inv_date is null,v.pay_date,v.inv_date) 

輸出

inv_no inv_date pname grand_total pay_date customer_name pay_amt bal_amt 
INV/17-17/0001 2017-08-08 00:00:00.000 Customer 1 1000.00 NULL NULL NULL NULL 
INV/17-17/0004 2017-08-27 00:00:00.000 Customer 2 500.00 NULL NULL NULL NULL 
INV/17-17/0002 2017-08-27 00:00:00.000 Customer 1 300.00 2017-08-27 00:00:00.000 Customer 1 500.00 1500.00 
INV/17-17/0005 2017-08-28 00:00:00.000 Customer 1 3000.00 NULL NULL NULL NULL 
INV/17-17/0006 2017-08-29 00:00:00.000 Customer 3 700.00 NULL NULL NULL NULL 
+0

正如你可以看到2017年8月27日00:00:00.000客戶1 500.00 1500.00 快到了兩次我希望這種情況出現一次只 –

+0

檢查編輯的代碼 – Anagha

+0

錯誤缺少運營商查詢表達式RowNumber() –

0

如果我的理解是正確的,這是你所需要的。

Select distinct inv_no,inv_date,pname.grand_total,pay_date,customer_name,pay_amt,bal_amt from 
(
Select t1.inv_no as inv_no,t1.inv_date as inv_date,t1.pname as pname,t1.grand_total as grand_total , 
t2.pay_date as pay_date,t2.customer_name as customer_name,t2.pay_amt as pay_amt,t2.bal_amt as 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(v.inv_date is null,v.pay_date,v.inv_date) 

注: ISNULL將有兩個參數傳遞到它。

CASE可跨所有SQL平臺移植,而IIF是SQL SERVER 2012+專用的

+0

錯誤操作員案件時,v。inv_date爲空然後v.pay_date else v.inv_date在MS Access中結束asc –

+0

@RahulDesai現在我已更新query.once檢查它 –

+0

相同的錯誤錯誤缺少操作員案例,當v.inv_date爲空時,然後v.pay_date else v .inv_date結束asc in MS Access –