[英]SQL GROUP BY - Check if column with different values has certain value
I am new to working with SQL and I am not sure how to approach the following problem:我是使用 SQL 的新手,我不确定如何解决以下问题:
Imagine I got this table想象一下我有这张桌子
User_ID | Operating System | LastSeen (containing some timestamps)
1 | IOS | hh:mm:ss
1 | AOS | hh:mm:ss
1 | Unknown | hh:mm:ss
2 | IOS | hh:mm:ss
2 | Unkown | hh:mm:ss
3 | AOS | hh:mm:ss
UserID in combination with the OperatingSystem could be a primary key and is unique in each row UserID 与操作系统结合可以是主键,并且在每一行中都是唯一的
I want to get a user table (and therefore I wanna use group by User_ID) giving me the latest LastSeen Value (using Max(LastSeen)) as well as a boolean for usesAndroid and usesIOS.我想获得一个用户表(因此我想通过 User_ID 使用组)给我最新的 LastSeen 值(使用 Max(LastSeen))以及用于 usesAndroid 和 usesIOS 的 boolean。
so the result should look like所以结果应该看起来像
User ID | Using_IOS | USING_AOS | LastSeen containing Max(LastSeen)
1 | True | True | hh:mm:ss
2 | True | False | hh:mm:ss
3 | False | True | hh:mm:ss
Regarding the Using_IOS/Using_AOS columns, I am not sure how to get them in a group by, can I use sth like a contains or in function in a GroupBy for a column that contains multiple different values in a group?关于 Using_IOS/Using_AOS 列,我不知道如何将它们分组,我可以在 GroupBy 中使用包含或 function 之类的东西来获取包含多个不同值的列组吗?
Thanks in advance!提前致谢!
You can group on the user ID and use sum
:您可以对用户 ID 进行分组并使用sum
:
select user_id, sum(OperatingSystem = "IOS") > 0, sum(OperatingSystem = "AOS") > 0, max(LastSeen)
from test group by user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.