question

Bins avatar image
Bins asked

Please help urgently - BB

I have a table name 'AccVoucher' which contains the following fields: Voucher Nbr, Voucher Amt, Voucher Type(Values are 'DB'-Debit and 'CR'-Credit), Department One Voucher Nbr can have more than one values Example of records in table Voucher Nbr Voucher Amt Voucher Type Department VC10001 1000 DB Finance VC10001 850 DB Finance VC10001 750 DB Finance VC10001 100 CR Finance VC10001 2000 DB IT VC10001 500 DB IT VC10001 50 CR IT VC10001 500 DB Purchase VC10001 50 DB Purchase VC10001 100 CR Purchase I have 2 queries Query 1: select sum(Voucher Amt)dbAmt from AccVoucher where Vouch_Nbr = 'VC10001' and Voucher Type = 'DB' group by Department Query 2: select sum(Voucher Amt)crAmt from AccVoucher where Vouch_Nbr = 'VC10001' and Voucher Type = 'CR' group by Department Using these two queries i need to find the difference of dbAmt - crAmt using group by Department. Please give me single query to get this result. Result of the query should be dbAmt crAmt Difference Department 2600 100 2500 Finance 2500 50 2450 IT 550 100 450 Purchase Please help me with this .... Its urgent
sql-server-2008case-expression
7 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sp_lock avatar image sp_lock commented ·
Can I change it back, mine looked better ;-)
1 Like 1 ·
Blackhawk-17 avatar image Blackhawk-17 commented ·
OW! Shot straight into the heart!
1 Like 1 ·
sp_lock avatar image sp_lock commented ·
Formatted the post for easier read
0 Likes 0 ·
Bins avatar image Bins commented ·
Thank u so much buddy
0 Likes 0 ·
Blackhawk-17 avatar image Blackhawk-17 commented ·
@Jonlee Lockwood - guess we did it at the same time, sorry.
0 Likes 0 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
@jonlee lockwood , @blackhawk-17 now now kiddies, you play nicely together please :)
0 Likes 0 ·
sp_lock avatar image sp_lock commented ·
They should have the "Blue banner of death" that use to pop up in the old system if someone had already posted. @blackhawk-17 the formatting of the original post was fine, @Bins just didnt the code button
0 Likes 0 ·
Kevin Feasel avatar image
Kevin Feasel answered
First, I'll turn your example into insert statements, and then follow that with the query. declare @vouchers table ( VoucherNbr varchar(8), VoucherAmt int, VoucherType char(2), Department varchar(15) ); insert into @vouchers(VoucherNbr, VoucherAmt, VoucherType, Department) select 'VC10001', 1000, 'DB', 'Finance' UNION ALL select 'VC10001', 850, 'DB', 'Finance' UNION ALL select 'VC10001', 750, 'DB', 'Finance' UNION ALL select 'VC10001', 100, 'CR', 'Finance' UNION ALL select 'VC10001', 2000, 'DB', 'IT' UNION ALL select 'VC10001', 500, 'DB', 'IT' UNION ALL select 'VC10001', 50, 'CR', 'IT' UNION ALL select 'VC10001', 500, 'DB', 'Purchase' UNION ALL select 'VC10001', 50, 'DB', 'Purchase' UNION ALL select 'VC10001', 100, 'CR', 'Purchase' select VoucherNbr, Department, SUM(case when VoucherType = 'DB' then 1 * VoucherAmt else 0 end) as DebitAmount, SUM(case when VoucherType = 'CR' then 1 * VoucherAmt else 0 end) as CreditAmount, SUM(case VoucherType when 'DB' then 1 when 'CR' then -1 else 0 end * VoucherAmt) as Difference from @vouchers group by VoucherNbr, Department; The [case][1] statement allows you to differentiate between debits and credits, treating them differently. That way, you can do all of your processing in one query rather than two separate queries. [1]: http://msdn.microsoft.com/en-us/library/ms181765.aspx
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sp_lock avatar image sp_lock commented ·
IO from @Mrs_Fatherjack query Table 'AccVoucher'. Scan count 2, logical reads 4, physical reads 0 IO from @Kevin Feasel query Table 'AccVoucher'. Scan count 1, logical reads 2, physical reads 0
1 Like 1 ·
Bins avatar image Bins commented ·
Thanks a lot Mr.Kevin and Mrs_Fatherjack for ur solution. It was helpful
0 Likes 0 ·
Mrs_Fatherjack avatar image
Mrs_Fatherjack answered
Does this do it, where #Temp is your table AccVoucher: SELECT Debit.Department, Debit.dbAmt, Credit.crAmt, dbAmt - crAmt AS [Difference] FROM ( select sum(VoucherAmt)dbAmt , Department from #Temp AS T where VoucherNbr = 'VC10001' and VoucherType = 'DB' group by Department ) Debit JOIN ( select sum(VoucherAmt)crAmt , Department from #Temp AS T where VoucherNbr = 'VC10001' and VoucherType = 'CR' group by Department) Credit ON Debit.Department = Credit.Department
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sp_lock avatar image sp_lock commented ·
@mrs_fatherjack... I beaten to the post!! Spend 5 mins editing the post ha..
0 Likes 0 ·
Blackhawk-17 avatar image Blackhawk-17 commented ·
@jonlee lockwood - lost posting the answer and lost your edits... rough question for you :)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.