473,473 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning average of multiple rows in a table join

Hi,

I'm am looking for a little help. I need to create a SQL view which
joins a few tables, and I need to return an average for a particular
column where a second duplicate ID exists...

Heres an example of how the results could be returned...
ID | Name | Order No. | Value
---+------+-----------+---------
5 | test | 1234 | 3
5 | test2| 1234 | 4
5 | test3| 1234 | 3
5 | void | 1235 | 5
5 | void2| 1235 | 6
5 | void3| 1235 | 5
5 | void4| 1235 | 7

ID is my main join which joins the tables

Name is a unique name

Order No is the same for the different names, I only need to return one
row with this order no, and the first name (the rest are irrelevant)

Value is the field which I wish to return as an average of all 3, 4 or
however many rows is returned and share the same order no. This is
where I get totally lost as I am pretty new to SQL. Can anyone provide
any help on how I would go about limiting this query to the unique
order no's and returning the average of the value field, and I can take
it from there with my own tables.

Thanks for your help

str8

Jul 23 '05 #1
3 9977
Something like this for example?

SELECT order_no, MIN(name), AVG(value)
FROM YourTable
GROUP BY order_no

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
I can't get that to work unless I only try to select the values with
the aggregates, although i don't think this is entirely what I'm
after....what I'm need to do is, based on my initial view...

ID | Name | Order No. | Value
---+------+-----------+-------*--
5 | test | 1234 | 3
5 | test2| 1234 | 4
5 | test3| 1234 | 3
5 | void | 1235 | 5
5 | void2| 1235 | 6
5 | void3| 1235 | 5
5 | void4| 1235 | 7

return something more like this...

ID | Name | Order No. | Value
---+------+-----------+-------*--
5 | test | 1234 | 3.33
5 | void | 1235 | 5.75

So for each unique order no, I return the details in the first row,
except value column, where I return the average of all values in all
rows which share the same order no.

I hope this makes sense? Thanks for your help so far!

str8

Jul 23 '05 #3
Based on what you posted it seems like I just left out one column:

SELECT id, order_no, MIN(name), AVG(value)
FROM YourTable
GROUP BY id, order_no

but maybe there could be more than one ID per order_no? That's why it
helps if you can include DDL (CREATE TABLE statements including keys
and constraints) with your questions so that we don't have to guess at
the keys and dependencies in your data.

Maybe you also have some other columns you didn't tell us about. To
answer you properly we'll need a better explanation of what you mean by
the "first" row for the order no. Tables have no fixed concept of
order. A table is an unordered set. So you need some other column or
columns that unambiguously identifies which row comes first. For
example:

SELECT id, order_no, /* ... other columns */,
(SELECT AVG(value)
FROM YourTable
WHERE order_no = T.order_no)
FROM YourTable AS T
WHERE ord =
(SELECT MIN(ord)
FROM YourTable
WHERE order_no = T.order_no)

The following article explains the best way to get help with a problem
such as this:
http://www.aspfaq.com/etiquett*e.asp?id=5006

Hope this helps.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stephen Miller | last post by:
Firstly, sorry for the long post, but I've included a fair bit of sample data. Im doing a comparision of 10yr Bond prices and CPI adjustments, with an 18 week moving average of the CPI. I'm...
9
by: Rowland Hills | last post by:
I have a table which is returning inconsistent results when I query it! In query analyzer: If I do "SELECT * FROM TABLE_NAME" I get no rows returned. If I do "SELECT COL1, COL2 FROM...
6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
1
by: Justin | last post by:
Hi, In the process of localizing the 'regions' table, we added three new tables. The localized data will be stored in the TokenKeys and TokenValues tables. It would be easier if we did away with...
2
by: nicolec | last post by:
I've been tearing my hair out over this UDF. The code works within a stored procedure and also run ad-hoc against the database, but does not run properly within my UDF. We've been using the SP,...
4
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
3
by: jenner4000 | last post by:
Hi, My first post in this group, so hope some of you can help me. My table: "answer" answerID answer(int) questionID(int) userID(int)
20
by: p175 | last post by:
Hi people, I have a stored procedure that creates many Global temporary session tables. Into each of these tables go the results of various processing using relational division all keyed and...
6
nathj
by: nathj | last post by:
Hi, I've been puzzling out the problem below for several hours and it's driving me mad. I know there is a way to do what I want I just can't figure it out. Here's the situation. I have the...
0
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
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,...
1
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...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.