我有一个视图,用于生成每周运行的关键任务cron的验证报告。该视图显示每周运行一次, 贷方帐户总数, (我的功劳归功于SO伙伴!),OPEN帐户的总和,已关闭帐户的总和,基本内容。
mysql> select * from view_command_OPEN_CLOSED_tally limit 5;
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| created | total_accounts_credited | total_amount_credited | total_open_credited | total_amount_open | total_closed_credited| total_amount_closed|
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| 2019-01-19 00:00:00 | 18175 | 3173.68 | 16953 | 7063.68 | 1222 | 110.00 |
| 2019-01-12 00:00:00 | 18135 | 4768.43 | 17053 | 9358.43 | 1082 | 410.00 |
| 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | 1497.75 | 0 | 0.00 |
| 2019-01-09 09:20:55 | 51 | 933.50 | 50 | 915.75 | 1 | 17.75 |
| 2019-01-08 16:45:14 | 10 | 187.50 | 10 | 187.50 | 0 | 0.00 |
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
我认为我可以轻松地从该视图中生成第二个视图,并且可以使用子查询显示与上周百分比的差异。 IE浏览器: 已创建,已计总贷方,上周差额,上周贷方,已计总贷方,上周贷方额,上周等... 我无法获得前一周的结果来匹配数据并计算百分比。结果集将支付前几行 我尝试过的所有查询都围绕着基本的左联接,但是由于某些原因,“上周”的结果未在适当的位置显示。 很明显,我的方法对MySQL表现出不良的态度,作为回应,它使我感到不连贯。
mysql> SELECT vtally.created,
-> vtally.total_accounts_credited,
-> vtally2.total_accounts_credited `previous`,
-> vtally.total_open_credited ,
-> vtally2.total_open_credited `previous`,
-> vtally.total_closed_credited,
-> vtally2.total_closed_credited`previous`
-> FROM view_command_OPEN_CLOSED_tally vtally
-> LEFT JOIN view_command_OPEN_CLOSED_tally vtally2
-> ON vtally.created = vtally2.created - INTERVAL 7 DAY
-> GROUP BY
-> DATE(vtally.created)
-> ORDER BY vtally.created DESC LIMIT 2;
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | NULL | 6953 | NULL | 222 | NULL |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
上面的结果集是一个示例, 我希望结果如下,每一个先前的列都将其前一周的结果保留在其左边:
(仅顶部行,不考虑底部行)
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | 8135 | 6953 | 7053 | 222 | 082 |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
在适当的列中具有适当的“上一个”总数时,我缺少什么?
我相信这可能是Group BY,但不知道从哪里去,似乎已经尝试了所有选择。
答案 0 :(得分:3)
如果vtally2
代表先前的数据,则表示vtally.created > vtally2.created
。
因此,在联接中将减号更改为加号:vtally.created = vtally2.created + INTERVAL 7 DAY
。
如果您使用窗口函数在子查询中计算列,则也可以忽略间隔为非恒定。
类似这样的东西:
WITH numbered_tally AS (
select created, total_accounts_credited, total_open_credited, total_closed_credited,
row_number() OVER (ORDER BY created) AS tally_number
from view_command_OPEN_CLOSED_tally
)
SELECT vtally.created,
vtally.total_accounts_credited,
vtally2.total_accounts_credited `previous`,
vtally.total_open_credited ,
vtally2.total_open_credited `previous`,
vtally.total_closed_credited,
vtally2.total_closed_credited `previous`
FROM numbered_tally vtally
LEFT JOIN numbered_tally vtally2
ON vtally.tally_number = 1 + vtally2.tally_number