SQL 计算指标随时间的增长率

SQL Calculate the rate of growth of the indicator over time

我有一个像这样的 postgres table:

dates
date
name
text
values
real
2017-05-01 A 1
2017-05-02 A 3
2017-05-02 B 10
2017-05-03 A 6
2017-05-04 A 12
2017-05-03 B 10
2017-05-04 B 10
2017-05-05 B 11

如何使用SQL计算指标随时间的增长率,并得到以下table

dates
date
name
text
values
real
growth
real
2017-05-01 A 1 NULL
2017-05-02 A 3 2
2017-05-02 B 10 NULL
2017-05-03 A 6 3
2017-05-04 A 12 6
2017-05-03 B 10 0
2017-05-04 B 10 0
2017-05-05 B 11 1

A​​ 的例子:

2017-05-01 (1)
2017-05-02 (3)
2017-05-03 (6)
2017-05-04 (12)
我正在计算相邻日期之间的度量差异并得到以下结果
2017-05-01(空)
2017-05-02 (3-1 =2)
2017-05-03 (6-3 = 3)
2017-05-04 (12-6=6)

如果您的 table 之前的日期没有间隔,您可以在此处使用 LAG() window 函数:

试试这个:

select 
*, 
values-(lag(values) over(partition by name order by dates)) "growth" 
from test

DEMO