0

最低価格と最高価格を持っている人のために、選択ステートメントにもSellerIDが必要です。以下はクエリです。どのような変更が必要か教えてください??

Select count(Id) TotalSeller,
       min(price) as MinPrice, ***SellerID_for_min_price***,
       max(price) as MaxPrice, ***SellerID_for_max_price***
  from ProdPrice
 where prodPriceId=1212

データ:

ProdId  SellerID    Price
1212    65  34740
1212    20  34855
1212    88  37299
1212    69  38490
1212    108 39990
1212    35  39999
1212    96  40990
4

1 に答える 1

0

この種のクエリを実行するに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
于 2013-06-12T16:00:06.353 に答える