Forum

Posted by Yuanyuan, March 1, 2022, 9:21 p.m.

#111 Having

with category_revenue as(
select PC.product_category_id, O.product_id, sum(qty)quantities,
sum(qty * unit_price_usd)as revenue,
dense_rank() over (partition by product_category_id order by sum(qty * unit_price_usd) desc)as ranking
FROM product_category PC
inner join orders O
on O.product_id = PC.product_id
group by PC.product_category_id, O.product_id
having quantities >= 2)
select product_category_id, product_id, revenue, ranking
from category_revenue
where ranking = 2
Can I add quantities--the new definedd aggregation column name after having?

Answers

MySQL allows it but not Postgres.
For Postgres, you can either use CTE or subquery so you can refer to the newly generated column in your later queries.
SQLPad user avatar

Leon (949)

March 4, 2022, 2:44 p.m.