Durchschnitt mit AVG berechnen

USE PUBS
SELECT CAST(title AS CHAR(65)) AS title
, type
, (SELECT AVG(price)
FROM
(SELECT TOP 3 price
FROM titles
ORDER BY price DESC)
t) AS average_price
FROM titles

title type average_price
----------------------------------------------------------------- ------------ ---------------------
The Busy Executive's Database Guide business 21.8300
Cooking with Computers: Surreptitious Balance Sheets business 21.8300
You Can Combat Computer Stress! business 21.8300
Straight Talk About Computers business 21.8300
Silicon Valley Gastronomic Treats mod_cook 21.8300
The Gourmet Microwave mod_cook 21.8300
The Psychology of Computer Cooking UNDECIDED 21.8300
But Is It User Friendly? popular_comp 21.8300
Secrets of Silicon Valley popular_comp 21.8300
Net Etiquette popular_comp 21.8300
Computer Phobic AND Non-Phobic Individuals: Behavior Variations psychology 21.8300
Is Anger the Enemy? psychology 21.8300
Life Without Fear psychology 21.8300
Prolonged Data Deprivation: Four Case Studies psychology 21.8300
Emotional Security: A New Algorithm psychology 21.8300
Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean trad_cook 21.8300
Fifty Years in Buckingham Palace Kitchens trad_cook 21.8300
Sushi, Anyone? trad_cook 21.8300

(18 row(s) affected)

Das Beispiel macht zwar nicht übermässig viel Sinn, verdeutlicht aber gut den dahinterstehenden Grundgedanken.

Noch keine Bewertungen