过滤至少出现 1 次的 2 个值
Filter for appearance of 2 values that must at least exist 1 times
标题可能不好,想不出更好的了。
我的评论数据,每条评论通过usernameChannelId分配给一个账号:
usernameChannelId | hasTopic | sentiment_sum | commentId
a | 1 | 4 | xyxe24
a | 0 | 2 | h5hssd
a | 1 | 3 | k785hg
a | 0 | 2 | j7kgbf
b | 1 | -2 | 76hjf2
c | 0 | -1 | 3gqash
c | 1 | 2 | ptkfja
c | 0 | -2 | gbe5gs
c | 1 | 1 | hghggd
我的代码:
SELECT u.usernameChannelId, avg(sentiment_sum) sentiment_sum, u.hasTopic
FROM total_comments u
WHERE u.hasTopic is True
GROUP BY u.usernameChannelId
HAVING count(u.usernameChannelId) > 0
UNION
SELECT u.usernameChannelId, avg(sentiment_sum) sentiment_sum, u.hasTopic
FROM total_comments u
WHERE u.hasTopic is False
GROUP BY u.usernameChannelId
我想获取所有至少有 1 条 hasTopic == 0 评论和 1 条 hasTopic == 1 评论的 usernameChannelId(以统计方式比较这两个组并删除仅在主题或非主题视频中发表评论的用户)。
我怎样才能这样过滤?
这里有一个小技巧可能会有所帮助。首先,您需要熟悉 CASE expression.,这里是文档的摘录。
The CASE expression
A CASE expression serves a role similar to IF-THEN-ELSE in other
programming languages.
The optional expression that occurs in between the CASE keyword and
the first WHEN keyword is called the "base" expression. There are two
basic forms of the CASE expression: those with a base expression and
those without.
如果 hasTopic 为 0,CASE when hasTopic is False then 1 else 0 END
等表达式的计算结果为 1。hasTopic is True
的表达式类似。
现在,可以对这些 CASE 求和,这将告诉您用户是否有 hasTopic True 和 hasTopic False 的行。
在 having 子句中这样的东西可能会起作用(当然每个值一个)
HAVING SUM(CASE when hasTopic is False then 1 else 0 END) > 0
(WHERE 子句需要去掉,UNION 查询就不需要了)
标题可能不好,想不出更好的了。
我的评论数据,每条评论通过usernameChannelId分配给一个账号:
usernameChannelId | hasTopic | sentiment_sum | commentId
a | 1 | 4 | xyxe24
a | 0 | 2 | h5hssd
a | 1 | 3 | k785hg
a | 0 | 2 | j7kgbf
b | 1 | -2 | 76hjf2
c | 0 | -1 | 3gqash
c | 1 | 2 | ptkfja
c | 0 | -2 | gbe5gs
c | 1 | 1 | hghggd
我的代码:
SELECT u.usernameChannelId, avg(sentiment_sum) sentiment_sum, u.hasTopic
FROM total_comments u
WHERE u.hasTopic is True
GROUP BY u.usernameChannelId
HAVING count(u.usernameChannelId) > 0
UNION
SELECT u.usernameChannelId, avg(sentiment_sum) sentiment_sum, u.hasTopic
FROM total_comments u
WHERE u.hasTopic is False
GROUP BY u.usernameChannelId
我想获取所有至少有 1 条 hasTopic == 0 评论和 1 条 hasTopic == 1 评论的 usernameChannelId(以统计方式比较这两个组并删除仅在主题或非主题视频中发表评论的用户)。
我怎样才能这样过滤?
这里有一个小技巧可能会有所帮助。首先,您需要熟悉 CASE expression.,这里是文档的摘录。
The CASE expression
A CASE expression serves a role similar to IF-THEN-ELSE in other programming languages.
The optional expression that occurs in between the CASE keyword and the first WHEN keyword is called the "base" expression. There are two basic forms of the CASE expression: those with a base expression and those without.
如果 hasTopic 为 0,CASE when hasTopic is False then 1 else 0 END
等表达式的计算结果为 1。hasTopic is True
的表达式类似。
现在,可以对这些 CASE 求和,这将告诉您用户是否有 hasTopic True 和 hasTopic False 的行。
在 having 子句中这样的东西可能会起作用(当然每个值一个)
HAVING SUM(CASE when hasTopic is False then 1 else 0 END) > 0
(WHERE 子句需要去掉,UNION 查询就不需要了)