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
 General SQL Server Forums
 New to SQL Server Programming
 selecting distinct with with all columns

Author  Topic 

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2011-12-19 : 08:35:41
Hello

I have a query that returns a list of users and there company and email address.

But some have a multiple email addresses and company_names.

Each user has a ref number, so some appear more than once.

How do i select them distinctly returning all columns.

data example.

552 rob davis
552 rob davis
222 lisa smith
665 martin bassett
665 martin bassett

I know i can select distinct just by selecting the ref number, but i want to return all columns.

MCTS / MCITP certified

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-19 : 08:40:46
[code];with cte as
(
select *, row_number() over (partition by ref_number order by (select null)) as RN
from employees
)
select * from cte where rn=1;[/code]This will get you one (randomly selected) row for each employee. If you want it to be more sophisticated - for example, get all e-mail addresses, more work will need to be done.
Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2011-12-19 : 08:56:59
no that is excellent. i have my list now,

thank you very much
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-19 : 12:17:34
very welcome.
Go to Top of Page

Nikhil341
Starting Member

9 Posts

Posted - 2011-12-19 : 16:54:54
Hey, I have a similar type of question.

If "CTE" is a table name in the above explanation, is there any generic SQL statement (Not including the Table name) to produce same kind of O/P?

Please have a look on the "Removing multiple rows in Excel by using the SQL." issue which was posted by me.

Thanks.

Thanks.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-19 : 17:53:04
quote:
Originally posted by masterdineen
MCTS / MCITP certified



Really

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2011-12-20 : 05:11:15
Yes really.

sometimes forget the basics,

we are not all perfect american rebots
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-12-20 : 07:03:43
Those answer your question certainly.

However, why does your schema allow this?

If you have a more normalised structure then this problem wouldn't exist.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-20 : 07:51:05
Actually.....


Using a "Randomly" selected set of values....what the hell good is that?

Oooops..you sent the sheet to my _________ address...not my home...



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -