Solved

SQL Syntax Needed for Combining record amounnts

Posted on 2011-03-15
3
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
Wth a little tweaking tis did the trick
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now