473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Order by aggregate function

aas4mis
97
I need to order the following by sumofrate.

Expand|Select|Wrap|Line Numbers
  1. SELECT [name], sum([pieces]) AS sumofpieces, sum([hours]) AS sumofhours, sum([pieces])/sum([hours]) AS sumofrate, sum([errors]) AS sumoferrors, [department]
  2. FROM tblTempRate
  3. WHERE name in (SELECT name 
  4.                FROM tblTempRate 
  5.                GROUP BY name, department 
  6.                HAVING sum(hours)>4)
  7. GROUP BY [name], [department]
Whenever I add "ORDER BY sum([pieces])/sum([hours])" to the end of the statement I get an "overflow" error. I've tried to change my sumofrate in the SELECT and ORDER BY clauses to "sum(nz([pieces],0))/sum(nz([hours],0))" in case this was some type of NULL error, but no go. Both pieces and hours are of type double, the only thing I can think of is too many decimal places in some of the results. Any ideas?

Help would be much appreciated. Thanks!!
Jun 14 '10 #1

✓ answered by jimatqsi

Don't forget, you can't divide by zero. Even if you're testing on sum(hours) and only accepting the ones >4.

Add .0001 to your divisor just to be sure you don't fall into that trap. But I don't think that would give you an overflow error, so you may have another problem.

Code out the divide by zero problem and then see if you are still getting the overflow error. If you are, make some tests to narrow down the problem. Change the sum([pieces]) to 1 or some other constant and see if the problem still happens. Then change it back and change sum([hours]) to 1 or some other constant and see if you get the problem.

If you think the scale is the problem you could put the division into a Round() statement to control the number of decimal places returned.

Jim

4 2023
jimatqsi
1,271 Expert 1GB
Don't forget, you can't divide by zero. Even if you're testing on sum(hours) and only accepting the ones >4.

Add .0001 to your divisor just to be sure you don't fall into that trap. But I don't think that would give you an overflow error, so you may have another problem.

Code out the divide by zero problem and then see if you are still getting the overflow error. If you are, make some tests to narrow down the problem. Change the sum([pieces]) to 1 or some other constant and see if the problem still happens. Then change it back and change sum([hours]) to 1 or some other constant and see if you get the problem.

If you think the scale is the problem you could put the division into a Round() statement to control the number of decimal places returned.

Jim
Jun 14 '10 #2
aas4mis
97
Well, if that isn't karma I don't know what is. Jim, nice to hear from you again, just replied to you this morning. :)

Thanks for leading me in the right direction. It was a division by zero problem. Here's the correct code.

Expand|Select|Wrap|Line Numbers
  1. SELECT [name], sum([pieces]) AS sumofpieces, sum([hours]) AS sumofhours, sum([pieces])/IIf(sum([hours])=0,0.001,sum([hours])) AS sumofrate, sum([errors]) AS sumoferrors, [department]
  2. FROM tblTempRate
  3. WHERE name in (SELECT name                             
  4.                FROM tblTempRate
  5.                GROUP BY name, department
  6.                HAVING sum(hours)>4)
  7. GROUP BY [name], [department]
  8. ORDER BY sum([pieces])/IIf(sum([hours])=0,0.001,sum([hours])) DESC;
  9.  
Once again the scripts,.. err.. bytes.com solved the problem.
Jun 14 '10 #3
jimatqsi
1,271 Expert 1GB
Great, glad to be of help. However, I'm not sure you want to substitute a value of .001 for a zero in your equation. It works mathematically, but probably not logically.

Perhaps you want instead a 1. I mean, what happens if you divide your number of pieces by that very small value? You get a very large value in return, and that may skew your dataset. Or maybe it's okay like that, I can't know.

Jim
Jun 14 '10 #4
aas4mis
97
@jimatqsi
Good catch. Didn't notice because any time hours is 0, pieces will be 0. But changed to divide by 1, just for good practice.
Jun 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Claudio Lapidus | last post by:
Hello I would like to know how can I define/create a new aggregate function. I need a custom function that operate on a set of text strings and return a certain string aggregate based on certain...
1
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT...
6
by: Larry Menard | last post by:
Folks, I know that DB2 does not (yet?) support this, but I wonder if anyone can suggest a work-around. I've seen article...
12
by: Bill Moran | last post by:
Hey all. I've hit an SQL problem that I'm a bit mystified by. I have two different questions regarding this problem: why? and how do I work around it? The following query: SELECT GCP.id,...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
1
by: R.A.M. | last post by:
Hello, I am learning SQL Server 2005. I have (correctly) written in .NET assembly DemoSQLServer with aggregate function AvgNoMinMax in class Demo and I have added assembly to database...
2
by: rdemyan via AccessMonster.com | last post by:
I can't seem to get the following SQL statement with the ORDER BY clause to work. It does work if I order by any of the fields in the SELECT part of the statement but not if it is the field in the...
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
1
by: Sandro997 | last post by:
Ok. I have a bit of a dilemma here. First, please consider the following function: CREATE OR REPLACE FUNCTION recent_lab(text, labs, treatments) RETURNS float8 AS $BODY$select...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.