面对查询设计问题,不确定我解决问题的方法是否过于复杂:
我有一个事实表:
Column | Type | Modifiers
------------+-----------------------------+-------------------------------------------------------
id | integer | not null default nextval('messages_id_seq'::regclass)
type | character varying(255) |
ts | numeric |
text | text |
score | double precision |
user_id | integer |
channel_id | integer |
time_id | integer |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
我目前正在针对它运行一些分析查询,其中之一(例如)是:
with intervals as (
select
(select '09/27/2014'::date) + (n || ' minutes')::interval start_time,
(select '09/27/2014'::date) + ((n+60) || ' minutes')::interval end_time
from generate_series(0, (24*60*7), 60 * 4) n
)
select
extract(epoch from i.start_time)::numeric * 1000 as ts,
extract(epoch from i.end_time)::numeric * 1000 as end_ts,
sum(avg(messages.score)) over (order by i.start_time) as score
from messages
right join intervals i
on messages.timestamp >= i.start_time and messages.timestamp < i.end_time
where messages.timestamp between '09/27/2014' and '10/04/2014'
group by i.start_time, i.end_time
order by i.start_time
正如你们可能知道的那样 - 此查询计算给定时间段分布的消息的“分数”属性的平均值,然后计算跨段的累积值(使用窗口)。
我接下来要做的是找到messages.text
最接近每个桶的平均值的前 5 个(例如)。
现在,我唯一的计划是:
1) Join messages with the time-buckets
2) Compute a score - avg(score) over (partition by start_time) as deviation and save it against each record of the joined relation
3) Compute a rank() over (order by deviation) as rank
4) Select where rank between 1 and 5
我之所以把这个命令式地按步骤写下来,是因为我第一次尝试设计一个涉及在窗口函数中使用窗口函数的设计(rank() over (partition by start_time, order by score - avg(score) over (partition by start_time))
,我什至不打算尝试看看它是否可行。
我可以就我是否朝着正确的方向寻求一些建议吗?