[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

"DISTINCT email" with extra fields also in recordset

Posted on 2003-10-23
8
Medium Priority
?
323 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 200 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
Technology Partners: 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.
Suggested Courses

834 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