【发布时间】:2018-08-01 23:22:45
【问题描述】:
这是我正在处理的一些伪 SQL 的示例。
select count(*) as "count", time2.iso_timestamp - time1.iso_timestamp
as "time_to_active",
case
when ("time_to_active" >= 1day and "time_to_active" <= 5days) then '1'
when ("time_to_active" >= 6days and "time_to_active" <= 11days) then
'2'
when ("time_to_active" >= 12days and "time_to_active" <= 20days) then
'3'
when ("time_to_active" >= 21days and "time_to_active" <= 30days) then
'4'
when ("time_to_active" >= 31days) then '5'
end as timetoactivegroup
from t
inner join t1 on t.p_id = t1.p_id
join timestamp time1 on t.timestamp_id = t1.id
join timestamp time2 on t1.timestamp_id = t2.id
我实际上是在尝试查询计算列适合某个范围的组。 n 和 y 天之间的订单。我遇到的主要问题是根据分组生成计数。
我可以毫无问题地让选择查询显示计算值。
【问题讨论】:
-
我不确定您要做什么。您能否详细说明您对样本数据和预期输出的问题?您正在尝试将一些持续时间分组到不同的组中,然后您想计算每个组包含多少个元素?曾经我遇到过类似的问题。我将组作为范围类型放入一个表中,并使用
JOIN ON a.range @> b.element加入它,结果为a.id as group_id。第二步:group by group_id
标签: database postgresql data-warehouse snowflake-cloud-data-platform