Solved

"DISTINCT email" with extra fields also in recordset

Posted on 2003-10-23
8
315 Views
Last Modified: 2008-02-01
Hi

I using the code below to get a deduplicated list from a SQL server table but i also want to be able to get extra fields form teh table so i add " ,name"  however this then gives me duplicates ... ?????

sql = "Select DISTINCT email,name from tbl1"

Help .... :o(
0
Comment
Question by:itimes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:SQLMaster
ID: 9606464
Use group by clause in your sql query

Consruct your sql like this:

sql = "Select DISTINCT email,name from tbl1 group by Email,name"

In case you aslo want to get extra fields, you can use group by clause with them also

Thanks





0
 
LVL 6

Expert Comment

by:lausz
ID: 9606866
Both are identical :

Select DISTINCT email,name from tbl1
=
Select DISTINCT email,name from tbl1 group by Email,name

If you have duplicated email when you add names in your select statement is because you have more than 1 name for each email...

If you want post an example and what do you need and we try to help you,.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 50 total points
ID: 9606977
If all you want is one name for each email address than try something like this:

sql = "Select email, Max(name) from tbl1 Group By email"

Anthony
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9607009
If you get dups using this Select
DISTINCT email,name from tbl1  
that means you have more than one name associated with same email address.

You can find the duplicate email address using this query

select email
from table
group by email
having count(*) > 1

You could try get distinct list. (You will get only the max name if there are more than one names associated with the same email address)

select email, max(name)
from table
group by email
0
 

Author Comment

by:itimes
ID: 9607159
acperkins,

This works perfectly

whats is max then ???
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9607212
>>whats is max then ??? <<
Max will give you the "maximum" name within the group.  In other words, if you have something like this:
John myemail@domain.com
Jim myemail@domain.com
Jay myemail@domain.com
Adam myotheremail@domain.com
Andrew myotheremail@domain.com

You would get:
John myemail@domain.com
Andrew myotheremail@domain.com

You can also use Min to give to the "minimum" name.  In that case the result would be:
Jay myemail@domain.com
Adam myotheremail@domain.com

Anthony
0
 

Author Comment

by:itimes
ID: 9607299
acperkins,

so if the name field was DATES

01/01/2002 01:01:56
02/01/2002 10:01:58
05/01/2002 02:01:12
04/01/2002 12:01:05

"05/01/2002 02:01:12"  woudl be the MAX ???

Thanks for the help ...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9607740
>>"05/01/2002 02:01:12"  woudl be the MAX ???<<
That is correct

Anthony
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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