简体   繁体   English

SQL/BigQuery:当语句通过分区时的情况

[英]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 conv_id element_id element_id author_type作者类型 part_type部分类型
1 1 11 11 bot机器人 comment评论
1 1 12 12 bot机器人 comment评论
2 2 22 22 bot机器人 comment评论
2 2 23 23 bot机器人 comment评论
2 2 24 24 admin行政 note笔记
3 3 32 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 conv_id element_id element_id author_type作者类型 part_type部分类型 tag标签
1 1 11 11 bot机器人 comment评论 bot机器人
1 1 12 12 bot机器人 comment评论 bot机器人
2 2 22 22 bot机器人 comment评论 bot机器人
2 2 23 23 bot机器人 comment评论 bot机器人
2 2 24 24 admin行政 note笔记 bot机器人
3 3 32 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.

相关问题 Bigquery 中的案例陈述 - Case statement in Bigquery Bigquery SQL:何时使用聚合 Function 而非分析 Function 而非子查询? - Bigquery SQL : When to use Aggregated Function over Analytics Function over Subquery? 选择需要对分区列进行过滤的 Bigquery 表的最新分区 - choose latest partition of a Bigquery table where filter over partition column is required 如何在 SQL [BQ] 中的语句中应用 ifnull 内部 case? - How to apply ifnull inside case when statement in SQL [BQ]? 如何在 BigQuery SQL 中捕获失败的 CAST 语句? - How to catch a failed CAST statement in BigQuery SQL? 写入 Bigquery 时间单位列分区时,Dataflow 不会创建空分区 - Dataflow doesn’t create an empty partition when writing to a Bigquery time-unit column partition Google BigQuery SQL 根据 if/then 语句创建多个“列”? - Google BigQuery SQL create multiple "columns" depending on if/then statement? BigQuery SQL:如何在 CASE 表达式中使用不同的列 - BigQuery SQL: How to use different columns in CASE expression 如何在 bigquery sql 中使用 windows function 在一段时间内获取聚合值的差异? - How to get difference in aggregated value over a certain period of time using windows function in bigquery sql? 查询以在 bigquery 中获取表分区元数据时出错 - Getting error while querying to get tables partition metadata in bigquery
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM