Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select distinct but return multiple columns

Author  Topic 

SuzyB_24
Starting Member

11 Posts

Posted - 2012-12-06 : 06:35:49
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
Master Smack Fu Yak Hacker

5155 Posts

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

SuzyB_24
Starting Member

11 Posts

Posted - 2012-12-06 : 07:10:16
Thank you xD
Go to Top of Page
   

- Advertisement -