SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select distinct but return multiple columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SuzyB_24
Starting Member

United Kingdom
11 Posts

Posted - 12/06/2012 :  06:35:49  Show Profile  Reply with Quote
I have inherited a database with a table that contains the following data.

Number	Name	        Image	              
2	Deck Two	/images/626_deck2.gif
2	Deck Two	/images/626_deck2in.gif	
3	Deck Three	/images/626_deck3.gif
3	Deck Three	/images/626_deck3in.gif	

I want to select one entry for each Number. But because the Image field is unique select distinct doesn't seem to work.

Can anyone advise how I can select only one entry for each number.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/06/2012 :  06:49:05  Show Profile  Reply with Quote
SELECT Number,NAME, IMAGE FROM
(
	SELECT *,
		ROW_NUMBER() OVER (PARTITION BY Number,NAME ORDER BY IMAGE) AS RN
	FROM 
		YourTable
)s WHERE RN=1;
Go to Top of Page

SuzyB_24
Starting Member

United Kingdom
11 Posts

Posted - 12/06/2012 :  07:10:16  Show Profile  Reply with Quote
Thank you xD
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000