Skip to content
Advertisement

calculate average for group based on element value

I have a table with 2 columns: Model and Price

I know that Pilot and Civic is Honda car while Highlander, Corolla, Rav4 is Toyota. I want to calculate, say average price for Honda group (30+20)/2 and Toyota (10+40+25)/3.

Output table:

My problem is that I am NOT allowed to create new table or new column in existing table.

Thus I think somewhere in the query I need to list all condition say

Can anyone please help me to write a query?

Thank you,

Harry

Advertisement

Answer

You can use a case expression:

Some databases extend the SQL standard by allowing the use of aliases in the group by clause (eg MySQL), so you can simplify it as follows:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement