简体   繁体   中英

selecting a column greater than a specified value

This is my current code:

select distinct categoryid, min(weight), max(weight), round(avg(weight), 2) as AVG_WEIGHT
from inventorypart
where categoryid is not null
group by categoryid;

I need to select the categoryid that has the avg weight greater than 4.

i have tried the following with no sucess

where categoryid is not null and avg(weight) > 4

The WHERE keyword cannot be used with aggregate functions, as WHERE is applied before GROUP BY. Use HAVING instead:

SELECT DISTINCT categoryid, MIN(weight), MAX(weight), ROUND(AVG(weight), 2) AS AVG_WEIGHT
FROM inventorypart
WHERE categoryid IS NOT NULL
GROUP BY categoryid
HAVING AVG(weight) > 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM