【发布时间】:2012-12-19 11:42:02
【问题描述】:
我有一些包含商店信息的记录。这些记录有几个不同的嵌套字段。嵌套字段之一是标签,一个是员工。我正在尝试计算具有标签和具有特定名称的员工的商店数量。所以我这样做了:
SELECT count(*)
FROM [stores.stores_844_1]
where tags.tag_name='foo'
and employees.first_name='bar'
然后我得到错误:
Error: Cannot query the cross product of repeated fields tags.tag_name and employees.first_name.
我可以通过将查询更改为:
SELECT count(*)
FROM ((flatten([stores.stores_844_1],tags))
where tags.tag_name='foo'
and employees.first_name='bar'
问题在于我正在动态创建 where 子句,因此我的 from 子句必须根据我在 where 中的内容进行更改。虽然我可以在代码中生成一些逻辑来确定 from 子句应该是什么,但我想知道是否有办法执行以下操作:
SELECT count(*)
FROM [stores.stores_844_1]
where tags.tag_name='foo' WITHIN RECORD
and employees.first_name='bar' WITHIN RECORD
这样就不用把主表弄平了? 我尝试过像这样使用丑陋的解决方法:
SELECT count(*)
FROM
(SELECT GROUP_CONCAT(CONCAT('>', tags.tag_name,'<')) WITHIN RECORD as f1, GROUP_CONCAT(CONCAT('>',employees.first_name,'<')) WITHIN RECORD as f2
FROM [stores.stores_844_1]
)
where f1 CONTAINS '>foo<'
and f2 CONTAINS '>bar<'
这个丑陋的解决方法可以按我的意愿工作,但它看起来真的很丑陋,必须有更好的方法,对吧?
【问题讨论】:
标签: google-bigquery