gpt4 book ai didi

SQL命令按日/月求和

转载 作者:行者123 更新时间:2023-12-02 17:37:06 27 4
gpt4 key购买 nike

我有下表:

+----+---------------------+-------------+-----------------+
| id | stat_time | reads | writes |
+----+---------------------+-------------+-----------------+
| 1 | 2013-07-18 20:00:00 | 42614543 | 1342129 |
| 2 | 2013-07-18 21:00:00 | 23085319 | 326139 |
| 3 | 2013-07-25 12:00:00 | 0 | 39639 |
| 4 | 2013-07-25 13:00:00 | 754166 | 39639 |
| 5 | 2013-07-25 14:00:00 | 693382 | 295323 |
| 6 | 2013-07-25 15:00:00 | 1334462 | 0 |
| 7 | 2013-07-25 16:00:00 | 10748367 | 261489 |
| 9 | 2013-07-25 17:00:00 | 4337294 | 0 |
| 10 | 2013-07-25 18:00:00 | 3002796 | 0 |
| 11 | 2013-07-25 20:00:00 | 3002832 | 0 |
| 12 | 2013-07-25 23:00:00 | 0 | 333468 |
| 13 | 2013-07-26 17:00:00 | 10009585 | 0 |
| 15 | 2013-07-26 18:00:00 | 6005752 | 0 |
| 17 | 2013-07-26 21:00:00 | 333663 | 0 |
+----+---------------------+-------------+-----------------+

我想执行这样的操作:

从 this_table GROUP BY stat_time 中选择 stat_time、SUM(读取)、SUM(写入),其中包含“同一天”..

因此,一个命令输出三行(一行表示 2013-07-18,第二行表示 2013-07-25,第三行表示 2013-07-26),并且在列中的每个此类行中读取/写入有这一天的读/写总和。

谢谢你,大卫

最佳答案

您希望将 stat_time 转换为一天来执行此操作。该方法取决于数据库。这是一种方法:

SELECT cast(stat_time as date) as stat_day, SUM(reads), SUM(writes)
from this_table
GROUP BY cast(stat_time as date)
order by stat_day;

以下是一些其他方法(针对 MySQL 和 Oracle):

SELECT date(stat_time) as stat_day, SUM(reads), SUM(writes)
from this_table
GROUP BY date(stat_time)
order by stat_day;

SELECT trunc(stat_time) as stat_day, SUM(reads), SUM(writes)
from this_table
GROUP BY trunc(stat_time)
order by stat_day;

关于SQL命令按日/月求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17899761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com