Solved

Results are multiplied when using SUM() and GROUP BY

Posted on 2008-10-24
15
585 Views
Last Modified: 2010-07-27
I'm attempting to run the following query on a table:

select distinct(batch_number),sum(amount),count(*) from tablename where
time > '2008-10-22' and time < '2008-10-23'
group by batch_number

It appears the results are multiplied greatly. The maximum count(*) I should get is 280, but I'm getting 1120, 560, etc. If I remove the distinct(batch_number) and group by batch_number, I get the correct totals. The problem is, I need that batch_number to know which batch has which amounts. This is for Microsoft SQL Server.
0
Comment
Question by:busydoingnothing
15 Comments
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22801949
isnt this what u need ? just remove distinct from select

select
 batch_number,
 sum(amount),
 count(*)
from tablename where
time > '2008-10-22' and time < '2008-10-23'
group by
 batch_number
0
 

Author Comment

by:busydoingnothing
ID: 22801988
I've tried that. I still get multiplied records in certain groups. I'm not sure if there's maybe other criteria that's causing it to count it several times over?
0
 
LVL 16

Expert Comment

by:Steve Krile
ID: 22803269
I just tried something similar.  I would make a couple changes to your syntax.  Try this:

Select
   batch_number,
   sum(amount),
   count(batch_number)
from
   tablename
where
   Time between '10/22/2008' and '10/23/2008'
group by
    batch_number



I have a similar table where I've done this and it works fine.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22804752
You are going to have to post your current output (and include the query used) and desired output.
0
 

Author Comment

by:busydoingnothing
ID: 22814785
Here's the query used:

select batch, sum(amount), count(batch) from incomingchecks where
timereceived between '2008-10-22' and '2008-10-23'
group by batch order by batch

The first row in the results comes back as this:

30684      828.92000      16

When I run the same query and specify the specific batch number by running this:

select fk_batch, sum(amount), count(pk_incomingcheck) from incomingchecks where
fk_batch=30684 group by fk_Batch

I get the following, correct output:

30684      207.23000      4
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22818254
Not surprising.  In the first case you are GROUPing by batch.  In the second by fk_Batch.  What do you get when you run this:

select      fk_batch,
      sum(amount),
      count(*)
from      incomingchecks
where      timereceived between '2008-10-22' and '2008-10-23'
      And fk_batch = 30684
group by
      fk_Batch

If the answer is not:
30684      207.23000      4

Than you know that the cause of the difference has to lie with the timereceived condition.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:busydoingnothing
ID: 23457906
Never figured this one out.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 23458247
I never got a response to my last comment.
0
 

Author Comment

by:busydoingnothing
ID: 23458359
That was a typo on my part. fk_batch is the actual name of the column, I just shortened up the query for posting purposes.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 23458373
So again, what do you get when you execute this:
select      fk_batch,
      sum(amount),
      count(*)
from      incomingchecks
where      timereceived between '2008-10-22' and '2008-10-23'
      And fk_batch = 30684
group by
      fk_Batch
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 23504641
I would recommend 2) Delete/no refund.
0
 

Author Comment

by:busydoingnothing
ID: 23505069
I tried acperkins' suggestion and it still multiplied. I ended up running the query again using a different datetime column in the table and that appeared to work. There was only a single date and no time in that column versus the other column, which had a date and time. I wonder if the presence of the time caused it to be multiplied by the number of unique times that were there (i.e. if there were 100 instances of 12:30 and 200 instances of 1:30, the results would be multiplied by 2 because there were two unique times).
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 125 total points
ID: 23510772
Right that is what I said:
"If the answer is not:
30684      207.23000      4

Than you know that the cause of the difference has to lie with the timereceived condition."

Incidentally, this:
"There was only a single date and no time in that column "
Is not correct.  A datetime column always has time in it.  It is just that in your particular case it was set to midnight.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help on Setting up an identical test database 17 53
combine an MS SQL string in Idera DM 9 32
Sql query 34 22
Merge Statement 3 0
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

747 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

12 Experts available now in Live!

Get 1:1 Help Now