简体   繁体   中英

Remove duplicates returning from join

I'm attempting to join two tables. Each table has a unique ID. Upon joining, multiple records are returned (many to one relationship). How can I avoid the duplication of records and obtain one matching record.

Join criteria used is on email and date field:

select 1.email, 1.lead_id, 1.lead_date, 2.rdm_id
from table_1 
   inner join table_2 on 1.email=2.email and 1.lead_date = 2.post_date

Table 1:

email, lead_id, lead_date
124@gmail.com, 1655535, 1/1/2013
124@gmail.com, 1655536, 1/1/2013

Table 2:

email, rdm_id, post_date
124@gmail.com, 3283370, 1/1/2013
124@gmail.com, 3283373, 1/1/2013

I'd like my output to be:

124@gmail.com, 1655535, 3283370, 1/1/2013
124@gmail.com, 1655536, 3283373, 1/1/2013

Currently returning:

124@gmail.com, 1655535, 3283370, 1/1/2013
124@gmail.com, 1655536, 3283370, 1/1/2013
124@gmail.com, 1655535, 3283373, 1/1/2013
124@gmail.com, 1655536, 3283373, 1/1/2013

Just use distinct or group by to remove duplicates.

select distinct ....

or

your_sql_query
group by email, num1, num2, date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM