Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Average of rows, not columns

Status
Not open for further replies.

Scorez2000

Programmer
Feb 5, 2004
87
GB
This is probably easy as pie, but I don't know
how to do it.

In a database, I have a table with a bunch of
fields. All number fields are numbers. I need to pull an
average for each record of about 8 fields.

I thought the AVG function would do the trick,
but that averages everything in the column. I
need it to average the vale over a few fields for
each record.


Ok, for example, if there was Field1, Field2, Field3 and a record with the values 7, 4 and 10, then the field in the resultant query would have the value 10.

Thanks in advance.
 
This type of problem can be handled directly in SQL by temporarily normalizing the data with Unions and making all the Union queries a derived table.

Select temp.id, avg(temp.field1)
From
(
Select id, field1 from yourtable
Union
Select id, field2 from yourtable
Union
Select id, field3 from yourtable
etc....
) as temp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top