原始数据
SELECT NAME,
p_date,
cost,
sum(cost) over() AS sample1, --所有行相加
sum(cost) over(PARTITION BY NAME) AS sample2,--按name分组,组内所有行相加
sum(cost) over(PARTITION BY NAME ORDER BY p_date) AS sample3,--按name分组,组内按日期累加(有相同日期也累加)
sum(cost) over(PARTITION BY NAME ORDER BY p_date,cost) AS sample8,--按name分组,组内按顺序累加
sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sample4,--和sample3一样,由起点到当前行的聚合
sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS sample5,--前面一行和当前行做聚合
sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sample6,--前面一行和后面一行做聚合
sum(cost) over(PARTITION BY NAME ORDER BY p_date ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS sample7 --当前行和后面所有行
FROM tmp_export.test123
注意下红框的数据,相同日期