Reply to Re: ORDER BY AND GROUP BY CLAUSE

Your name:

Reply:


Posted by Ed Murphy on 11/28/07 05:38

bwalton_707@yahoo.com wrote:

> On Nov 27, 3:32 pm, --CELKO-- <jcelko...@earthlink.net> wrote:

>> This is the usual template for finding the latest invoice is:
>>
>> SELECT I1.invoice_nbr, C.customer_name, C.shopping_addr, C.acct_nbr
>> FROM Invoices AS I1,
>> Customers AS C
>> WHERE I1.acct_nbr = C.acct_nbr
>> AND I1.posting_date
>> = (SELECT MAX(I2.posting_date)
>> FROM Invoices AS I2
>> WHERE I2.acct_nbr = C.acct_nbr);

(Side note: Please trim quotes down to just the part that's
immediately relevant to your reply, like I've done here.)

> Thanks for the reply, unforunately your solution is not completely
> accurate because it will return multiple records
> if they have the same datetime (posted) date which is not the desired
> result.

Assuming that you want the acct_nbr's lowest invoice_nbr with the most
recent posting_date:

SELECT I1.invoice_nbr, C.customer_name, C.shopping_addr, C.acct_nbr
FROM Invoices AS I1,
Customers AS C
WHERE I1.acct_nbr = C.acct_nbr
AND I1.invoice_nbr
= (SELECT MIN(I2.invoice_nbr)
FROM Invoices AS I2
WHERE I2.acct_nbr = C.acct_nbr
AND I2.posting_date
= (SELECT MAX(I3.posting_date)
FROM Invoices AS I3
WHERE I3.acct_nbr = C.acct_nbr));

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация