Selecting rows with distinct values for a column.

  • Let's say I have this dataset.

    UserIDEmail

    ------------------------------------

    1jamesbond@mi6.com

    1jbond@mi6.com

    1jamesb@mi6.com

    2mpenny@mi6.com

    3q@mi6.com

    3weapons_dept@mi6.com

    How do I pick out just one email address for each user id? I'm struggling to write a query to do it.

    Just as long as I get a result set with 3 user ids, and 3 email addresses, I'm happy. It doesn't matter which email address is selected.

  • Kaptnik (6/12/2013)


    Let's say I have this dataset.

    UserIDEmail

    ------------------------------------

    1jamesbond@mi6.com

    1jbond@mi6.com

    1jamesb@mi6.com

    2mpenny@mi6.com

    3q@mi6.com

    3weapons_dept@mi6.com

    How do I pick out just one email address for each user id? I'm struggling to write a query to do it.

    Just as long as I get a result set with 3 user ids, and 3 email addresses, I'm happy. It doesn't matter which email address is selected.

    This:

    select

    UserID,

    Email

    from

    (select

    rn = row_number() over (partition by UserID order by (select null)),

    UserID,

    Email

    from

    yourTableNameHere

    ) dt

    where

    rn = 1;

    Untested, but it should work.

  • As easy as

    SELECT UserID,

    MAX(Email) Email

    FROM MyTable

    GROUP BY UserID --Edited post to add this line

    If you need a specific email, you need to define the priority.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (6/12/2013)


    As easy as

    SELECT UserID,

    MAX(Email) Email

    FROM MyTable

    If you need a specific email, you need to define the priority.

    Yep, easy except you are missing something:

    SELECT

    UserID,

    MAX(Email) Email

    FROM

    MyTable

    GROUP BY

    UserID;

  • Yup, I was going to say that it was missing the GROUP BY clause as well.

    Both solutions work, but I'm going with Luiz's as it's very simple, and since I don't care which email address I get, I don't need to prioritize.

    Thanks guys!

  • Lynn Pettis (6/12/2013)


    Luis Cazares (6/12/2013)


    As easy as

    SELECT UserID,

    MAX(Email) Email

    FROM MyTable

    If you need a specific email, you need to define the priority.

    Yep, easy except you are missing something:

    SELECT

    UserID,

    MAX(Email) Email

    FROM

    MyTable

    GROUP BY

    UserID;

    :w00t: I need to sleep some more :hehe:

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply