Skip to content
Advertisement

Display average value of parent values next to each sub value

SQL Fiddle

In the table I have different campaigns and corresponding campaing_categories.
Now I want to get the average_sales_price per Campaign_Category and display this it next to each Campaign_ID.
The expected result should look like this:

With the following query I only get the average_sales_price per Campaign_ID:

How do I have to modify my query to display the average_sales_price of the corresponding Campaign_Category next to each Campaign_ID?

Advertisement

Answer

You can use correlated sub-query :

Here is SQL Fiddle.

Note : Do not use column position in GROUP/ORDER BY clause, if you change the column position in SELECT statement then query will fail to generate expected result. So, all qualify column name instead.

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