一張表,同一user_id多次不同渠道進入,要求是當user_id 唯一通過A渠道進入時保留該user_id首次通過A時的記錄,其他情況下,剔除通過A渠道進入的記錄。
表:
user_id channel dt
1 A 2020/6/28
1 A 2020/6/29
2 A 2020/6/30
2 V 2020/7/1
3 A 2020/7/2
3 S 2020/7/3
3 V 2020/7/4
最后效果
user_id channel dt
1 A 2020/6/28
2 V 2020/7/1
3 S 2020/7/3
3 V 2020/7/4
uj5u.com熱心網友回復:
select * from (select t1.user_id user_id, 'A' channel, min(t1.dt) dt from t t1
where t1.channel = 'A' and not exists (
select 1 from t t2 where t2.user_id = t1.user_id and t2.channel != 'A')
group by t1.user_id
union all
select t3.* from t t3
where t3.channel != 'A'
) t4
order by t4.dt asc
uj5u.com熱心網友回復:
親測可用轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/10262.html
標籤:MySQL
上一篇:求助mysql時間問題
