我有一个表格,将人们输入体重的日期存储到我们的应用程序中,我需要弄清楚如何(或者是否)我可以编写一个查询来确定他们称重的连续周数。
有许多资源提供了一般如何计算条纹的示例,但每周部分让我感到非常沮丧。老实说,我甚至不确定从哪里开始。我的伪代码看起来像是:“计算连续几周的数量至少有1个称重”,但是连续几周的部分很棘手。
有没有人实现过这样的东西?
PostgreSQL 9.3是我正在使用的。称量日期为TIMESTAMP WITHOUT TIME ZONE
类型。
答案 0 :(得分:0)
实际上你问过一个立方体和sql-query这不是一个好主意但是
select log_id,
sum(
case when "dt" between '2012-01-01' and '2012-01-01'+interval'1 month' and "dr">=user.registration_date::date and exists(select 1 from user.activity ph where ph.user_id = log_id and ph.dt::date between '2012-01-01' and '2012-01-01'+interval'1 month' ) then 1
when "dt" between '2012-01-01' and '2012-01-01'+interval'1 month' and "dt">=user.registration_date::date and not exists(select 1 from user.activity ph where ph.user_id = log_id and ph.dt::date between '2012-01-01' and '2012-01-01'+interval'1 month' ) then 0
when "dt" between '2012-01-01' and '2012-01-01'+interval'1 month' and "dt"< user.registration_date::date then -1 else null end) as "2012-01",
sum(case when "dt" between '2012-02-01' and '2012-02-01'+interval'1 month' and "dt">=user.registration_date::date and exists(select 1 from user.activity ph where ph.user_id = log_id and ph.dt::date between '2012-02-01' and '2012-02-01'+interval'1 month' ) then 1
when "dt" between '2012-02-01' and '2012-02-01'+interval'1 month' and "dt">=user.registration_date::date and
not exists(select 1 from user.activity ph where ph.user_id = log_id and ph.dt::date between '2012-02-01' and '2012-02-01'+interval'1 month' ) then 0
when "dt" between '2012-02-01' and '2012-02-01'+interval'1 month' and "dt"<user.registration_date::date then -1 else null end) as "2012-02"
from user.activity where dt between '2012-01-01' and '2012-03-01' group by log_id
按月计算的用户活动示例
矩阵
Value >0
- 用户已在所选月份注册并拥有活动
Value =0
用户已注册,但在所选月份内没有活动
Value < 0
用户尚未在所选月份注册