select CategoryId,max(id_min_value),max(id_max_value)
from
(select CategoryId,
case when ItemValue=min(ItemValue) over (partition by CategoryId) then ItemId end as id_min_value,
case when ItemValue=max(ItemValue) over (partition by CategoryId) then ItemId end as id_max_value
from Items) Items
group by CategoryId