?
Solved

Need help using sum in a SQL query

Posted on 2013-06-20
2
Medium Priority
?
395 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
[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
2 Comments
 
LVL 11

Assisted Solution

by:Simone B
Simone B earned 1000 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 1000 total points
ID: 39263564
SELECT PaymentType ,     PaymentCode,     SUM(PaymentAmount) as PaymentTotal
FROM YourTableName
GROUP BY PaymentType, PaymentCode


..try that.
0

Featured Post

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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