PostgreSQL - 显示上一年+当前年度的数据

时间:2013-06-10 13:28:39

标签: sql postgresql postgresql-9.2

我需要制作一个专栏,显示上一年度的利润,直到给定的一周。 所以它将在几周内分割当前年份,它将显示给定周的利润。 更清楚地说,前一年的利润是1000.今年的第一周利润是100,第二周是200,三分之一,一周-100(亏损),依此类推。 所以看起来应该是这样的:

week1|week2|week3|
1100 |1300 |1200 |

我尝试的是:

SELECT
CASE when f1.year = DATE_PART('year', now()) THEN f1.week END as week,
profit as profit
FROM (
SELECT 
DATE_PART('week', so.date_order) as week,
DATE_PART('year', so.date_order) as year,
so.profit as profit
FROM
sale_order as so
GROUP BY
week, year, profit
WHERE
so.date_order >= date_trunc('year', now() - '1 year'::interval)::timestamp::date  and so.date_order <= date_trunc('year', now()+ '1 year'::interval)::timestamp::date-1 
)as f1
GROUP BY
week, profit
ORDER BY
week

但这并不是我需要的,因为它会在每个特定的一周内分割利润。我的意思是它只显示了几个星期的利润,但是我需要这几个星期的利润&#39; +&#39;往年利润&#39;。

我的查询尝试窗口功能:

(
SELECT
x.id as id,week as week, x.last_year_profit + y.running_profit as week_profit
FROM
(
SELECT
min(sol.id) as id,
 --DATE_PART('year',  so.date_order) AS calcyear, DATE_PART('week',  so.date_order) AS calcweek,
sum(sol.price_subtotal - (CASE WHEN sol.account_cost_amount != 0 THEN sol.account_cost_amount ELSE sol.purchase_price END )) as last_year_profit
-- sum(sol.price_subtotal) as price_unit, sum(sol.purchase_price) as purchase_price, sum(sol.account_cost_amount) as account_cost_amount
FROM
sale_order as so
INNER JOIN sale_order_line as sol ON (sol.order_id = so.id)
INNER JOIN res_partner as rp ON (so.partner_id = rp.id)
WHERE EXISTS (
SELECT * FROM  res_partner_category_rel rpcl
WHERE 
rpcl.partner_id=rp.id and rpcl.category_id=37
and (so.date_order >= date_trunc('year', now() - '1 year'::interval)::timestamp::date  and so.date_order <= date_trunc('year', now())::timestamp::date-1 )
and so.state != 'cancel'
)
) as x
CROSS JOIN (
SELECT
date_trunc('week', so.date_order) as week,
sum(sum(sol.price_subtotal - (CASE WHEN sol.account_cost_amount != 0 THEN sol.account_cost_amount ELSE sol.purchase_price END ))) OVER  ( ORDER BY date_trunc('week', so.date_order)) as running_profit

FROM
sale_order as so
INNER JOIN sale_order_line as sol ON (sol.order_id = so.id)
INNER JOIN res_partner as rp ON (so.partner_id = rp.id)
WHERE EXISTS (
SELECT * FROM  res_partner_category_rel rpcl
WHERE 
rpcl.partner_id=rp.id and rpcl.category_id=37
AND so.date_order >= date_trunc('year', now())::timestamp::date
AND    so.date_order <  date_trunc('year', now() + '1 year'::interval)::timestamp::date 
and so.state != 'cancel'
)
GROUP BY
week
) as y
GROUP BY
id, week,week_profit
) as f1

出于某种原因,它不会在几周内分割利润,而是只显示一行总数如下:

week    |week_profit|
20130114| 1500       |

1 个答案:

答案 0 :(得分:4)

运行总和的基本查询

将众所周知的汇总函数sum()用作window function

SELECT week, x.last_year_profit + y.running_profit AS week_profit
FROM (         -- total last year
   SELECT sum(profit) AS last_year_profit
   FROM   sale_order
   WHERE  date_order >= date_trunc('year', now() - interval '1 year')
   AND    date_order <  date_trunc('year', now()) 
   ) x
CROSS JOIN (   -- running sum current year
   SELECT date_trunc('week', date_order) AS week
         ,sum(sum(profit)) OVER (ORDER BY date_trunc('week', date_order))
                                                        AS running_profit
   FROM   sale_order
   WHERE  date_order >= date_trunc('year', now() - interval '1 year')
   AND    date_order <  date_trunc('year', now() + interval '1 year')
   GROUP  BY 1
   ) y;

结果:

week       | week_profit
-----------+------------
2012-01-02 | 1100
2012-01-09 | 1300
2012-01-16 | 1200
...

这里的高级功能是我将窗口聚合函数组合在一个查询级别中 - 即使在单个表达式(!)中,也会导致此{{1对于无辜的眼睛来说,这看起来很奇怪:

SELECT

在这个密切相关的答案中找到有关其如何运作的详细说明:
Postgres window function and group by exception

另请注意我在查询中改进的其他多个详细信息。

->SQLfiddle

Array / crosstab()

在一行中累积所有星期的原始方法是聚合数组中的结果:

sum(sum(profit)) OVER (ORDER BY date_trunc('week', date_order))

结果:

SELECT ARRAY( 
    SELECT x.last_year_profit + y.running_profit  -- only one column
    FROM (
    -- rest like query above
   ) a

或者,更高级,您使用{1100,1300,1200, ...} 查询,如相关答案中所述: PostgreSQL Crosstab Query

许多相关的crossbab答案中的一个,特别是处理时态数据:
Querying row counts segregated by date ranges