It has been a while since we have seen a puzzle so let us do a small puzzle today. Actually, this is a very simple puzzle if you have worked with SQL Server for a while. I got an email from one of the readers from the blog post and he wanted me to correct the following query which he had written. Well, I have been working with SQL Server for over 19 years and I often do not remember the syntax and have to look up online. Here is a very simple puzzle for you which involves Aggregate and Subquery.
Important Update: The Workshop is now over and it was one of the landmark workshops, where I shared business secrets of successful Performance Tuning Expert. You can watch the recording of the workshop for a limited period of time. |
Puzzle – Aggregate and Subquery
Here is the query when we run against the AdventureWorks database, it gives us an error. Remember AdventureWorks database is a sample database and you can download and install AdventureWorks over here.
USE AdventureWorks2014 GO SELECT pch.StandardCost, p.ProductID FROM Production.ProductCostHistory pch INNER JOIN Production.Product p ON pch.ProductID = p.ProductID WHERE pch.StandardCost > AVG(p.StandardCost) GO
Now when you run the above query, it gives us the following error:
Msg 147, Level 15, State 1, Line 17
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Now here is your task, you need to help me to fix the above query. The hint to resolve the query is already in the blog post’s title and in the query error. You need to understand what my query is attempting to do and use the hint which is already pointed out to you. After you successfully fix the query it will produce the following results.
Rules
Well, there are no real rules, as long as you modify my query to get me above results. Please post your query in the comments section below before April 22, 2019. I will keep all the answers hidden till April 22, 2019 11:59:00 PM.
Winner
One Winner will get 1 free seat for my upcoming class, which is worth USD 1000.
SQL Server Performance Tuning Practical Workshop for EVERYONE
Date:Â April 23, 2019, Tuesday
8:00 AM Pacific Time | 11:00 AM Eastern Time
If you do not want to take part in the contest, you can directly register for the class here.
Update: April 23, 2019
The contest is closed and the winning Answer is here:
USE AdventureWorks2014 GO SELECT pch.ProductID, pch.StartDate,pch.EndDate, pch.StandardCost, pch.ModifiedDate FROM Production.ProductCostHistory pch INNER JOIN Production.Product p ON pch.ProductID = p.ProductID WHERE pch.StandardCost > p.StandardCost GO
There is no need for the AVG on the StandardCost as there will be always a single entry for the product. Even if you change the query with group by + having you will still see the query uses the simple join.
Congratulations to the winner Nicholas Månsson. He is very excited to join the SQL Server Performance Tuning Practical Workshop for EVERYONE.
Everyone who participated you got to solve this difficult puzzle so indirectly you are a winner (of knowledge) as well. This was an intense competition with over 500 comments.
Reference:Â Pinal Dave (https://blog.sqlauthority.com)
527 Comments. Leave new
SELECT p.ProductID,
pch.StartDate,
pch.EndDate,
pch.StandardCost,
pch.ModifiedDate
FROM Production.ProductCostHistory pch
INNER JOIN (
SELECT p.ProductID,
p.StandardCost
FROM Production.Product p
GROUP BY
p.ProductID, p.StandardCost
) AS p
ON p.ProductID = pch.ProductID
WHERE pch.StandardCost > p.StandardCost
SELECT p.ProductID,
pch.StartDate,
pch.EndDate,
pch.StandardCost,
pch.ModifiedDate
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p
ON p.ProductID = pch.ProductID
WHERE pch.StandardCost > p.StandardCost
SELECT pch.ProductID, pch.StartDate,MAX(pch.StandardCost) as StandardCost, pch.ModifiedDate
FROM Production.ProductCostHistory pch
GROUP BY pch.ProductID,pch.StartDate,pch.EndDate,pch.ModifiedDate
HAVING MAX(pch.StandardCost)>(select p.StandardCost from Production.Product p where p.ProductID=pch.ProductID )
The interesting moment that the query doesn’t need any aggregation to get the wanted result. Just remove AVG() from query and you get these 6 rows:
SELECT pch.*
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > p.StandardCost
it shoul be having instead of where
Post complete query.
> (Select avg(p.standardcost) from production.product p where p.productid = ph.productid)
USE AdventureWorks2014
GO
SELECT pch.StandardCost, p.ProductID
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > (select avg(p.StandardCost) from Production.Product)
GO
Hi,
Please find the query below.
SELECT sub.ProductID
, sub.StartDate
, sub.EndDate
, sub.StandardCost
, sub.ModifiedDate
FROM (
SELECT pch.ProductID
, pch.StartDate
, pch.EndDate
, pch.StandardCost
, pch.ModifiedDate
, AVG(pch.StandardCost) OVER (ORDER BY p.ProductID) avgCost
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
)sub
WHERE StandardCost > avgCost
USE AdventureWorks2017
GO
SELECT
pch.*
FROM Production.ProductCostHistory pch
WHERE pch.StandardCost > (SELECT
AVG(P.StandardCost)
FROM Production.Product P
WHERE P.ProductID = pch.ProductID)
GO
USE AdventureWorks2017
GO
SELECT
pch.*
FROM Production.ProductCostHistory pch
INNER JOIN (SELECT
ProductID
,AVG(StandardCost) StandardCost
FROM Production.Product
GROUP BY ProductID) p
ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > p.StandardCost
GO
Here is my attempt:
Select pch.*
From Production.ProductCostHistory pch
inner join Production.Product p
ON pch.ProductID = p.ProductID
and pch.StandardCost > p.StandardCost
SELECT pch.StandardCost, p.ProductID
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > (SELECT AVG(p2.StandardCost)
FROM Production.Product p2
WHERE p2.ProductID = pch.ProductID)
USE AdventureWorks2014
GO
SELECT p.ProductID, StartDate, EndDate, pch.StandardCost, ModifiedDate
FROM Production.ProductCostHistory pch
INNER JOIN ( SELECT AVG(StandardCost) avg_standardcost, ProductID FROM Production.Product group by ProductID) p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > avg_standardcost
GO
I would use a CTE to get the Ave of p.StandardCost and then use the CTE value in the Where clause.
That is another idea.
Hi Pinal
Here is the modified query based on teh problem statement.
SELECT pch.StandardCost, p.ProductID
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > (SELECT DISTINCT StandardCost FROM Production.Product WHERE pch.ProductID = ProductID)
SELECT Â p.ProductID, pch.StartDate,EndDate,pch.StandardCost,pch.ModifiedDate
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > (Select AVG(StandardCost) from Production.Product where ProductID=P.ProductID )
USE AdventureWorks2014
GO
SELECT pch.*
FROM Production.ProductCostHistory pch
INNER JOIN
(SELECT ProductId, AVG(p.StandardCost) AS AvgStandardCost
FROM Production.Product p
GROUP BY productid) p ON Pch.ProductID = p.ProductID
AND pch.StandardCost > p.AvgStandardCost
GO
Please review it. Excited to get a seat for performance tunning.
The valid answer is announced.
Select pch.* from
(
select StandardCost = AVG(p.StandardCost),ProductID from Production.Product p
group by productid ) as p
join Production.ProductCostHistory pch ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > p.StandardCost
USE AdventureWorks2014
GO
SELECT pch.StandardCost, p.ProductID
FROM Production.ProductCostHistory pch
INNER JOIN (SELECT ProductID, AVG(StandardCost) StandardCost FROM Production.Product GROUP BY ProductID) p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > p.StandardCost
GO
USE AdventureWorks2017
GO
SELECT p.ProductID, startdate, enddate, pch.StandardCost, pch.modifieddate
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
GROUP by p.ProductID, startdate, enddate, pch.StandardCost, pch.modifieddate
HAVING pch.StandardCost > AVG(p.StandardCost)
GO