Google bigquery 同一列上的多个IF
一个愚蠢的问题: 我必须在同一列中分组不同的if语句。我该怎么办 if(string='AAA','A','other'),if(string='BBB','B','other')作为第1列Google bigquery 同一列上的多个IF,google-bigquery,Google Bigquery,一个愚蠢的问题: 我必须在同一列中分组不同的if语句。我该怎么办 if(string='AAA','A','other'),if(string='BBB','B','other')作为第1列 谢谢 从中签出模拟透视表的部分 使用条件语句将子选择查询的结果组织为行和列。在下面的示例中,搜索以值“Google”开头的大多数修订版Wikipedia文章的结果被组织到列中,如果它们符合各种标准,则在列中显示修订计数 SELECT page_title, /* Populate these col
谢谢 从中签出模拟透视表的
部分
使用条件语句将子选择查询的结果组织为行和列。在下面的示例中,搜索以值“Google”开头的大多数修订版Wikipedia文章的结果被组织到列中,如果它们符合各种标准,则在列中显示修订计数
SELECT
page_title,
/* Populate these columns as True or False, */
/* depending on the condition */
IF (page_title CONTAINS 'search',
INTEGER(total), 0) AS search,
IF (page_title CONTAINS 'Earth' OR
page_title CONTAINS 'Maps', INTEGER(total), 0) AS geo,
FROM
/* Subselect to return top revised Wikipedia articles */
/* containing 'Google', followed by additional text. */
(SELECT
TOP (title, 5) as page_title,
COUNT (*) as total
FROM
[publicdata:samples.wikipedia]
WHERE
REGEXP_MATCH (title, r'^Google.+') AND wp_namespace = 0
);
返回:
+---------------+--------+------+
| page_title | search | geo |
+---------------+--------+------+
| Google search | 4261 | 0 |
| Google Earth | 0 | 3874 |
| Google Chrome | 0 | 0 |
| Google Maps | 0 | 2617 |
| Google bomb | 0 | 0 |
+---------------+--------+------+
从中签出模拟数据透视表的部分
使用条件语句将子选择查询的结果组织为行和列。在下面的示例中,搜索以值“Google”开头的大多数修订版Wikipedia文章的结果被组织到列中,如果它们符合各种标准,则在列中显示修订计数
SELECT
page_title,
/* Populate these columns as True or False, */
/* depending on the condition */
IF (page_title CONTAINS 'search',
INTEGER(total), 0) AS search,
IF (page_title CONTAINS 'Earth' OR
page_title CONTAINS 'Maps', INTEGER(total), 0) AS geo,
FROM
/* Subselect to return top revised Wikipedia articles */
/* containing 'Google', followed by additional text. */
(SELECT
TOP (title, 5) as page_title,
COUNT (*) as total
FROM
[publicdata:samples.wikipedia]
WHERE
REGEXP_MATCH (title, r'^Google.+') AND wp_namespace = 0
);
返回:
+---------------+--------+------+
| page_title | search | geo |
+---------------+--------+------+
| Google search | 4261 | 0 |
| Google Earth | 0 | 3874 |
| Google Chrome | 0 | 0 |
| Google Maps | 0 | 2617 |
| Google bomb | 0 | 0 |
+---------------+--------+------+
用例语句
Select
Case
When string="AAA" then "A_String"
When string="BBB" then "B_String"
When String in ("CCC","DDD") then "C_OR_D_String"
Else "Unknow_String"
End AS String_Class
From My_Table
希望这有助于使用案例陈述
Select
Case
When string="AAA" then "A_String"
When string="BBB" then "B_String"
When String in ("CCC","DDD") then "C_OR_D_String"
Else "Unknow_String"
End AS String_Class
From My_Table
希望这有帮助,不是一个愚蠢的问题。。。正如您在清理BQ中的数据时所期望的那样,这种情况更加频繁
即使其他答案对于你的问题来说是一个很好的解决方法,如果你想用if语句编写你的查询,这将是一种方法:
IF(string = 'AAA','A',(IF(string = 'BBB','B,'other'))) as COLUMN,
在本例中,您只是嵌套IF语句,只要在条件无效时,两个IF语句的“other”值相同,它就可以工作 这不是一个愚蠢的问题。。。正如您在清理BQ中的数据时所期望的那样,这种情况更加频繁
即使其他答案对于你的问题来说是一个很好的解决方法,如果你想用if语句编写你的查询,这将是一种方法:
IF(string = 'AAA','A',(IF(string = 'BBB','B,'other'))) as COLUMN,
在本例中,您只是嵌套IF语句,只要在条件无效时,两个IF语句的“other”值相同,它就可以工作