我拥有的数据经过格式化后,在上述数据的整个日期中已经包含了访问者的累计总金额。我想做的是将运行总格式的指标转换为该特定日期内的访问次数。
Current Table
site|Run Tol Visits|Date
-----+--------------+--------
aol | 24 |01/01/19
aol | 57 |01/02/19
aol| 100|01/03/19
aol| 140|01/04/19
cnn | 24 |01/01/19
cnn | 57 |01/02/19
cnn| 100|01/03/19
cnn| 140|01/04/19
fox | 24 |01/01/19
fox | 57 |01/02/19
fox| 100|01/03/19
fox| 140|01/04/19
Desired output
Visits|Date
-------+--------
24|01/01/19
33|01/02/19
43|01/03/19
40|01/04/19
答案 0 :(得分:1)
使用window function lag()
,例如:
with my_table(site, total_visits, date) as (
values
('aol', 24, '01/01/19'),
('aol', 57, '01/02/19'),
('aol', 100, '01/03/19'),
('aol', 140, '01/04/19'),
('cnn', 24, '01/01/19'),
('cnn', 57, '01/02/19'),
('cnn', 100, '01/03/19'),
('cnn', 140, '01/04/19'),
('fox', 24, '01/01/19'),
('fox', 57, '01/02/19'),
('fox', 100, '01/03/19'),
('fox', 140, '01/04/19')
)
select
site,
total_visits,
date,
total_visits- lag(total_visits, 1, 0) over (partition by site order by date) as visits
from my_table;
site | total_visits | date | visits
------+--------------+----------+--------
aol | 24 | 01/01/19 | 24
aol | 57 | 01/02/19 | 33
aol | 100 | 01/03/19 | 43
aol | 140 | 01/04/19 | 40
cnn | 24 | 01/01/19 | 24
cnn | 57 | 01/02/19 | 33
cnn | 100 | 01/03/19 | 43
cnn | 140 | 01/04/19 | 40
fox | 24 | 01/01/19 | 24
fox | 57 | 01/02/19 | 33
fox | 100 | 01/03/19 | 43
fox | 140 | 01/04/19 | 40
(12 rows)