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 を debit に設定すると、credit 列を空白に設定できないか、amt を特定の列の credit に設定すると、debit 列を空白に設定できないことです。私は次のクエリに取り組んでいました....

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つのクエリに参加しようとしていました

4

2 に答える 2

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 に答える
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 に答える