I'll illustrate my problem with a fictitious example that illustrates the
kind of thing I'm trying to do. I have a table roughly like this :
Name Surname Age
======== ========= ====
Steve Brown 13.0
Steve Smith 22.0
Steve Smith 24.0
John Wilkins 22.0
John Wilkins 26.0
James Smith 24.0
Note that there are two people called Steve Smith, with different ages, and
two people called John Wilkins.
I need to generate another table that shows the average ages associated with
all the Name/Surname combinations in the original table :
Name Surname AverageAge
======== ========= ==========
Steve Brown 13.0
Steve Smith 23.0 <= Average of 22 and 24
John Wilkins 24.0 <= Average of 22 and 26
James Smith 24.0
Can anyone suggest a way to do this ? I'm stumped ...
Steve.SELECT first_name, last_name, AVG(age)
FROM Foobar
GROUP BY first_name, last_name;|||SELECT name, surname, AVG(age) AS average_age
FROM YourTable
GROUP BY name, surname
David Portas
SQL Server MVP
--|||Do:
SELECT Name, Surname, AVG( Age )
FROM tbl
GROUP BY Name, Surname
ORDER BY Name DESC;
Anith|||Hey, two replies with two minutes ! Great, many thanks. Of course, the
answer is so obvious now ... :)
S.
*** Sent via Developersdex http://www.examnotes.net ***
Wednesday, March 7, 2012
Can SQL do this ?
Labels:
database,
example,
fictitious,
illustrate,
illustrates,
microsoft,
mysql,
oracle,
roughly,
server,
sql,
table,
thekind
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment