- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个结构表:
id | date | player_id | score
--------------------------------------
1 | 2019-01-01 | 1 | 1
2 | 2019-01-02 | 1 | 1
3 | 2019-01-03 | 1 | 0
4 | 2019-01-04 | 1 | 0
5 | 2019-01-05 | 1 | 1
6 | 2019-01-06 | 1 | 1
7 | 2019-01-07 | 1 | 0
8 | 2019-01-08 | 1 | 1
9 | 2019-01-09 | 1 | 0
10 | 2019-01-10 | 1 | 0
11 | 2019-01-11 | 1 | 1
我想再创建两个列,“total_score”、“last_seven_days”。
total_score 是 player_id 得分的滚动总和
last_seven_days 是过去 7 天的得分,包括日期之前和之前
我编写了以下 SQL 查询:
SELECT id,
date,
player_id,
score,
sum(score) OVER all_scores AS all_score,
sum(score) OVER last_seven AS last_seven_score
FROM scores
WINDOW all_scores AS (PARTITION BY player_id ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),
last_seven AS (PARTITION BY player_id ORDER BY id ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING);
得到如下输出:
id | date | player_id | score | all_score | last_seven_score
------------------------------------------------------------------
1 | 2019-01-01 | 1 | 1 | |
2 | 2019-01-02 | 1 | 1 | 1 | 1
3 | 2019-01-03 | 1 | 0 | 2 | 2
4 | 2019-01-04 | 1 | 0 | 2 | 2
5 | 2019-01-05 | 1 | 1 | 2 | 2
6 | 2019-01-06 | 1 | 1 | 3 | 3
7 | 2019-01-07 | 1 | 0 | 4 | 4
8 | 2019-01-08 | 1 | 1 | 4 | 4
9 | 2019-01-09 | 1 | 0 | 5 | 4
10 | 2019-01-10 | 1 | 0 | 5 | 3
11 | 2019-01-11 | 1 | 1 | 5 | 3
我意识到我需要改变这个
last_seven AS(PARTITION BY player_id ORDER BY id ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING)
而不是 7,使用某种日期格式,因为只有数字 7 会引入错误。
即能够执行 date - 2days
或 date - 6days
我还想添加 3 个月、6 个月、12 个月等列,因此需要它能够动态显示。
最佳答案
Postgres 11+ 的解决方案:
像@LaurenzAlbe 那样使用RANGE 间隔
Postgres <11 的解决方案:
(只呈现“days”部分,“all_scores”部分相同)
在 player_id
和相关的 date
范围内加入针对自身的表格:
SELECT s1.*,
(SELECT SUM(s2.score)
FROM scores s2
WHERE s2.player_id = s1.player_id
AND s2."date" BETWEEN s1."date" - interval '7 days' AND s1."date" - interval '1 days')
FROM scores s1
关于postgresql - 多个窗函数的累加和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866349/
我对编程非常陌生(所以我提前道歉),并且我无法弄清楚如何创建一个 for 循环来执行以下操作: 我要求用户输入两个变量(我将它们称为 x 和 y),然后我计算 x/y = z。我想提出这个两个变量输入
我正在尝试对 vector 使用累加函数 vector A; double B = 0; A.reserve(100); for(itr = 0; itr < 210; itr++) { t
如果我想累积 std::vector 的绝对值,我可以使用 lambda 来计算绝对值并将其添加到 std::accumulate #include int main (){ std::ve
所以我需要使用 accumulate 对 vector 中的一些 double 值求和,其中我的 VECTOR 实际上是指向对象的指针。 现在,当我将 accumulate 与 int 一起用于 in
假设我有一个 (None, 2)-shape 张量 indices 和 (None,)-shape 张量 values。这些实际行号和值将在运行时确定。 我想设置一个 4x5 张量 t,索引的每个元素
我有一小部分固定节点: , , , .每个节点的值可以是 1 或 0。此外,每个节点的权重分别为:1、2、3、4。不使用节点属性。如何使用 XSLT 1.0 将每个节点的值乘以其权重相加?示例:
目前我在下面有一个数据集,如果 ColA 为 0,我尝试累加该值,而如果 ColA 再次为 1,则将值重置为 0(再次重新开始计数)。 ColA 1 0 1
我是一名优秀的程序员,十分优秀!