0 sql if-statement google-bigquery
假设我有一个类似BigQuery文档中提到的模式:
Last modified Schema Total Rows Total Bytes Expiration
----------------- ----------------------------------- ------------ ------------- ------------
27 Sep 10:01:06 |- kind: string 4 794
|- fullName: string (required)
|- age: integer
|- gender: string
+- phoneNumber: record
| |- areaCode: integer
| |- number: integer
+- children: record (repeated)
| |- name: string
| |- gender: string
| |- age: integer
+- citiesLived: record (repeated)
| |- place: string
| +- yearsLived: integer (repeated)
Run Code Online (Sandbox Code Playgroud)
假设我们有fullNames:John,josh,harry
城市生活:纽约,芝加哥,西雅图
我如何迭代citiesLived并使用条件计数.例如,我想计算有多少用户使用fullName = John生活在citiesLived.place = newyork和citiesLived.place = chicago,但还没有住在citiesLived.place = seattle.
谢谢,约翰
您可以使用OMIT IF关键字.(这是未记录的,我将提交一个错误,以确保它被记录)
SELECT COUNT(*) FROM (
SELECT fullname,
IF (citiesLived.place == 'newyork', 1, 0) as ny,
IF (citiesLived.place == 'chicago', 1, 0) as chi
FROM (FLATTEN(name_table, citiesLived))
OMIT RECORD IF citiesLived.place = 'seattle')
WHERE fullname = 'John'
AND ny == 1
AND chi == 1
Run Code Online (Sandbox Code Playgroud)