Solved

"DISTINCT email" with extra fields also in recordset

Posted on 2003-10-23
8
308 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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

 

Author Comment

by:itimes
Comment Utility
acperkins,

This works perfectly

whats is max then ???
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>"05/01/2002 02:01:12"  woudl be the MAX ???<<
That is correct

Anthony
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

771 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

13 Experts available now in Live!

Get 1:1 Help Now