1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| select * from
(select B.Department_ID
, D.Dept_Description
, B.Item_ID
, I.Item_Description
, count(*)
, dense_rank()
over(partition by B.Department_ID, D.Dept_Description
order by count(*) desc
) as RK
from tbl_Bakery_Order_Facts B
inner join tbl_Departments D
on D.departement_id=B.departement_id
inner join tbl_Items I
on I.Item_ID=B.Item_ID
group by D.departement_id
, D.dept_Description
, B.Item_ID
, I.Item_Description
order by B.Departement_ID
, count(*) desc
) as sub
where sub.RK < 3 |