[英]SQL/BigQuery: case when statement over partition by
I have a table about conversations.我有一张关于对话的桌子。 There are many conversation elements for unique conversation IDs
唯一对话 ID 有许多对话元素
conv_id ![]() |
element_id ![]() |
author_type![]() |
part_type![]() |
---|---|---|---|
1 ![]() |
11 ![]() |
bot![]() |
comment![]() |
1 ![]() |
12 ![]() |
bot![]() |
comment![]() |
2 ![]() |
22 ![]() |
bot![]() |
comment![]() |
2 ![]() |
23 ![]() |
bot![]() |
comment![]() |
2 ![]() |
24 ![]() |
admin![]() |
note![]() |
3 ![]() |
32 ![]() |
bot![]() |
note![]() |
and I want to write a case when statement for each conversation id, -> if author_type is 'bot' and part_type is 'comment', then label it as 'bot' in a new column for all the rows of that conversation id.我想为每个对话 id 写一个 case when 语句,-> 如果 author_type 是 'bot' 并且 part_type 是 'comment',那么 label 它在该对话 id 的所有行的新列中作为 'bot'。
So the result will look like this:所以结果将如下所示:
conv_id ![]() |
element_id ![]() |
author_type![]() |
part_type![]() |
tag![]() |
---|---|---|---|---|
1 ![]() |
11 ![]() |
bot![]() |
comment![]() |
bot![]() |
1 ![]() |
12 ![]() |
bot![]() |
comment![]() |
bot![]() |
2 ![]() |
22 ![]() |
bot![]() |
comment![]() |
bot![]() |
2 ![]() |
23 ![]() |
bot![]() |
comment![]() |
bot![]() |
2 ![]() |
24 ![]() |
admin![]() |
note![]() |
bot![]() |
3 ![]() |
32 ![]() |
bot![]() |
note![]() |
for example, the when conv_id is 2, it wrote 'bot' even if one of the rows didn't meet the criteria.例如,当 conv_id 为 2 时,即使其中一行不符合条件,它也会写入“bot”。
I tried this code, but it is not working, and the error message is 'over keyword must follow a function call'.我试过这段代码,但它不起作用,并且错误消息是'over keyword must follow a function call'。
CASE
when
author_type = 'bot' and part_type = 'comment'
then 'bot'
over (partition by conversation_id)
end as tag
Thank you谢谢
I edited the post and changed one condition.我编辑了帖子并更改了一个条件。
Consider below query.考虑下面的查询。
if author_type is 'bot' and part_type is 'comment', then label it as 'bot' in a new column for all the rows of that conversation id.
如果 author_type 是 'bot' 而 part_type 是 'comment',那么 label 在该对话 id 的所有行的新列中将其作为 'bot'。
SELECT *,
MAX(IF(author_type = 'bot' AND part_type = 'comment', 'bot', NULL)) OVER (PARTITION BY conv_id) AS tag
FROM sample_table
ORDER BY conv_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.