这对SQL来说还很新-帮助将是非常感激的。我有一张表Region,Month,MemberID,和Sales(每个成员多次交易)。我只想根据每个地区每月的销售总额提取前两名成员....所以本质上是:' q! Q! Q6 j8 ]# M; Q0 h5 `9 d
Region Month MemberID Sales----------------------------------------- 1/1/2013 A $2000 2 2/1/2013B $300 1/1/2013 A $100 1/1/2013 B $50 1/1/2013D $500 2 2/1/2013 C $200成为: , _! Y& ]7 e; L8 f& U9 u- Y5 YRegion Month Member ID Sales----------------------------------------- 1/1/2013 A $300 1/1/2013 B $50 2 1/1/2013 D $500 2 1/1/2013B $200最后会有10个地区,我想每个月按会员排名前五。5 K( W; \! s/ N7 \) n
+ v1 s) d* A; i5 d$ H 解决方案: : Q- B; G; Y% Z8 f/ g 您可以执行以下操作row_number():4 V* C1 U0 y+ S
select region,month,MemberId,salesfrom (select region,month,MemberId,sum(sales) as sales row_number() over (partition by region,month order by sum(sales) desc) as seqnum from table t group by region,month,MemberId ) twhere seqnum <= 2;