Re: Limit within groups

2009-01-07 Thread Baron Schwartz
mysql@lists.mysql.com >>Subject: Re: Limit within groups >> >>On Tue, Jan 6, 2009 at 3:13 PM, Jerry Schwartz >> wrote: >>> Each account has multiple customers, and each customer has multiple >>sales. I >>> want to get the top 20 customers for each acc

RE: Limit within groups

2009-01-07 Thread Jerry Schwartz
>-Original Message- >From: baron.schwa...@gmail.com [mailto:baron.schwa...@gmail.com] On >Behalf Of Baron Schwartz >Sent: Wednesday, January 07, 2009 9:54 AM >To: Jerry Schwartz >Cc: mysql@lists.mysql.com >Subject: Re: Limit within groups > >On Tue, Jan 6, 2009

Re: Limit within groups

2009-01-07 Thread Baron Schwartz
On Tue, Jan 6, 2009 at 3:13 PM, Jerry Schwartz wrote: > Each account has multiple customers, and each customer has multiple sales. I > want to get the top 20 customers for each account. http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ Keep reading, it ta

Re: Limit within groups

2009-01-06 Thread Phil
] On Behalf Of > >Phil > >Sent: Tuesday, January 06, 2009 3:41 PM > >To: Jerry Schwartz > >Cc: mysql@lists.mysql.com > >Subject: Re: Limit within groups > > > >How about something like > > > >select account,customer,max(total) from (

RE: Limit within groups

2009-01-06 Thread Jerry Schwartz
>-Original Message- >From: freedc@gmail.com [mailto:freedc@gmail.com] On Behalf Of >Phil >Sent: Tuesday, January 06, 2009 3:41 PM >To: Jerry Schwartz >Cc: mysql@lists.mysql.com >Subject: Re: Limit within groups > >How about something like > >

Re: Limit within groups

2009-01-06 Thread Phil
How about something like select account,customer,max(total) from (select account,customer,sum(sale_amount) as total from group by customer) as y group by account; Seems to work in my test case.. Regards Phil On Tue, Jan 6, 2009 at 3:13 PM, Jerry Schwartz wrote: > Here's a bit of business t

Limit within groups

2009-01-06 Thread Jerry Schwartz
Here's a bit of business that is baffling me, and probably shouldn't. My table looks like this: account customer sale_amount Each account has multiple customers, and each customer has multiple sales. I want to get the top 20 customers for each account. If I simply do "GROUP BY accoun