This is a discussion on Re: DISTINCT ROW problem within the SQL Server forums, part of the Microsoft SQL Server category; --> SkunkDave (dave_casserly@totalise.co.uk) writes: > I want the top 20 records to be distinct by Food_Code. > > SELECT DISTINCTROW ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| SkunkDave (dave_casserly@totalise.co.uk) writes: > I want the top 20 records to be distinct by Food_Code. > > SELECT DISTINCTROW TOP 20 Food_Code, ID, The_Date, Grams_Eaten > FROM food_eaten > ORDER BY ID DESC; > > With this query im getting the top 20 ID's, which is what i want but i > want the Food_Code to be distinct. Is this a contradiction in my SQL > statement? DISTINCT applies to all columns. > How else could i get the last 20 items entered but have distinct > Food_Codes's? By posting the following information: o The CREATE TABLE statement for your table. o Sample data in form of INSERT statements. o Desired output from the sample data. (It's probably a good idea to change to TOP 3 rather than TOP 20 to reduce the need for data.) Without this information it is a guesswork what you are after. -- Erland Sommarskog, SQL Server MVP, sommar@algonet.se Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp |