Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Syntax Needed for Combining record amounnts

Posted on 2011-03-15
3
Medium Priority
?
295 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
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Desired Skill Set for Microsoft Dynamics CRM Technical Resources – Part I
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

926 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