Unix Technical Forum

Re: DISTINCT ROW problem

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 ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:13 PM
Erland Sommarskog
 
Posts: n/a
Default Re: DISTINCT ROW problem

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:09 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com