Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:32:10+00:00 2026-06-06T22:32:10+00:00

I have a table with the following columns in SQL Server: MEMBERID, MEMBEREMAIL, FATHEREMAIL,

  • 0

I have a table with the following columns in SQL Server:

MEMBERID, MEMBEREMAIL, FATHEREMAIL, MOTHEREMAIL, MEMBERNAME

MEMBERID is PK. The three email columns are not unique, so the same email may appear several times in the same row AND in several rows.

I am trying to extract a unique list of emails, and for each email also get a memberid and membername (it does not matter from which record).

For example if I have three rows:

1   x@x.com   y@y.com   y@y.com   Mark
2   z@z.com   y@y.com   x@x.com   John
3   x@x.com   y@y.com   z@z.com   Susan

I want to get the three emails (x@x.com, y@y.com, z@z.com) and for each of those a MEMBERID in which they appear. It does NOT which MEMBERID (for example for x@X.com I don’t care if I get the values 1 and Mark or 2 and John or 3 and Susan, as long as x@x.com appears only once in the results.

If I use DISTINCT when trying to return the email and memberid and membername, of course I get all of the rows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:32:11+00:00Added an answer on June 6, 2026 at 10:32 pm

    You could use a subquery to normalize all emails. Then you can use row_number to filter out one memberid, membername per email:

    select  *
    from    (
            select  row_number() over (partition by email order by memberid) as rn
            ,       *
            from    (
                    select  MEMBERID
                    ,       MEMBERNAME
                    ,       MEMBEREMAIL as email
                    from    YourTable
                    union all
                    select  MEMBERID
                    ,       MEMBERNAME
                    ,       FATHEREMAIL
                    from    YourTable
                    union all
                    select  MEMBERID
                    ,       MEMBERNAME
                    ,       MOTHEREMAIL
                    from    YourTable
                    ) as emails
            ) num_emails
    where   rn = 1
    

    You could also normalize the emails using the UNPIVOT clause, like this:

    select  *
    from    (
            select  row_number() over (partition by email order by memberid) as rn
            ,       *
            from    (
                    select  MEMBERID
                    ,       MEMBERNAME
                    ,       email
                    from    YourTable
                    unpivot (
                                    email
                            for     emailOwner
                            in      (
                                    MEMBEREMAIL,
                                    FATHEREMAIL,
                                    MOTHEREMAIL
                                    )
                            ) as u
                    ) as emails
            ) num_emails
    where   rn = 1
    

    Try both versions at SQL Fiddle:

    • UNION ALL version

    • UNPIVOT version

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchical (tree structure) SQL Server table TEmployee with following columns Id
I'm using SQL-Server 2005 Standard. I have Users table with following columns: userID(int), userImage(varchar),
Using SQL Server 2005 I have a table with the following columns id name
I have a table in SQL Server with the following columns: id int (primary
In SQL Server 2005 I have a table with the following columns: Table Reservations:
I have a table (SQL Server 2005) with the following columns: State: - StateId
In a SQL Server table I have the following 2 columns: RowId : primary
I have a table in SQL Server that contains the following columns : Id
In SQL Server 2008, I have a table with following columns and data Date
I have a table in SQL Server 2005 database that has following columns: Id,ProductName,Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.