2016-06-07 70 views
3
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; 
+0

为什么'US'出现在预期输出的第一条记录而不是'UK'? –

+1

哪些“郊区和walk_time”值与user_id一起预计?如果在使用频率相同的两个类别之间有一个联系,那么您希望选择哪一个? – 1000111

+0

对于每个用户,我正在尝试获取类别,郊区,walk_time中的值,这些值具有最大的出现次数。假设用户访问郊区IND 3时间和US 1时间,那么我们将获取IND。 – Prem

回答

2
SELECT user_id, 
     (SELECT category FROM temp_user_notes t1 
     WHERE t1.user_id = T.user_id 
     GROUP BY category ORDER BY count(*) DESC LIMIT 1) as category, 
     (SELECT suburb FROM temp_user_notes t2 
     WHERE t2.user_id = T.user_id 
     GROUP BY suburb ORDER BY count(*) DESC LIMIT 1) as suburb, 
     (SELECT dated FROM temp_user_notes t3 
     WHERE t3.user_id = T.user_id 
     GROUP BY dated ORDER BY count(*) DESC LIMIT 1) as dated, 
     (SELECT walk_time FROM temp_user_notes t4 
     WHERE t4.user_id = T.user_id 
     GROUP BY walk_time ORDER BY count(*) DESC LIMIT 1) as walk_time 
FROM (SELECT DISTINCT user_id FROM temp_user_notes) T 

http://sqlfiddle.com/#!9/8aac6a/19

+0

感谢您的答案,但为什么它离开了用户。结果不会为每个用户提取。 – Prem

+0

它不会遗漏用户那里的user_id –

+0

检查sqlfiddle –

1

试试这个,看起来有点复杂,但希望对你有帮助;)

的Mysql架构:

CREATE TABLE table1 
    (`user_id` int, `category` varchar(10), `suburb` varchar(3), `dated` datetime, `walk_time` int) 
; 

INSERT INTO table1 
    (`user_id`, `category`, `suburb`, `dated`, `walk_time`) 
VALUES 
    (1, 'experience', 'US', '2016-04-09 00:00:00', 5), 
    (1, 'discovery', 'US', '2016-04-09 00:00:00', 5), 
    (1, 'experience', 'UK', '2016-04-09 00:00:00', 5), 
    (1, 'experience', 'AUS', '2016-04-23 00:00:00', 10), 
    (2, 'actions', 'IND', '2016-04-15 00:00:00', 2), 
    (2, 'actions', 'IND', '2016-04-15 00:00:00', 1), 
    (2, 'discovery', 'US', '2016-04-21 00:00:00', 2), 
    (3, 'discovery', 'FR', '2016-04-12 00:00:00', 3), 
    (3, 'Emotions', 'IND', '2016-04-23 00:00:00', 3), 
    (3, 'discovery', 'UK', '2016-04-12 00:00:00', 4), 
    (3, 'experience', 'IND', '2016-04-12 00:00:00', 3) 
; 

查询SQL:

select c.user_id, c.category, s.suburb, d.dated, w.walk_time 
from (
    select user_id, left(group_concat(category order by cnt desc), locate(',', group_concat(category order by cnt desc)) - 1) as category 
    from (
     select 
      user_id, category, count(1) as cnt 
     from table1 
     group by user_id, category 
    ) t 
    group by user_id 
) c 
inner join (
    select user_id, left(group_concat(suburb order by cnt desc), locate(',', group_concat(suburb order by cnt desc)) - 1) as suburb 
    from (
     select 
      user_id, suburb, count(1) as cnt 
     from table1 
     group by user_id, suburb 
    ) t 
    group by user_id 
) s on c.user_id = s.user_id 
inner join (
    select user_id, left(group_concat(dated order by cnt desc), locate(',', group_concat(dated order by cnt desc)) - 1) as dated 
    from (
     select 
      user_id, dated, count(1) as cnt 
     from table1 
     group by user_id, dated 
    ) t 
    group by user_id 
) d on c.user_id = d.user_id 
inner join (
    select user_id, left(group_concat(walk_time order by cnt desc), locate(',', group_concat(walk_time order by cnt desc)) - 1) as walk_time 
    from (
     select 
      user_id, walk_time, count(1) as cnt 
     from table1 
     group by user_id, walk_time 
    ) t 
    group by user_id 
) w on c.user_id = w.user_id 

结果:

| user_id | category | suburb |  dated  | walk_time | 
+---------+------------+--------+---------------------+-----------+ 
| 1  | experience | US  | 2016-04-09 00:00:00 | 5   | 
| 2  | actions | IND | 2016-04-15 00:00:00 | 2   | 
| 3  | discovery | IND | 2016-04-12 00:00:00 | 3   | 
相关问题