Re: Can I do this using SQL?

From: Roy Hann <specially_at_processed.almost.meat>
Date: Fri, 10 Feb 2006 09:13:18 -0000
Message-ID: <F4CdnfcxIfIqxHHeRVnysg_at_pipex.net>


"Tony Rogerson" <tonyrogerson_at_sqlserverfaq.com> wrote in message news:dshilm$6p0$1$8302bc10_at_news.demon.co.uk...
> What, CELKO writing a cursor?
>
> I'm shocked!

Me too, but for a different reason.

Joe appears to be falling into the trap of assuming that the first N rows will give the N highest values. Admittedly the OP's question was posed in a way that invited the error, but it is still an error. The general solution ought to assume that there could be an arbitrary number of rows with the same highest value. (The Microsoft SQL TOP n WITH TIES extension does handle ties correctly, if you are using MS SQL and remember to write WITH TIES. Using a cursor and simply truncating rows after the first N does not.)

This is not a "mere" theoretical concern. About 10 years ago I thoroughly embarrassed and annoyed a group of very eminent surgeons by misreporting some morbidity and mortality statistics to the government. I had written a query that made exactly this wrong assumption. I was in very deep disgrace for a very long time. (Maybe I still am; I don't work there anymore.)

I am shocked that Joe has not provided a solution in the form of a quota query because I am pretty sure he has written at least one article on the subject, using subqueries.

Roy Received on Fri Feb 10 2006 - 10:13:18 CET

Original text of this message