Solved

"DISTINCT email" with extra fields also in recordset

Posted on 2003-10-23
8
313 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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 58
Find unused columns in a table 12 66
WSUS Console loosed connectivity to the database. 13 32
SQL Query Task 11 42
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

679 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