Solved

selecting duplicates AND non duplicates.

Posted on 2007-04-11
3
186 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
Comment Utility
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
Comment Utility
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
Comment Utility
wow, that seems to have worked out perfectly
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now