Solved

Need help using sum in a SQL query

Posted on 2013-06-20
2
377 Views
Last Modified: 2013-06-20
Hi Experts,
I have a table in my SQL Server 2008 db that holds payment records. My table currently stores about 250,000 records.  Some of the columns in my table are PaymentType, PaymentCode, and PaymentAmount.  I want to get a sum of the PaymentAmount based on the various PaymentType and PaymentCode groupings. Do I need to use a Case Statement?  How can I accomplish this?  Below is a sample of the raw data, and what I want the out should be.  Thanks in advance.

Raw Data Sample:
PaymentType      PaymentCode      PaymentAmount
CR            PD            $10.00            
CR            FI            $35.12
CR            FI            $23.99
CR            PD            $25.20
CR            PD            $20.00      
CR            PD            $35.00      
HE            CH            $40.00
HE            CH            $15.00


RESULT SHOULD BE:
PaymentType      PaymentCode      Total            
CR            FI            $59.11
CR            PD            $90.20
HE            CH            $55.00

Thank you,
mrotor
0
Comment
Question by:mainrotor
2 Comments
 
LVL 11

Assisted Solution

by:Simone B
Simone B earned 250 total points
ID: 39263563
Try this:

SELECT PaymentType, PaymentCode, Sum(PaymentAmount) as Total
from PaymentRecordsTable
Group by PaymentType, PaymentCode

Open in new window

0
 
LVL 25

Accepted Solution

by:
Ron Malmstead earned 250 total points
ID: 39263564
SELECT PaymentType ,     PaymentCode,     SUM(PaymentAmount) as PaymentTotal
FROM YourTableName
GROUP BY PaymentType, PaymentCode


..try that.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on t-sql 2012 10 53
migrate a SQL 2008 to 2016, 2 26
Access 2003 query lost it's only join 7 24
SQL Syntax: How to force case sensitive query? 2 20
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

7 Experts available now in Live!

Get 1:1 Help Now