为多个辅助联接记录选择1条记录

时间:2013-12-20 07:22:25

标签: sql select join group-by

查询为每个关联人选择1个潜在客户。

如何按Lead_ID + MAX(person.Created_Timestamp)分组

select 
top 100 lead.lead_id,person.first_name,person.last_name,person.gender,person.modified_ts
from lead
left join person on person.lead_id=lead.lead_id
left join person_email on person_email.person_id=person.person_id
left join person_address on person_address.person_id=person.person_id
left join address_location on person_address.address_location_id=address_location.address_location_id
where  address_location.state_code = 'QL'

1 个答案:

答案 0 :(得分:0)

select 
lead.lead_id, MAX(person.Created_Timestamp)
from lead
left join person on person.lead_id=lead.lead_id
left join person_email on person_email.person_id=person.person_id
left join person_address on person_address.person_id=person.person_id
left join address_location on person_address.address_location_id=address_location.address_location_id
where  address_location.state_code = 'QL'
GROUP  BY lead.lead_id