Problem with SELECT DISTINCT on one field but show other fields

In a recent question,, a solution was provided that worked for most cases. But on further study I find that one simple case still gives duplicate answers.

This was the problem for MS SQL 2000:-

In this query, I want the field 'HotelID' to be distinct, but the other fields to be normal. How to do?
SELECT     SupServID, Title, EndDate, HotelID, Quality, SortQuality
FROM         qryPrices
WHERE     (SupServID = 2193) AND (GETDATE() < EndDate)
ORDER BY EndDate, SortQuality DESC

The solution that was accepted in the previous question is given is below, but in the attached file is a case where distinct values for the field Quality generate duplicates for HotelID. The desired output would be that ONLY distinct values of HotelID appear.  The first line of the solution below looks for distinct values of all.

SELECT DISTINCT SupServID, FromDate, Title, EndDate, HotelID, Quality, SortQuality
FROM         qryWebTourPrices po
WHERE     (EndDate =
                          (SELECT     TOP 1 pi.enddate
                            FROM          qryWebTourPrices pi
                            WHERE      po.hotelid = pi.hotelid AND (po.SupServID = 2403) AND (GETDATE() < EndDate)
                            ORDER BY pi.fromdate DESC))

Thanks for input on this!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
please check again this article :

the TOP 1 will not solve the issue if you have several rows with the same enddate.
check out the ROW_NUMBER() tricks ...
try this

            ,MAX(FromDate) AS FromDate
            ,MAX(Title) AS Title             
            ,MAX(EndDate) AS EndDate
            ,MAX(Quality) AS Quality
            ,MAX(SortQuality) AS SortQuality
FROM dbo.QryPrices(NOLOCK)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pld51Author Commented:
Thanks for these.

angellll: thanks again, I checked again, the ROW_NUMBER tricks unfortunately only applies to MS SQL 2005, not 2000.

penyCuicas: I've tested this and it works on the sample given and a few other test cases. It looks promising, please give me a day to test exhaustively and ensure it will works always. Thanks!
pld51Author Commented:
Thanks for patience. This worked perfectly on all the tests we ran. It's a solution I would never ever have thought of! Thanks for helping.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.