Я пытаюсь вернуть значения из столбца uid, основываясь на том, что каждый uid появляется хотя бы один раз с двумя разными категориальными переменными:
+--------+--------+---------+
| uid | type | period |
+--------+--------+---------+
| abc123 | event1 | control |
| abc123 | event1 | test |
| def456 | event1 | control |
| def456 | event1 | control |
+--------+--------+---------+
В этом случае abc123
вернет счетчик 2 длясобытие1, поскольку идентификатор пользователя отображается как в тестовом периоде, так и в контрольном периоде, 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
У меня также есть дополнительная проблема, когда Bigquery не распознает последние 2 таблицы, которые я определил в предложении with
, когда я пытаюсь выбрать из них переменные (например, ci.cinstancecount
).Я выполнил запрос, включающий cb
и tb
просто отлично.Не знаете, почему добавление 2 дополнительных таблиц нарушает его?