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

  • Home
  • SEARCH
  • 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 242339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:49:45+00:00 2026-05-11T20:49:45+00:00

What I have is basically a problem which is easily solved with multiple tables,

  • 0

What I have is basically a problem which is easily solved with multiple tables, but I have only a single table to do it.

Consider the following database table

UserID UserName EmailAddress         Source
3K3S9  Ben      ben@myisp.com        user
SF13F  Harry    lharry_x@hotbail.com 3rd_party
SF13F  Harry    reside@domain.com    user
76DSA  Lisa     cake@insider.com     user
OL39F  Nick     stick@whatever.com   3rd_party
8F66S  Stan     myman@lol.com        user

I need to select all fields, but only who each user once along with one of their email addresses (the “biggest” one as determined by the MAX() function). This is the result I am after …

UserID UserName EmailAddress         Source
3K3S9  Ben      ben@myisp.com        user
SF13F  Harry    lharry_x@hotbail.com 3rd_party
76DSA  Lisa     cake@insider.com     user
OL39F  Nick     stick@whatever.com   3rd_party
8F66S  Stan     myman@lol.com        user

As you can see, “Harry” is only shown once with his “highest” email address the correcponding “source”

Currently what is happening is that we are grouping on the UserID, UserName, and using MAX() for the EmailAddress and Source, but the max of those two fields dont always match up, they need to be from the same record.

I have tried another process by joining the table with itself, but I have only managed to get the correct email address but not the corresponding “source” for that address.

Any help would be appreciated as I have spent way too long trying to solve this already 🙂

  • 1 1 Answer
  • 1 View
  • 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-05-11T20:49:45+00:00Added an answer on May 11, 2026 at 8:49 pm

    If you’re on SQL Server 2005 or higher,

    SELECT  UserID, UserName, EmailAddress, Source
    FROM    (SELECT UserID, UserName, EmailAddress, Source,
                    ROW_NUMBER() OVER (PARTITION BY UserID
                                       ORDER BY EmailAddress DESC) 
                        AS RowNumber
             FROM   MyTable) AS a
    WHERE   a.RowNumber = 1
    

    Of course there are ways to do the same task without the (SQL-Standard) ranking functions such as ROW_NUMBER, which SQL Server implemented only since 2005 — including nested dependent queries and self left joins with an ON including a ‘>’ and a WHERE ... IS NULL trick — but the ranking functions make for code that’s readable and (in theory) well optimizable by the SQL Server Engine.

    Edit: this article is a nice tutorial on ranking, but it uses RANK in the examples instead of ROW_NUMBER (or the other ranking function, DENSE_RANK) — the distinction matters when there are “ties” among grouped rows in the same partition according to the ordering criteria. this post does a good job explaining the difference.

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

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can exclude whole directories or directory trees with "**"… May 13, 2026 at 5:18 pm
  • Editorial Team
    Editorial Team added an answer Yes, I would use simple mode. In fact, I do...… May 13, 2026 at 5:18 pm
  • Editorial Team
    Editorial Team added an answer Rather than using side-effects, use the overload of Select which… May 13, 2026 at 5:18 pm

Related Questions

Env: Tomcat 5.x on Java 1.5.x on Windows using OracleJDBC driver on Oracle 9i
I am creating an iPhone app which I would like to have a similar
I'm currently trying to solve a problem where I need to find the position
Just wanted opinions on a design question. If you have a C++ class than

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.