user_id category suburb dated walk_time
1 experience US 2016-04-09 5
1 discovery US 2016-04-09 5
1 experience UK 2016-04-09 5
1 experience AUS 2016-04-23 10
2 actions IND 2016-04-15 2
2 actions IND 2016-04-15 1
2 discovery US 2016-04-21 2
3 discovery FR 2016-04-12 3
3 Emotions IND 2016-04-23 3
3 discovery UK 2016-04-12 4
3 experience IND 2016-04-12 3
我试图让每一个用户最常用的类别,郊区,日,walk_time如何获得行的最OCCURENCES为每一位用户在MySQL
所以生成的表将
user_id category suburb dated walk_time
1 experience US 2016-04-09 5
2 actions IND 2016-04-15 2
3 discovery IND 2016-04-12 3
我在这里尝试的查询是
select user_id,
substring_index(group_concat(suburb order by cnt desc), ',', 1) as suburb_visited,
substring_index(group_concat(category order by cct desc), ',', 1) as category_used,
substring_index(group_concat(walk_time order by wct desc), ',', 1) as walked,
substring_index(group_concat(dated order by nct desc), ',', 1) as dated_at
from (select user_id, suburb, count(*) as cnt,category, count(*) cct, walk_time, count(*) wct, dated,count(*) nct
from temp_user_notes
group by user_id, suburb,category,walk_time,dated
) upv
group by user_id;
为什么'US'出现在预期输出的第一条记录而不是'UK'? –
哪些“郊区和walk_time”值与user_id一起预计?如果在使用频率相同的两个类别之间有一个联系,那么您希望选择哪一个? – 1000111
对于每个用户,我正在尝试获取类别,郊区,walk_time中的值,这些值具有最大的出现次数。假设用户访问郊区IND 3时间和US 1时间,那么我们将获取IND。 – Prem