Sql 基于条件值返回值

Sql 基于条件值返回值,sql,google-bigquery,Sql,Google Bigquery,我尝试从uid列返回值,每个uid至少出现一次,并带有两个不同的类别变量: +--------+--------+---------+ | uid | type | period | +--------+--------+---------+ | abc123 | event1 | control | | abc123 | event1 | test | | def456 | event1 | control | | def456 | event1 | control | +-

我尝试从uid列返回值,每个uid至少出现一次,并带有两个不同的类别变量:

+--------+--------+---------+
|  uid   |  type  | period  |
+--------+--------+---------+
| abc123 | event1 | control |
| abc123 | event1 | test    |
| def456 | event1 | control |
| def456 | event1 | control |
+--------+--------+---------+
在这种情况下,
abc123
将返回事件1的计数2,因为uid同时出现在测试期间和控制期间,
def456
将不返回计数,因为它只出现在一个期间,给出了以下中间表:

+--------+-----------+
|  uid   | typecount |
+--------+-----------+
| abc123 |         2 |
+--------+-----------+
这是我目前的代码:

with cb as(
  select uid, count(type) as cbuffercount, period
    from `AJG.ABV_buff_wperiods`
    where type="bufferStart" and seq>12 and not uid="null" and not uid="" and period="control"
    group by uid, period
    having count(uid)>1),
tb as(
  select uid, count(type) as tbuffercount, period
    from `AJG.ABV_buff_wperiods`
    where type="bufferStart" and seq>12 and not uid="null" and not uid="" and period="test"
    group by uid, period
    having count(uid)>1),
ci as(
  select uid, count(instance) as cinstancecount, period
    from `AJG.ABV_buff_wperiods`
    where seq>12 and not uid="null" and not uid="" and period="control"
    group by uid, period
    having count(uid)>1),
ti as(
    select uid, count(instance) as tinstancecount, period
    from `AJG.ABV_buff_wperiods`
    where seq>12 and not uid="null" and not uid="" and period="test"
    group by uid, period
    having count(uid)>1)
select uid, cb.cbuffercount, tb.tbuffercount, ci.cinstancecount, ti.tinstancecount,
cb.cbuffercount-tb.tbuffercount as absbufferddx, (cb.cbuffercount/ci.cinstancecount)-(tb.tbuffercount/tb.tinstancecount) as proportionalbufferddx
from
  cb join tb
  using(uid)
where
  cb.uid=tb.uid
order by absbufferddx desc
我还有一个问题,当我试图从中选择变量时(例如,
ci.cinstancecount
),Bigquery无法识别我在
with
子句中定义的最后两个表。我运行了一个查询,包括
cb
tb
都很好。不知道为什么添加两个额外的表会破坏它?

这就是您想要的吗

select uid, count(distinct period)
from t
group by uid
having count(distinct period) >= 2;
如果您想同时计算
事件
期间
,那么我建议您使用字符串咀嚼。BigQuery不支持数组或结构上的
count(distinct)
,因此您最好:

select uid, count(distinct concat(event, '|', period))
from t
group by uid
having count(distinct concat(event, '|', period)) >= 2;

编辑您的问题并将所需结果显示为文本表。您的示例数据和查询没有真正意义--查询非常复杂,不清楚与示例数据的关系是什么。我特别想将其添加到having子句中,因此uid必须至少与控件和测试周期一起出现一次,那么,如果不同的句点=2,我可以使用case子句来计算uid吗?计数(类型)是我想要的,但我只想根据上述条件对其进行计数。