SQLFriend
3/29/2007 5:50:00 PM
This worked great, Thanks Roy
"Roy Harvey" wrote:
> SELECT Cust, InvoiceDate
> FROM Invoices as A
> WHERE InvoiceDate IN
> (SELECT TOP 4 B.InvoiceDate
> FROM Invoices as B
> WHERE A.Cust = B.Cust
> ORDER BY B.InvoiceDate Desc)
>
> The one ambiguity in the above is when there are multiple invoices for
> a date. If the desire is to treat all the invoices with the same
> customer and date as one, then add DISTINCT to both the outer query
> and the subquery. If you want to treat them some other way please
> specify.
>
> Roy Harvey
> Beacon Falls, CT
>
> On Thu, 29 Mar 2007 08:44:07 -0700, SQLFriend
> <SQLFriend@discussions.microsoft.com> wrote:
>
> >I would like to get the 4 max dates (largest) per customer, any idea how to
> >do it?
> >For exapmle:
> >Cust Invoice Date
> >1 1/1/06
> >1 2/1/06
> >1 3/1/06
> >1 4/1/06
> >1 5/1/06
> >1 6/1/06
> >1 7/1/06
> >2 1/1/06
> >2 2/1/06
> >2 3/1/06
> >2 4/1/06
> >2 5/1/06
> >2 6/1/06
> >2 7/1/06
> >I would like to run a query to get invoices from Apr to Jul for Cust 1 and 2
> >and should always give the the last 4 invoices anytime I run it.
> >
> >Thanks in advance
>