この種のクエリを実行するにrow_number()
は、最小値と最大値を持つ行を識別するために使用します。
Select count(Id) TotalSeller, min(price) as MinPrice,
min(case when seqnum_min = 1 then id end) as SellerIdMin,
max(price),
min(case when seqnum_max = 1 then id end) as SellerIdMax
from (select pp.*,
row_number() over (partition by prodPriceId order by price) as seqnum_min,
row_number() over (partition by prodPriceId order by price desc) as seqnum_max
from ProdPrice pp
where prodPriceId=1212
) pp
名前を取得するには、サブクエリで結合を実行できます。
Select count(Id) TotalSeller, min(price) as MinPrice,
min(case when seqnum_min = 1 then SellerName end) as SellerNameMin,
max(price),
min(case when seqnum_max = 1 then SellerName end) as SellerNameMax
from (select pp.*, s.SellerName,
row_number() over (partition by prodPriceId order by price) as seqnum_min,
row_number() over (partition by prodPriceId order by price desc) as seqnum_max
from ProdPrice pp join
Sellers s
on pp.id = s.id
where prodPriceId=1212
) pp