SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Create union qry
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ranjeet_dutt
Starting Member

4 Posts

Posted - 04/21/2014 :  05:26:45  Show Profile  Reply with Quote
hi all, i am newbie in sql
i am using sqlceserver in my vb.net application
i want to create a query likewise:

VoucherNo VoucherDate CreditAmount DebitAmount Balance Cr/Dr
---------------------------------------------------------------------------
RC001 01/01/2014 10000.00 0.00 10000.00 Cr
IN001 01/01/2014 0.00 12000.00 2000.00 Dr
---------------------------------------------------------------------------

Receipt Table
-------------
ReceiptNo: RC001
ReceiptDate: 01/01/2014
Amount: 10000.00

Invoice Table
-------------
InvoiceNo: IN001
InvoiceDate: 01/01/2014
Amount: 12000.00

please advise code
Ranjeet

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 04/21/2014 :  06:40:15  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Haven't tested this but should be close.

;with cte1 as
(
select VoucherNo = ReceiptNo ,
VoucherDate = ReceiptDate ,
CreditAmount = 0.00 ,
DebitAmount = Amount
from Receipt
union all
select VoucherNo = InvoiceNo ,
VoucherDate = InvoiceDate ,
CreditAmount = Amount ,
DebitAmount = 0.00
from Invoice
),
cte2 as
(select *, seq = row_number() over (order by VoucherDate) from cte1)
cte3 as
(
select VoucherNo, VoucherDate, CreditAmount, DebitAmount,
Balance = CreditAmount - DebitAmount, seq = 1
from cte2 where seq = 1
union all
select cte2.VoucherNo, cte2.VoucherDate, cte2.CreditAmount, cte2.DebitAmount,
Balance = cte3.Balance + cte2.CreditAmount - cte2.DebitAmount, seq = cte3.seq + 1
from cte3,cte2 where cte3.seq + 1 = cte2.seq
)
select VoucherNo, VoucherDate, CreditAmount, DebitAmount,
Balance = case when Balance < 0 then Balance * -1 else Balance end ,
case when Balance < 0 then 'DR' else 'CR' end
from cte3
order by seq

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ranjeet_dutt
Starting Member

4 Posts

Posted - 04/21/2014 :  14:11:58  Show Profile  Reply with Quote
sir thanks for your reply but i am using sqlce, hence this code might not working coming error
Go to Top of Page

ranjeet_dutt
Starting Member

4 Posts

Posted - 04/21/2014 :  17:29:56  Show Profile  Reply with Quote
worked with sql server thanks again
Go to Top of Page

ranjeet_dutt
Starting Member

4 Posts

Posted - 04/21/2014 :  17:30:25  Show Profile  Reply with Quote
one more thing

can i add opening balance in this query?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.2 seconds. Powered By: Snitz Forums 2000