Wednesday, March 28, 2012

query problem

Hi i just dont know how to start on this,
the problem is that i want to generate a result on an accounting format for a set of reciepts and payments:
The scenario is as attached below:something like

select a.account_id ,
date = a.date,
"Invoice/CollectionId" = a.id ,
Debit = case when type = 1 then right(space(20) + convert(varchar(20),a.amount),20) else '' end ,
Credit = case when type = 2 then right(space(20) + convert(varchar(20),a.amount),20) else '' end
from
(
select id = i.invoice_id, i.amount, date = i.invoice_date, i.order_id, o.account_id from invoices i, order o where o.order_id = i.order_id, type = 1
union
select id = c.collection_id, c.amount, c.collection_date, null, c.account_id, 2 from collections c
) as amount a
order by a.account_id , a.date descsql

No comments:

Post a Comment