如何在每个时间段内选择和汇总历史表中的最新项目? [英] How to select and sum() most recent item in a history table for each time period?

查看:122
本文介绍了如何在每个时间段内选择和汇总历史表中的最新项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当另一个表中的条目发生更改时,我有一个包含单个条目的历史记录表。我需要执行一个查询,以生成每个时间段内最新条目的sum()或count()。

I have a history table containing a single entry when an entry in another table changes. I need to perform a query that produces the sum() or count() of the most recent entries for each time period.

以下是我的表结构的相关位:

Here's the relevant bits of my table structure:

CREATE TABLE opportunity_history (
  "id" BIGSERIAL PRIMARY KEY,
  "opportunity_id" TEXT NOT NULL,
  "employee_external_id" TEXT NOT NULL,
  "item_date" TIMESTAMP NOT NULL,
  "amount" NUMERIC(18,2) NOT NULL DEFAULT 0
);

例如,如果我在1月创建了一个机会,并在2月更新了两次,计数一次,一月一次,二月一次。

So for example if I have a single opportunity created in January, and updated twice in February, I want to count it once in Jan, and only once in Feb.

我有其他类似的查询(不涉及历史-只是单个数据点),效果很好在单个查询中加入generate_series()。我希望能够实现类似的目标。这是使用generate_series的示例:

The other similar queries I have (which don't involve history - just singular data points) work fine by joining to a generate_series() in a single query. I would love to be able to achieve something similar. Here's an example using generate_series:

SELECT Periods.day, sum(amount) as value
FROM (
        SELECT generate_series('2012-01-01', '2013-01-01', '1 month'::interval)::date AS day
    ) as Periods LEFT JOIN opportunity ON (
      employee_external_id='...'
AND   close_date >= Periods.day
AND   close_date <  Periods.day
)
GROUP BY 1
ORDER BY 1

但是,这对契机历史记录无效,因为如果一个项目在同一个月内被列出两次,则您得到

However that doesn't work for opportunity_history, because if a single item is listed in the same month twice you get duplication.

我真的很为难。我尝试通过WITH RECURSIVE进行操作,但似乎没有什么适合我。

I'm really stumped on this one. I've tried doing it via WITH RECURSIVE and nothing seems to unfold properly for me.

编辑:

示例数据(跳过id列并使用日期而不是时间戳):

Example data (skipping id columns and using dates instead of timestamps):

'foo', 'user1', 2013-01-01, 100
'bar', 'user1', 2013-01-02, 50
'foo', 'user1', 2013-01-12, 100
'bar', 'user1', 2013-01-13, 55
'foo', 'user1', 2013-01-23, 100
'foo', 'user1', 2013-02-04, 100
'foo', 'user1', 2013-02-15, 100
'bar', 'user1', 2013-03-01, 55

总和,我想要:

2013-01   155 (foo on 2013-01-23 and bar on 2013-01-13)
2013-02   100 (foo on 2013-02-15)
2013-03   55  (bar on 2013-03-01)

或计数:

2013-01   2
2013-02   1
2013-03   1

还请注意,我很高兴使用扩展 SQL,例如CTE或WITH RECURSIVE或窗口函数需要。如果我可以在单个查询中这样做,我宁愿避免在Pg / plsql函数中出现循环。

Also note I'm happy to use "extended" SQL such as CTEs or WITH RECURSIVE or window functions if required. I'd rather avoid a loop in a Pg/plsql function if I can do it in a single query.

推荐答案

select item_month, count(*), sum(amount)
from (
   select opportunity_id, 
          item_date,
          amount,
          to_char(item_date, 'yyyy-mm') as item_month,
          row_number() over (partition by opportunity_id, to_char(item_date, 'yyyy-mm') order by item_date desc) as rn
   from opportunity_history
) t
where rn = 1
group by item_month
order by 1;

SQLFiddle示例: http://sqlfiddle.com/#!15/c4152/1

SQLFiddle example: http://sqlfiddle.com/#!15/c4152/1

这篇关于如何在每个时间段内选择和汇总历史表中的最新项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆