Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Return all product subcategory record that take an Average 3 days or longer to manufacture.

What I have tried:

select*
From [Production].[Product]
group by[ProductID]
having avg([DaysToManufacture])>3
Posted
Updated 1-Dec-17 3:43am

What Solution 1 is saying is that you cannot do SELECT * because you can only SELECT the columns that are in your GROUP BY statement and the ones that you SUM(field) OR COUNT(field) etc.

So, you can do something like this using a derived table:
SQL
SELECT p.*
FROM (
  -- this is your original sql here
  select productid  -- you can get product_id because you are grouping on it
  From [Production].[Product]
  group by[ProductID] 
  having avg([DaysToManufacture])>3
) x
INNER JOIN product p ON x.productid = p.productid -- on the outside, you now join back to the same table so you can get all the rest of the fields
 
Share this answer
 
That won't work: you can only return aggregate functions and group clause columns from a GROUP BY statement.
I'm not sure exactly what your data is, or what you are trying to return - so have a look at this: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]
Hopefully it will explain what you are trying to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900