Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Single SUM() and SUM() with GROUP BY giving different results
Old 01-20-2010, 03:43 AM Single SUM() and SUM() with GROUP BY giving different results
Junior Talker

Posts: 3
Name: Benoit LeBlanc
Trades: 0
Hello All,

I'm currently working with a paradox database that was implemented before I started working at my current job at an insurance firm.

Long story short is that when I am trying to to compile a query of the debit/credit balances of all the active clients, it gives me a different balance per client if I do a query for each individual client. With a client base of a 100K and with a number of transactions over 2 million it isn't viable to do so. So here is what I do for an indiviudal client:

Code:
SELECT COUNT(Debit) as NumberOfDebits, COUNT(Credit) as NumberOfCredit, SUM(Debit) as DebitTotal, SUM(Credit) as CreditTotal 
FROM MemberTransactions 
WHERE MemberID = '####000094';
As I mentioned above, this gives the right balances for the member, but if I do the following:

Code:
SELECT MemberID,COUNT(Debit) as NumberOfDebits, COUNT(Credit) as NumberOfCredit, SUM(Debit) as DebitTotal, SUM(Credit) as CreditTotal 
FROM MemberTransactions 
GROUP BY MemberID;
It gives me both a different count and sum results for most the members in the table.

Would anyone have an idea why the sum() and count() would be different with the second query?

Thanks a lot in advance
DHDesign is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-20-2010, 04:53 AM Re: Single SUM() and SUM() with GROUP BY giving different results
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
In the first query you extract a specific userid in the second it performs an aggregate of all the users.
So your SUM is the total for ALL users (unless you only have the one user)

http://www.w3schools.com/sql/sql_groupby.asp
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-20-2010, 05:59 AM Re: Single SUM() and SUM() with GROUP BY giving different results
Junior Talker

Posts: 3
Name: Benoit LeBlanc
Trades: 0
But shouldn't the GROUP BY seperate them by memberID, as the final number in the group by query is actually lower than the single member query.
DHDesign is offline
Reply With Quote
View Public Profile
 
Old 01-20-2010, 06:51 AM Re: Single SUM() and SUM() with GROUP BY giving different results
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
and does the COUNT(column) in the GROUPBY query match the number of records returned?

Without seeing the relationships between the values returned and the table structure and data it is impossible to say definitively why.

But a COUNT(column) returns the number of rows in the dataset returned by the query.

So if memberid nnn had 50 rows in the database the count(debits) would return 50 whereas a GROUPBY would aggregate the rows into groups so there may NOT be 50 rows in the dataset.

Assuming the Paradox SQL engine is standardised.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-21-2010, 02:41 AM Re: Single SUM() and SUM() with GROUP BY giving different results
Junior Talker

Posts: 3
Name: Benoit LeBlanc
Trades: 0
Here is the table structure so you can understand what I have to work with and what I want to
accomplish. Every row is a single transaction with either a debit or a credit to the member's
account. So what I want to do is sum up every debit and credit into a single cell for each respectively for every member. So that is why I was the group by, thinking that it would add up
every credit and debit for every member, but as you pointed out it won't do that. So how would I go about that. I've tried to do an outer join on the membernr from the member details, but I still need to group by which gives me the same result in the end


Table Structure:


Code:
 
 PeriodNr                  I
 EffectiveDate           D
 Entrynr                        +
 MemberNr                A
 Date                            D
 JournalNr                 A
 ReferenceNr             A
 DtAmount                N
 CtAmount                N
 Narration                  A
 ModifyUserId            A
 ModifStamp             @
One thing I did notice is that after I run my query


Code:
 

 SELECT COUNT(A.CtAmount) as CreditCount, Sum(A.CtAmount) as Credit, COUNT(A.DtAmount) as DebitCount , SUM(DtAmount) as Debit, M.MemberNr, M.Premium  
 FROM MemAcc as A  
 LEFT OUTER JOIN Member as M on A.MemberNr = M.MemberNr  
 GROUP BY M.MemberNr, M.Premium;

There is a single row at top with no MemberNr and a significantly high number of counts, debit
and credit. Much higher than any account should be, so I'm guessing for some reason that the
missing transactions are going into this row for some reason.


For an example, if I uniquely query lets say member X, I get a debit and credit of 3094 and debit count of 55 and credit count of 18 which matches with the number of records that are in the table for that member, but when I run the above query I get a credit count of 2, debit count of 19, credit of 1590 and debit of 2090.


So I am stumped. I don't know if this is a Paradox problem, or rather my inept understanding of SQL.


Oh yeah the blank member has a credit count of 273, debit count of 341, credit of 19030 and debit of 17168.


Thanks again for the help that you have already given me.

Last edited by DHDesign; 01-21-2010 at 03:06 AM..
DHDesign is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Single SUM() and SUM() with GROUP BY giving different results
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.60338 seconds with 12 queries