Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more: (untagged)
Hi
I have a SQL query looking at 4 tables. The results come back with duplicate results in column A which is email address. With the below SQL query, how can I add code that removes duplicate email addresses?

SQL
SELECT distinct(cl.RIID_),   cl.EMAIL_ADDRESS_,   bro.ITEM,   bro.ACTIVITY_DATE  
FROM $A$ cl  JOIN $B$ bro ON cl.EMAIL_ADDRESS_ = bro.EMAIL_ADDRESS_   
JOIN $D$ cms ON cms.SKU = bro.ITEM  
WHERE cl.EMAIL_DELIVERABILITY_STATUS_ = 'D'   
AND cl.EMAIL_PERMISSION_STATUS_ = 'I'   
AND cms.BSCATEGORYNAME = 'Toys & Baby'   
AND (trunc(bro.ACTIVITY_DATE)) >= (Trunc(Cast(From_Tz(Cast(Sysdate As Timestamp ), 'US/Pacific') At Time Zone 'Australia/Sydney' As Date)) - 30)
Posted
Updated 29-Jun-15 21:55pm
v3
Comments
Suvendu Shekhar Giri 30-Jun-15 3:57am    
Can you show the result you are getting? Just 2 duplicate records are enough.
Member 11802120 1-Jul-15 1:28am    
HiHere is an example
RIID_ EMAIL_ADDRESS_ ITEM ACTIVITY_DATE
4185 melissa@XX.com TY9220 2015-06-24 16:10:00.0
4185 melissa@XX.com TY9220 2015-06-25 07:33:45.0
ZurdoDev 30-Jun-15 8:45am    
Since you have distinct at least one column is different. So, what is different?
Member 11802120 1-Jul-15 1:29am    
Its the activity date. Each duplicate has a different time

1 solution

You have RIID_, EMAIL_ADDRESS_, ITEM and ACTIVITY_DATE in the SELECT list. DISTINCT determines the distinct result based the column list those are present in the SELECT list. It doesn't check for a single column rather check for the distinct record, which is formed by the all the columns in the SELECT list.

In your case, even though you have kept the column RIID within a pair of brackets still it is not going to consider that you want distinct RIID only.

So, what can be the solution?
The best thing is to remove ACTIVITY_DATE form the SELECT list if you don't need it.
If you really need that column to be there then you may need to dig further to findout which of the availble multiple ACTIVITY_DATE should correspond to the single distinct RIID or the set of other columns in the result.

I really feel that either you don't need the ACTIVITY_DATE or you don't need the result set to be unique.

If you still feel that you need the same result then please let us know why with little more description about your functional requirement for this particular query.

Hope, it helps :)
 
Share this answer
 
v3
Comments
Member 11802120 1-Jul-15 21:33pm    
thanks, works perfectly! i wasn't thinking that logically!
Suvendu Shekhar Giri 2-Jul-15 0:28am    
Glad to know that it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900