要执行此类查询,您希望使用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