Solved

SQL Syntax Needed for Combining record amounnts

Posted on 2011-03-15
3
272 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 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Automatically creating a Trello card using data from a Microsoft Dynamics CRM record turned out to be an easy project that yielded great results.  Here's how I did this for an internal team at General Code.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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