2012-11-30 64 views
1

我有2個表無論是否加入2個選擇查詢。記錄

table 1        table 2 
------------------     ---------------------- 
description paidamt    description recievedamt 
ele. bill  200     donation 1000 
stationary 500     fees  200 
salary  1000     

,我想導致以下格式(數據將在日期的基礎上進行排序)

description debit credit 
--------------------------- 
ele. bill 200 
donation   1000 
stationary 500 
fees    200 
salary  1000 

事情是,當我設置AMT借記然後我不能將信用欄設置爲空白,或者當我設置信用額度欄時,我不能將借項欄設置爲空白。我是工作在下面的查詢....

WHEN Payment_Voucher_Master.grand_tot <> '0' 
    THEN '' 
    ELSE 'dgf' 
END credit 
FROM Receipt_Voucher_Master,Payment_Voucher_Master 
GROUP BY Payment_Voucher_Master.PaidTo,Payment_Voucher_Master.grand_tot 

SELECT Receipt_Voucher_Master.PaidTo 
    AS description, Receipt_Voucher_Master.grand_total as credit, 
CASE 
WHEN Receipt_Voucher_Master.grand_total <> '0' 
    THEN '' 
    ELSE 'dgf' 
END debit 
FROM Receipt_Voucher_Master,Payment_Voucher_Master 
GROUP BY Receipt_Voucher_Master.PaidTo,Receipt_Voucher_Master.grand_total; 

我想加入這兩個查詢

回答

1
declare @table1 table (
[description] varchar(20), 
paidamt money 
) 

declare @table2 table (
[description] varchar(20), 
recievedamt money 
) 

insert @table1 values 
('ele. bill',200), 
('stationary',500), 
('salary',1000) 

insert @table2 values 
('donation',1000), 
('fees',200) 

select [description],paidamt as [debit],null as [credit] from @table1 
union all 
select [description],null as [debit],recievedamt as [credit] from @table2 
+0

根據需要給出結果...讓我繼續努力..將盡快回復你..謝謝qik回覆 –

+0

我已經使用你的查詢和工作在很小的變化和日期的基礎上排序很好......沙克斯很多! –

1

這應該爲你工作,

SELECT 
    COALESCE(a.[description],b.[description]) AS [description], 
    ISNULL(a.[paidamt],'') AS debit, 
    ISNULL(b.[recievedamt],'') AS credit 
FROM [table1] AS a 
FULL OUTER JOIN [table2] AS b 
    ON a.[description] = b.[description] 
+0

給出的結果按需要...讓我工作吧..將盡快回復你..感謝qik回覆 –