Solved

"DISTINCT email" with extra fields also in recordset

Posted on 2003-10-23
8
309 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

22 Experts available now in Live!

Get 1:1 Help Now