简体   繁体   English

MySQL - 按两列分组

[英]MySQL - Group by two Columns

I have a table that looks like this我有一张看起来像这样的桌子

id, user_id   , type, date
3,  ivnWvOQqoN, iOS , 2015-11-24 15:46:09
4,  dskIbJhuSd, iOS , 2015-11-23 19:39:31
5,  dskIbJhuSd, iOS , 2015-11-24 23:37:45
6,  ----------, iOS , 2015-11-22 23:38:05
7,  ----------, iOS , 2015-11-23 23:38:10

And right now I am doing a query like this现在我正在做这样的查询

SELECT COUNT(*) AS entries, user_id, DATE(created_at) as date, device_type
FROM App_Usage
WHERE device_type = 'iOS'
  AND created_at between 2015-11-22 AND 2015-11-25
GROUP BY DATE(created_at), user_id

This return results that I would expect it looks like this这个返回结果我希望它看起来像这样

((1L, '----------', datetime.date(2015, 11, 22), 'ios'),
(1L, 'dskIbJhuSd', datetime.date(2015, 11, 23), 'ios'),
(1L, '----------', datetime.date(2015, 11, 23), 'ios'),
(1L, 'dskIbJhuSd', datetime.date(2015, 11, 24), 'ios'),
(1L, 'ivnWvOQqoN', datetime.date(2015, 11, 24), 'ios')

The last two lines in the above code, become the last line in the next code.上面代码的最后两行,成为下一段代码的最后一行。 And the second line in above code becomes 2nd line in code below.上面代码中的第二行变成下面代码中的第二行。

My question is could I group by date and if the user is not '----------' , so it would return a result like this我的问题是我可以按日期分组,如果用户不是'----------' ,那么它会返回这样的结果

((1L, '----------', datetime.date(2015, 11, 22), 'ios'),
(1L, 'combo of users' datetime.date(2015, 11, 23), 'ios'),
(1L, '----------', datetime.date(2015, 11, 23), 'ios'),
(2L, 'combo of users', datetime.date(2015, 11, 24), 'ios'),

I left the user_id in the first results to show that as long as the user is '---------' I want those grouped and the others grouped.我在第一个结果中留下了 user_id 以表明只要用户是'---------'我希望那些分组和其他分组。

So I want to group by date, and then by where user is not '----------' , how could I do this or is it even possible?所以我想按日期分组,然后按用户不是'----------'的地方分组,我怎么能这样做,甚至可能吗?

DESIRED OUTPUT期望的输出

OUTPUT:输出:

number of uses, user_id**, type, date;
(1, '----------', 'ios', '2015-11-22 23:38:05'),
(1, 'some users or combo', 'ios', '2015-11-22 13:33:33'),
(2, 'some users or combo', 'ios', '2015-11-23 13:35:37'),
(1, '----------', 'ios', '2015-11-24 00:09:44'),
(2, 'some users or combo', 'ios', '2015-11-24 00:09:44'),

**So I want any users that is '----------' to be grouped with '-------' but any user that is not '--------' to be goruped with all other users. **所以我希望任何'----------'用户都与'-------'分组,但任何不是'--------'的用户与所有其他用户混在一起。 As you can see on the 24rd, the 2 different uses are put as one and the use that was '-------' is serperate (this is the last two lines above)正如您在 24 日所看到的,2 种不同的用途合二为一,“-------”的用途是锯齿状的(这是上面的最后两行)

select count(*) as entries, dateCreate, device_type, user_id
from 
    (SELECT DATE(created_at) as dateCreate, device_type, case when user_id = '----------' then '-----------' else 'combo of users' as user_id  
    FROM App_Usage 
    WHERE device_type = 'iOS' 
    AND created_at between 2015-11-22 
    AND 2015-11-25) as temp_table 
GROUP BY dateCreate, user_id, device_type

note: used dateCreate instead of date as it is a keyword注意:使用 dateCreate 而不是 date 因为它是一个关键字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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