Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 11th, 2006, 09:47 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting only rows with distinct columns.

Hello,

I have a problem that should be very simple but I am having a really bad day or something and just can't make it out. Any help will be highly appreciated!

Imagine a table with columns ID, ANIMAL, FOOD.

The content of the table is the following:

1, Ape, Banana
2, Pig, JustAboutAnything
3, Bird, Seed
4, Ape, Ecological Banana

Now I want to retrieve all columns but I want the ANIMAL-column to be DISTINCT in nature. For example I only want the first row if there are more occurences of a column-data (like in the example above that features two "Ape"-occurences). I only want the first 3 rows in the example above.

Cheers!



 
Old January 11th, 2006, 11:54 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

USE TEMPDB; -- Good habit for SQL 2005 is to use ; after every statement
-- Setup Animal Feed test data (some animals feed more equally than others)
CREATE TABLE dbo.AnimalFeed
(ID int
,Animal varchar(20)
,Food varchar(29)
);

INSERT dbo.AnimalFeed VALUES ( 1, 'Ape' , 'Banana' );
INSERT dbo.AnimalFeed VALUES ( 2, 'Pig' , 'Anything non-metallic' );
INSERT dbo.AnimalFeed VALUES ( 3, 'Bird' , 'Bird Seed' );
INSERT dbo.AnimalFeed VALUES ( 4, 'Ape' , 'Rotten Banana' );
INSERT dbo.AnimalFeed VALUES ( 5, 'Ape' , 'Potato' );
INSERT dbo.AnimalFeed VALUES ( 6, 'Pig' , 'Pig Feed' );
INSERT dbo.AnimalFeed VALUES ( 7, 'Bird' , 'Worms' );

-- Return First row for each animal
SELECT *
FROM dbo.AnimalFeed AF
WHERE AF.ID = (SELECT TOP 1 ID
                FROM dbo.AnimalFeed AF2
                WHERE AF2.Animal = AF.Animal);

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
 
Old January 11th, 2006, 12:28 PM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot David!

I had been experimenting with a much more complex solution with plenty more subqueries but - as always - the solution to what seems to be puzzling problems is normally simple.

Cheers!


 
Old January 11th, 2006, 12:29 PM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Dave!

Simple and elegant just as it ideally should be!

Cheers,
Thomas


 
Old January 12th, 2006, 04:49 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thomas,

I am glad I could help. Correlated SubQueries are quite elegant, however they can have a performance impact since the subquery effectively runs once for every row of the outer query. In the subquery you should use an order by to determine which row you want returned (the lowest ID, the highest ID, or some other criteria)

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rows 2 Columns kilika Oracle 0 October 14th, 2005 01:16 PM
Columns To Rows alyeng2000 SQL Language 2 March 11th, 2005 03:08 PM
Rows into columns shamsad SQL Language 0 April 7th, 2004 04:39 AM
Rows into columns shamsad Oracle 0 April 7th, 2004 04:38 AM
Distinct fields from mult columns abbylee26 Classic ASP Basics 4 March 18th, 2004 12:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.