在postgresql上获取日期限制的运行总计
我有一个在Postgresql 9.3上运行的数据库查询,看起来像这样,以便获得会计分录的运行余额:在postgresql上获取日期限制的运行总计,postgresql,Postgresql,我有一个在Postgresql 9.3上运行的数据库查询,看起来像这样,以便获得会计分录的运行余额: select *,(sum(amount) over(partition by ae.account_id order by ae.date_posted, ae.account_id )) as formula0_1_ from account_entry as ae -- where ae.date_posted> '2014-01
select *,(sum(amount) over(partition
by
ae.account_id
order by
ae.date_posted,
ae.account_id
)) as formula0_1_
from
account_entry as ae
-- where ae.date_posted> '2014-01-01'
order by account_id desc, date_posted asc
不带where子句的预期输出为:
id | date | amount | running balance
1 2014-01-01 10 10
2 2014-01-02 10 20
我从where条款中得到的是:
id | date | amount | running balance
2 2014-01-02 10 10
如果我尝试按日期范围(上面注释的位)进行筛选,如何使此查询返回相同的正确结果?您需要首先选择并计算所有数据的运行余额,然后将
WHERE
子句放在外部select
中
SELECT
*
FROM
(SELECT
*,
SUM(amount) OVER (
PARTITION BY
ae.account_id
ORDER BY
ae.date_posted,
ae.account_id
) AS formula0_1_
FROM
account_entry AS ae) AS total
WHERE
total.date_posted > '2014-01-01'
ORDER BY
account_id DESC,
date_posted ASC;
你能展示一个输入数据和预期结果的例子吗?(问题更新为postgres版本和样本数据)