473,586 Members | 2,870 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_tim e - 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(TIMESTAMPDI FF(MINUTE,compl etion_time,orde r_time)) As AVG Minutes FROM Table 1, AVG(fee) As AVG Fee Where Invoice = “2”, AVG(Invoice) As AVG #Invoices
Mar 21 '07 #1
2 5801
zottty
27 New Member
I am trying to select the following averages from the tables below.
average order turn time in minutes (completion_tim e - 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(TIMESTAMPDI FF(MINUTE,compl etion_time,orde r_time)) 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
3979
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 syntax i use the MS Query Aanalyzer. following Stored Procedure is ok and does it's job fine with "EXECUTE IntDayView" CREATE PROCEDURE...
5
1721
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( field_1 test_domain, field_2 integer ); insert into test_table values( 1, 1 ); insert into test_table values( '2', '2' ); select * from test_table...
3
2307
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
16395
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 the top values box in the query page would be ideal. Is there a way to select only the top "n" values from a query? Thank You Ivan
2
13548
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 is weather data. There is a new record every 5 minutes, every day. So. What I want to do with one SQL statement is figure out the Average of...
4
6531
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 you can point me which elements to use when performing such a task, i would really appreciate it. thanks
3
9031
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 performance monitoring subsystem and can be accessed via the System.Diagnostics.PerformanceCounter object. I haven't found an obvious way to get to...
21
3235
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 #include <stdio.h> double atr (double th, double tl, double wc)
5
2145
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 3 values and then 4 values and then last 3 value and then calculate the three averages. I have written a code but I guess it is very good way to...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.