无论是否加入2个选择查询。的记录

时间:2012-11-30 14:20:36

标签: asp.net sql-server-2005

我有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;

我试图加入这两个查询

2 个答案:

答案 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]

答案 1 :(得分: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