Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

SQL - Sum of Cash based on Customer ID

Posted on 2013-10-23
Medium Priority
212 Views
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.

-Nick
0
Question by:NCollinsBBP
[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
4 Comments

LVL 66

Accepted Solution

Jim Horn earned 2000 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
GROUP BY CustID, TranNo
HAVING Sum(BaseAmt) > 10000
``````
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.
0

LVL 70

Expert Comment

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

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

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).

-Nick
0

LVL 66

Expert Comment

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

Featured Post

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses
Course of the Month6 days, 3 hours left to enroll

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.