Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Help with SQL Express 2005
Old 07-11-2008, 11:13 AM Help with SQL Express 2005
Super Talker

Posts: 116
Trades: 0
Hi,

I have a question...and I'm a little rusty on my SQL.

I have a database in MS-SQL Express 2005 which have several email fields as columns...

ex: ID, UserName, Email, Contact1Email, Contact2Email

I want to do a query that will give the results as...

ID, UserName, EmailField

Where EmailField will be a separate record for each of the email columns above.

Ex: Record 1 - ID, UserName, EmailField (contains Email)
Record 2 - ID, UserName, EmailField (Contains Contact1Email)
Record 3 - ID, UserName, EmailField (Contains Contact2Email)
Record 4 - ID (Next Record), UserName, EmailField (Contains Email)

and so on.

Can anyone help?

It would be great to do it killing duplicate emails.

Thanks
Donna Z
DonnaZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-11-2008, 02:13 PM Re: Help with SQL Express 2005
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Write a bunch of SELECT queries and pull them together with the UNION operator.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-13-2008, 02:42 PM Re: Help with SQL Express 2005
vividearth's Avatar
Experienced Talker

Posts: 33
Location: London, UK
Trades: 0
Something like:

select
distinct*from(
select id, username, email from table
union
select id, username, contact1email from table
union
select id, username, contact2email from table
)as emails

This will not remove people who have the same email under different names though.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
vividearth is offline
Reply With Quote
View Public Profile Visit vividearth's homepage!
 
Old 07-13-2008, 04:09 PM Re: Help with SQL Express 2005
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
This will not remove people who have the same email under different names though.
Then use a "group by" clause on your "select distinct" query, and drop the distinct...

I've just added an "origin" column, if you need to distinguish where each row come from
Code:
select id, username, email, origin from(
select id, username, email, 'table1' from table
union
select id, username, contact1email, 'table2' from table
union
select id, username, contact2email, 'table3' from table
)as emails
group by emails.email
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 07-13-2008 at 04:12 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-14-2008, 03:21 PM Re: Help with SQL Express 2005
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Tripy is right, and notice how he gave the inner union query a correlation name. It won't run otherwise. Also, everybody should note that a union query automatically runs an implicit distinct. To union without an implied distinct, use the union all command.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-15-2008, 09:00 AM Re: Help with SQL Express 2005
Super Talker

Posts: 116
Trades: 0
Everyone - thanks.
DonnaZ is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with SQL Express 2005
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17547 seconds with 12 queries