我正在寻找一个查询,该查询将从给定日期开始累计重新激活收入。目前我有以下查询;
SELECT advertisable, EXTRACT(YEAR from day), EXTRACT(MONTH from day), ROUND(SUM(cost)/1e6) FROM adcube dac
WHERE advertisable_eid IN
(SELECT advertisable FROM adcube dac
GROUP BY advertisable HAVING SUM(cost)/1e6 > 100)
GROUP BY advertisable, EXTRACT(YEAR from day), EXTRACT(MONTH from day)
ORDER BY advertisable, EXTRACT(YEAR from day), EXTRACT(MONTH from day)
从此我然后导出到excel并检查帐户,他们已停止花费4个月然后重新激活。然后,我跟踪新的重新启动月份的新收入。
是否可以在不需要Excel的情况下获取SQL查询?
由于
答案 0 :(得分:1)
假设数据中实际存在四个月,您可以使用窗口函数执行此操作。您可以通过获取两个row_numbers()
之间的差异来连续找到 N 事物。这是一个想法:
with t as (
SELECT advertisable, EXTRACT(YEAR from day) as yy, EXTRACT(MONTH from day) as mon,
ROUND(SUM(cost)/1e6) as val
FROM adcube dac
WHERE advertisable_eid IN (SELECT advertisable
FROM adcube dac
GROUP BY advertisable
HAVING SUM(cost)/1e6 > 100
)
GROUP BY advertisable, EXTRACT(YEAR from day), EXTRACT(MONTH from day)
)
select advertisable, min(yy * 10000 + mon) as yyyymm
from (select t.*,
(row_number() over (partition by advertisable order by yy, mon) -
row_number() over (partition by advertisable, val order by yy, mon)
) as grp
from t
)
group by advertisable, grp, val
having count(*) >= 4 and val = 0;