MySQL 沿对角线对值求和
MySQL sum values diagonally
我有一个 table 看起来像这样:
| created_at | Current_Value | M1 | M2 | M3 | M4 |
| ---------- | ------------- | --- | --- | --- | --- |
| 01/08/2020 | 840 | 840 | 838 | 838 | 838 |
| 01/09/2020 | 65 | 63 | 61 | 59 | 0 |
| 01/10/2020 | 109 | 104 | 99 | 0 | 0 |
| 01/11/2020 | 105 | 100 | 0 | 0 | 0 |
| 01/12/2020 | 61 | 0 | 0 | 0 | 0 |
Current_Value
列存储在 created_at
月创建的项目数,M1
- M4
列存储 Current_Value
值减去该值在接下来的几个月中相对于 created_at
月,因此例如第二行(日期 01/09/2020)中的列 M1
告诉我在 M1(下个月,所以 01/10 /2020) 值是 63,所以它减少了 2 (65 - 63) 等等。
现在我想在 Metabase 中创建一个聚合报告,显示每月项目数量的总体减少 - 意味着总共一行。
现在的问题是,因为我在 Metabase 中创建此报告,所以我不能使用存储例程,也不能使用任何其他语言,所以我只能使用基本的 MySQL.
我需要实现的是对角相加,所以,例如,我需要这样做:
注意:短语 上一行 缩写为 pr
所以更简洁
| created_at | Current_Value | M1 | M2 | M3 | M4 |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/08/2020 | 840 | 840 | 838 | 838 | 838 |
| 01/09/2020 | 65 + M1 in pr | 63 + M2 in pr | 61 + M3 in pr | 59 + M4 in pr | 0 |
| 01/10/2020 | 109+ M1 in pr | 104+ M2 in pr | 99 + M3 in pr | 0 | 0 |
| 01/11/2020 | 105+ M1 in pr | 100+ M2 in pr | 0 | 0 | 0 |
| 01/12/2020 | 61 + M1 in pr | 0 | 0 | 0 | 0 |
现在在正常情况下,我只会使用会话变量来实现这一点,所以我会这样做:
(@varM1 := 0 + @varM1) AS M1_Prev, ( @varM1 := M1) M1_Now
从上一行获取值,以便我可以将其添加到当前行,但是,我在这里需要做的不仅仅是从上一行获取值,而是已经 求和 上一行的值,所以我实际需要做的是这样的:
注意:每个单元格被拉伸到 3 行,但是,它仍然只代表一个单元格
| created_at | Current_Value | M1 | M2 | M3 | M4 |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/08/2020 | 840 | 840 | 838 | 838 | 838 |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/09/2020 | 65 + M1 in pr | 63 + M2 in pr | 61 + M3 in pr | 59 + M4 in pr | 0 |
| | = 65 + 840 | = 63 + 838 | = 61 + 838 | = 59 + 838 | |
| | = 905 | = 901 | = 899 | = 897 | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/10/2020 | 109+ M1 in pr | 104+ M2 in pr | 99 + M3 in pr | 0 | 0 |
| | = 109 + 901 | = 104 + 899 | = 99 + 897 | | |
| | = 1010 | = 1003 | = 996 | | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/11/2020 | 105+ M1 in pr | 100+ M2 in pr | 0 | 0 | 0 |
| | = 105 + 1003 | = 100 + 996 | | | |
| | = 1108 | = 1096 | | | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/12/2020 | 61 + M1 in pr | 0 | 0 | 0 | 0 |
| | = 61 + 1096 | | | | |
| | = 1157 | | | | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
所以我需要得到相同的 table,但值以这种方式聚合,所以我可以处理结果。
在过去的几个小时里,我一直在为这个问题绞尽脑汁,重新安排和创建新的变量来实现这个目标,但不幸的是,我没有成功。
这甚至可以用普通的 MySQL 实现吗?有可能吗,请你指出正确的方向,因为不幸的是我被困住了。
我正在使用 MySQL v5.7.
非常感谢
数据模型使事情变得比应有的困难。虽然这个 可能 可以用 unpivot/pivot 和 window 函数,但我发现用递归查询表达逻辑更简单:
with recursive
data as (select t.*, row_number() over(order by created_at) rn from mytable t),
cte as (
select rn, created_at, current_value, m1, m2, m3, m4 from data where rn = 1
union all
select d.rn, d.created_at,
d.current_value + c.m1, d.m1 + c.m2, d.m2 + c.m3, d.m3 + c.m4, d.m4
from cte c
inner join data d on d.rn = c.rn + 1
)
select * from cte
这基本上是从最早日期到最新日期遍历数据集,同时根据需要跨行执行计算。
rn | created_at | current_value | m1 | m2 | m3 | m4
-: | :--------- | ------------: | ---: | --: | --: | --:
1 | 2020-08-01 | 840 | 840 | 838 | 838 | 838
2 | 2020-09-01 | 905 | 901 | 899 | 897 | 0
3 | 2020-10-01 | 1010 | 1003 | 996 | 0 | 0
4 | 2020-11-01 | 1108 | 1096 | 0 | 0 | 0
5 | 2020-12-01 | 1157 | 0 | 0 | 0 | 0
请注意,window 函数仅在 MySQL 8.0 中可用。
我有一个 table 看起来像这样:
| created_at | Current_Value | M1 | M2 | M3 | M4 |
| ---------- | ------------- | --- | --- | --- | --- |
| 01/08/2020 | 840 | 840 | 838 | 838 | 838 |
| 01/09/2020 | 65 | 63 | 61 | 59 | 0 |
| 01/10/2020 | 109 | 104 | 99 | 0 | 0 |
| 01/11/2020 | 105 | 100 | 0 | 0 | 0 |
| 01/12/2020 | 61 | 0 | 0 | 0 | 0 |
Current_Value
列存储在 created_at
月创建的项目数,M1
- M4
列存储 Current_Value
值减去该值在接下来的几个月中相对于 created_at
月,因此例如第二行(日期 01/09/2020)中的列 M1
告诉我在 M1(下个月,所以 01/10 /2020) 值是 63,所以它减少了 2 (65 - 63) 等等。
现在我想在 Metabase 中创建一个聚合报告,显示每月项目数量的总体减少 - 意味着总共一行。
现在的问题是,因为我在 Metabase 中创建此报告,所以我不能使用存储例程,也不能使用任何其他语言,所以我只能使用基本的 MySQL.
我需要实现的是对角相加,所以,例如,我需要这样做:
注意:短语 上一行 缩写为 pr
所以更简洁
| created_at | Current_Value | M1 | M2 | M3 | M4 |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/08/2020 | 840 | 840 | 838 | 838 | 838 |
| 01/09/2020 | 65 + M1 in pr | 63 + M2 in pr | 61 + M3 in pr | 59 + M4 in pr | 0 |
| 01/10/2020 | 109+ M1 in pr | 104+ M2 in pr | 99 + M3 in pr | 0 | 0 |
| 01/11/2020 | 105+ M1 in pr | 100+ M2 in pr | 0 | 0 | 0 |
| 01/12/2020 | 61 + M1 in pr | 0 | 0 | 0 | 0 |
现在在正常情况下,我只会使用会话变量来实现这一点,所以我会这样做:
(@varM1 := 0 + @varM1) AS M1_Prev, ( @varM1 := M1) M1_Now
从上一行获取值,以便我可以将其添加到当前行,但是,我在这里需要做的不仅仅是从上一行获取值,而是已经 求和 上一行的值,所以我实际需要做的是这样的:
注意:每个单元格被拉伸到 3 行,但是,它仍然只代表一个单元格
| created_at | Current_Value | M1 | M2 | M3 | M4 |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/08/2020 | 840 | 840 | 838 | 838 | 838 |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/09/2020 | 65 + M1 in pr | 63 + M2 in pr | 61 + M3 in pr | 59 + M4 in pr | 0 |
| | = 65 + 840 | = 63 + 838 | = 61 + 838 | = 59 + 838 | |
| | = 905 | = 901 | = 899 | = 897 | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/10/2020 | 109+ M1 in pr | 104+ M2 in pr | 99 + M3 in pr | 0 | 0 |
| | = 109 + 901 | = 104 + 899 | = 99 + 897 | | |
| | = 1010 | = 1003 | = 996 | | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/11/2020 | 105+ M1 in pr | 100+ M2 in pr | 0 | 0 | 0 |
| | = 105 + 1003 | = 100 + 996 | | | |
| | = 1108 | = 1096 | | | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
| 01/12/2020 | 61 + M1 in pr | 0 | 0 | 0 | 0 |
| | = 61 + 1096 | | | | |
| | = 1157 | | | | |
| ---------- | ------------- | ------------- | ------------- | -------------- | --- |
所以我需要得到相同的 table,但值以这种方式聚合,所以我可以处理结果。
在过去的几个小时里,我一直在为这个问题绞尽脑汁,重新安排和创建新的变量来实现这个目标,但不幸的是,我没有成功。
这甚至可以用普通的 MySQL 实现吗?有可能吗,请你指出正确的方向,因为不幸的是我被困住了。
我正在使用 MySQL v5.7.
非常感谢
数据模型使事情变得比应有的困难。虽然这个 可能 可以用 unpivot/pivot 和 window 函数,但我发现用递归查询表达逻辑更简单:
with recursive
data as (select t.*, row_number() over(order by created_at) rn from mytable t),
cte as (
select rn, created_at, current_value, m1, m2, m3, m4 from data where rn = 1
union all
select d.rn, d.created_at,
d.current_value + c.m1, d.m1 + c.m2, d.m2 + c.m3, d.m3 + c.m4, d.m4
from cte c
inner join data d on d.rn = c.rn + 1
)
select * from cte
这基本上是从最早日期到最新日期遍历数据集,同时根据需要跨行执行计算。
rn | created_at | current_value | m1 | m2 | m3 | m4 -: | :--------- | ------------: | ---: | --: | --: | --: 1 | 2020-08-01 | 840 | 840 | 838 | 838 | 838 2 | 2020-09-01 | 905 | 901 | 899 | 897 | 0 3 | 2020-10-01 | 1010 | 1003 | 996 | 0 | 0 4 | 2020-11-01 | 1108 | 1096 | 0 | 0 | 0 5 | 2020-12-01 | 1157 | 0 | 0 | 0 | 0
请注意,window 函数仅在 MySQL 8.0 中可用。