Solved

selecting duplicates AND non duplicates.

Posted on 2007-04-11
3
189 Views
Last Modified: 2007-04-11
I am doing some maintenance on our email user database.  I have the following statement that selects the latest email from the duplicate emails.

SELECT
      EmailAddress,
      MAX(USERID) AS LatestUserID
FROM
      Account
GROUP BY
      EmailAddress
HAVING
      Count(EmailAddress) > 1

What I want to do, is in the SAME statement, select all the emails that are not duplicates. So I have a unique email address on each row.
Thanks in advance
0
Comment
Question by:rbradberry
  • 2
3 Comments
 
LVL 10

Expert Comment

by:ksaul
ID: 18891723
SELECT
      EmailAddress,
      MAX(USERID) AS LatestUserID
FROM
      Account
GROUP BY
      EmailAddress
HAVING
      Count(EmailAddress) > 1

UNION

SELECT
      EmailAddress,
      MAX(USERID)
FROM
      Account
GROUP BY
      EmailAddress
HAVING
      Count(EmailAddress) = 1
0
 
LVL 10

Accepted Solution

by:
ksaul earned 500 total points
ID: 18891739
Actually, if I understand it right you can just do:

SELECT
      EmailAddress,
      MAX(USERID) AS LatestUserID
FROM
      Account
GROUP BY
      EmailAddress
0
 
LVL 3

Author Comment

by:rbradberry
ID: 18891900
wow, that seems to have worked out perfectly
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question