Solved

SQL Syntax Needed for Combining record amounnts

Posted on 2011-03-15
3
281 Views
Last Modified: 2012-05-11
I have a situtation where I need to combine the invidual records into one while also keeping records. This is for an accounting program and here is an example.

Field: TrxNumb
Field: DocAmount
Field: DebitAmt
Field: CreditAmt

Let's say we have these four records:
1001,500,150,0
1001,500,150,0
1001,500,150,0
1001,500,50,0

So the total document amount is $500 made up of 150+150+150+50.
What I am trying to accomplish with one sql statement is to list the four records plus a fifth record which would look like :
1001,500,0,500  where on this record the credit amount in the sum of the four debit amounts. It is the creation of the fifth record that is casing trouble. The common field is the TrxNumb field. The four records actually exist in the table and the fifth record is a virtual record. I need the fifth record to complete the dataset. Can this be accomplished in one SQL statement or perhaps I need to create another table from this data and use that table?
0
Comment
Question by:rwheeler23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:owaisyahya
ID: 35141683
Here is the query that might work in your case

select TrxNumb,DocAmount,DebitAmt,CreditAmt,sum(DebitAmt) as 'CreditAmt' from accounts
0
 
LVL 18

Accepted Solution

by:
Victoria Yudin earned 500 total points
ID: 35141715
How about something like this:
select TrxNumb, DocAmount, DebitAmt, CreditAmt
from YourTable --replace with yours
union all
select TrxNumb, DocAmount, sum(DebitAmt) DebitAmt, sum(CreditAmt) CreditAmt
from YourTable --replace with yours
group by TrxNumb, DocAmount
order by TrxNumb, DocAmount

Open in new window

0
 

Author Closing Comment

by:rwheeler23
ID: 35141942
Wth a little tweaking tis did the trick
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question