Forum

Posted by Yuanyuan, Feb. 28, 2022, 3:01 p.m.

#109 Why my query is wrong

with pop_product_category as(
select product_category_id,
dense_rank(product_category_id)over(order by count(order_id) desc)ranking
from orders O
inner join product_category PC
on PC.product_id = O.product_id
group by product_category_id)

select product_category_id, ranking
from pop_product_category
group by product_category_id
limit 3

Answers

The dense_rank(product_category_id)over(order by count(order_id) syntax
is not allowed.
You can use CTE to count first, then run dense_rank afterward.
SQLPad user avatar

Leon (949)

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