1

我有 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 设置为借方时,我无法将贷方列设置为空白,或者当我将 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;

我试图加入这两个查询

4

2 回答 2

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]
于 2012-11-30T14:48:13.083 回答
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
于 2012-11-30T14:58:32.920 回答