SQL - Sum of Cash based on Customer ID

Posted on 2013-10-23
Last Modified: 2013-10-31
To my dearest experts,

I've searched the KB, but can't locate a basic version of what I'm looking for.  

I have a Transaction table (ActivityHist) that show's bank activity.  What I need is (hopefully) a rather simple solution.  I want to see a list of Transactions (TranNo) on a specific Date (BookDate) broken down by Customer (CustID) with a sum of activity (BaseAmt) of 10000.01 or higher.  

All data fields are in the same table, no joins and such required.

Any and all help would be greatly appreciated.

Question by:NCollinsBBP
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
LVL 65

Accepted Solution

Jim Horn earned 500 total points
ID: 39594036
<air code>  Let's start with this..
Declare @dt date = '2013-10-01'

SELECT CustID, TranNo, Sum(BaseAmt) as BaseAmtSum
FROM ActivityHist
HAVING Sum(BaseAmt) > 10000

Open in new window

Q:  Does the transactions make up the sum, where you wish to apply the criteria of 'greater than ten grand'?

btw I wrote an article on SQL Server GROUP BY Solutions that would be a good read.
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39594582
You will need a JOIN, since you have to determine the total while also listing the details.

Below is as close as I get with the info so far.  I used ABS(BaseAmt) in the SUM() so that credits/debits would not cancel each other out, as either/both are activity on the account.  If that's not correct, naturally remove the ABS() :-) .

DECLARE @BookDate date -- chg data type to to exactly match the data type on the table
DECLARE @trans_total decimal(9, 2) -- chg data type to to exactly match the data type on the table

SET @BookDate = '20131022'
SET @trans_total = 10000.01

FROM dbo.ActivityHist ah
    -- check for any customer that meets/exceeds the desired total of value of all trans
    FROM dbo.ActivityHist
        BookDate = @BookDate
    HAVING SUM(ABS(BaseAmt)) >= @trans_total
) AS ah_total ON
    ah_total.CustID = ah.CustID
    ah.BookDate = @BookDate
    ah.CustID, ah.TranNo

Author Closing Comment

ID: 39614183
This was the first solution provided and it worked perfectly. Thank you!

(Sorry for the late reply... had some massive hardware / server issues that bled into multiple days... this became a low priority).  

LVL 65

Expert Comment

by:Jim Horn
ID: 39614218
Thanks for the grade.  Good luck with your project.  -Jim

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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