473,508 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Select Average Values

1 New Member
I am trying to select the following averages from the tables below.
average order turn time in minutes (completion_time - order_time), average fee of the final invoice, and the average # of invoices based on the following two tables:

row_id doc_id order_time completion_time
----------- -------------------- ------------------------------------------- -------------
1 Doc1 2007-03-20 10:32:07.697 2007-03-20 1:44:20.697
2 Doc2 2007-03-20 10:41:33.243 2007-03-20 1:47:06.243
3 Doc3 2007-03-20 11:06:33.260 2007-03-20 1:48:46.260
4 Doc4 2007-03-20 11:14:53.260 NULL



row_id doc_id Invoice fee
----------- -------------------- ----------- ---------------------
1 Doc1 1 .0000
2 Doc1 2 200.0000
3 Doc2 1 215.0000
4 Doc3 1 222.0000

I need the results to look like:
AVG Minutes AVG Fee AVG #Invoices
__________ _________ _____________
X Y Z

I am struggling. All I've come up with is:

SELECT AVG(TIMESTAMPDIFF(MINUTE,completion_time,order_tim e)) As AVG Minutes FROM Table 1, AVG(fee) As AVG Fee Where Invoice = “2”, AVG(Invoice) As AVG #Invoices
Mar 21 '07 #1
2 5797
zottty
27 New Member
I am trying to select the following averages from the tables below.
average order turn time in minutes (completion_time - order_time), average fee of the final invoice, and the average # of invoices based on the following two tables:

row_id doc_id order_time completion_time
----------- -------------------- ------------------------------------------- -------------
1 Doc1 2007-03-20 10:32:07.697 2007-03-20 1:44:20.697
2 Doc2 2007-03-20 10:41:33.243 2007-03-20 1:47:06.243
3 Doc3 2007-03-20 11:06:33.260 2007-03-20 1:48:46.260
4 Doc4 2007-03-20 11:14:53.260 NULL



row_id doc_id Invoice fee
----------- -------------------- ----------- ---------------------
1 Doc1 1 .0000
2 Doc1 2 200.0000
3 Doc2 1 215.0000
4 Doc3 1 222.0000

I need the results to look like:
AVG Minutes AVG Fee AVG #Invoices
__________ _________ _____________
X Y Z

I am struggling. All I've come up with is:

SELECT AVG(TIMESTAMPDIFF(MINUTE,completion_time,order_tim e)) As AVG Minutes FROM Table 1, AVG(fee) As AVG Fee Where Invoice = “2”, AVG(Invoice) As AVG #Invoices
I don't really understand the 3rd,average # of invoices part, but the following query is probably a good starting point:

Expand|Select|Wrap|Line Numbers
  1.  SELECT 
  2. AVG(TIMESTAMPDIFF(MINUTE,completion_time,order_time)) As AVGMinutes 
  3. AVG(fee) as AVGFee
  4. FROM Table1 
  5. LEFT JOIN Table2
  6. USING(doc_id)
  7.  
I guess if you want to get the # of invoces, you probably need to break down/group your query and use nested selects/stored procedures.

z
Mar 22 '07 #2
Michele
4 New Member
Hi,

If I'm understanding this right, I think you might get into trouble joining the two tables on doc ID because you have a many to one relationship (e.g. Doc1 is listed two times in the second table) and that's going to screw up your aggregations.

What you want to do is aggregate table 1 and then aggregate table 2 and then join the results. The easiest way to do this is by creating two intermediate tables and then joining them. You can also used subqueries and derived tables, but you probably want to check your sums at each step so I'd make some tables (at least at the first attempt).

Are you also looking for a count(*) for the number of invoices? (e.g. sum the invoice amount for the group and then divide by count(*) to get the average invoice amount.)

Michele
Mar 22 '07 #3

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

Similar topics

3
3971
by: Jan Schmidt | last post by:
Hi NG, i've got a mystic problem i can't solve, perhaps one of you has a good idea... i will explain with some code, so fo better understanding my probroblem: i'll use asp, but for testing the...
5
1717
by: Robert Creager | last post by:
I'm using 7.4b4 with domains, and am having a problem with selecting without casting. create domain test_domain as integer check( (value notnull) and (value >= 1) ); create table test_table(...
3
2301
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
3
16387
by: racquetballguy | last post by:
Hi I wish to have a user enter a parameter into a form for the number of rows to use to do an average. This form is based upon a query. SELECT TOP does not support parameters. Something like...
2
13537
by: chrisale | last post by:
Hi All, I've been racking my brain trying to figure out some sort of Sub-Select mySQL statement that will create a result with multiple rows of averaged values over a years time. What I have...
4
6524
by: gaga | last post by:
hi guys, a part of my program requires me to calculate an average of items that are sold. the easiest way to do that would be writing a function, but im having trouble making up the parameters. if...
3
9028
by: Frank Rizzo | last post by:
I am trying to do some monitoring of some PerfMon counters and have a question. Does PerfMon figure out the Minimum, Maximum, Average values for each counter? Or are those values part of the...
21
3218
by: Bill Cunningham | last post by:
I have create these 2 files. Called main.c and atr.c. They seem to work pretty well. I just wanted to submit them to see what if any errors others that know more might find. Thanks. atr.c ...
5
2137
by: kumarboston | last post by:
Hi all, I was trying to calculate the average value from different parts of the same data file. For example, if suppose we have number 1 - 10 and i was trying to calculate the average of only first...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
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
7401
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...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.