Wednesday 15 February 2012

join - SQL-ex.ru #26 Selecing average from two tables -


I had a question about a SQL query on the website. The query asks:

Define the average price of PCs and laptops manufactured by A manufacturer.

The database schema is as follows:

I wrote my query in this form:

  select average (vaccine) Add Avg_Price (SELECT AVG (A.Price) as a PC from PC (A Model = Bimodel) Where Bmc = 'A' Union Select All All Laptops C As AVG (CPIC) On the product D (C model = D. model) where D. Maker = 'A') T   

The problem is that it does not return the correct answer, the average is higher than expected. Is the calculation of the average inaccurate? How do I change the query so that it gives the expected answer?

You are average in PC prices and laptops are priced differently, then on average, the average average. Your query was good, despite this you should not be an average of prices in sub queries. Just return prices to the top level at the sub-questions and average level:

  Choose from Avg_Price from Avg_Price (Select PC in PC.module where prod.Maker = 'A') Association (Select All from Laptop. Join Prod Group on PC. Model = Prod model where prod.maker = 'A')) q    

No comments:

Post a Comment