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
2 5801
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: - SELECT
-
AVG(TIMESTAMPDIFF(MINUTE,completion_time,order_time)) As AVGMinutes
-
AVG(fee) as AVGFee
-
FROM Table1
-
LEFT JOIN Table2
-
USING(doc_id)
-
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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.
|
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
|
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...
| |
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
|
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...
|
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)
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |