- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个这样的表格,其中包含几个气候指标(降雨率、温度等)
mysql> select rain_rate, temperature, datetime from weather limit 10;
+--------------+---------------+----------------------+
| rain_rate | temperature | datetime |
+--------------+---------------+----------------------+
| 5.0000000000 | 24.4000000000 | 2017-02-08 16:00:56 |
| 1.0000000000 | 22.4000000000 | 2017-02-06 12:10:36 |
| 2.0000000000 | 28.7000000000 | 2017-02-02 13:57:15 |
| 5.0000000000 | 24.7000000000 | 2017-02-01 14:14:16 |
| 1.0000000000 | 16.1000000000 | 2017-01-08 06:01:26 |
| 2.0000000000 | 18.2000000000 | 2017-01-12 05:10:43 |
| 3.0000000000 | 11.9000000000 | 2017-01-10 06:20:54 |
| 4.0000000000 | 16.8000000000 | 2017-01-25 16:10:14 |
| 5.0000000000 | 24.4000000000 | 2016-12-18 23:10:56 |
| 4.0000000000 | 26.6000000000 | 2016-12-30 09:03:54 |
...
可以看出,时间戳(日期时间字段)不遵循任何模式。
我想获取温度和降雨率按小时的最近 24 个平均值,以及一个包含相关小时数值的列,采用 24 小时格式, 按小时升序排列。
例如,如果我在今天晚上 18:30 执行查询,它应该返回这 24 行:
+-----------------+------------------+-------+
| avg(rain_rate) | avg(temperature) | hour |
+-----------------+------------------+-------+
| 3.5000000000 | 23.1000000000 | 19 | |
| 1.0000000000 | 22.6000000000 | 20 | |
| 3.5000000000 | 24.7000000000 | 21 | |-> hours of "yesterday"
| 4.5000000000 | 23.8000000000 | 22 | |
...
| 2.0000000000 | 26.3000000000 | 13 | |
| 1.5000000000 | 21.6000000000 | 14 | |
| 7.0000000000 | 23.4000000000 | 15 | |-> hours of "today"
| 2.5000000000 | 21.4000000000 | 16 | |
| 7.0000000000 | 21.2000000000 | 17 | |
| 3.0000000000 | 25.3000000000 | 18 | |
到目前为止我最好的尝试:
select avg(rain_rate), avg(temperature), hour(datetime) as hour
from weather
where (datetime >= now() - interval 24 hour)
group by hour(datetime)
order by max(datetime) asc
看起来该查询返回了字段的正确平均值,但是小时字段似乎没有按我需要的那样排序,也没有对应于平均值...
非常感谢任何帮助。提前致谢。
最佳答案
您想在过去 24 小时内按小时计算平均值。
好的。这是一种方法:
select date(datetime), hour(datetime),
avg(rain_rate), avg(temperature)
from weather
where (datetime >= now() - interval 24 hour)
group by date(datetime), hour(datetime)
order by min(datetime);
注意:距当前时间 24 小时可能有点奇怪。您可以获得 25 行记录(两个部分小时)。您可能需要这个 where
:
where datetime < curdate() + interval hour(now()) hour and
datetime >= curdate() + interval hour(now()) - 24 hour
关于mysql - 从列和 GROUP BY 中获取 AVG 的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124968/
我想编写一个 linq 表达式,该表达式将返回不包含特定值的 ID。例如,我想返回所有不具有 Value = 30 的不同 ID。 ID, Value 1, 10 1, 20 1, 30 2,
我正在尝试使用 Regexp 匹配 Nmap 命令的输出。可以有两种不同的格式。 第一种格式(当 nmap 可以找到主机名时) Nmap scan report for 2u4n32t-n4 (192
我正在 Visual Studio 2012 上使用 C# 开发一个软件。我使用 MySQL Connector 6.9.1 进行 MySQL 连接。我的软件在我的操作系统(Win8 x64)上运行顺
在 Django 中(使用 django.contrib.auth 时)我可以添加一个 Group到另一个 Group ?即一个Group成为另一个成员(member) Group ? 如果是这样,我
我试图通过使用动态组参数对数据进行分组来循环。 我们可以在循环的 WHERE 条件上使用动态查询,但我不知道是否可以在组条件中使用动态字符串。 以下是用户决定按哪个字段分组,然后根据决定放置其他逻辑的
我有这样的字符串 s = 'MR1|L2-S1x' 模式总是相同的:一个或两个字符,在 [|.+:x-] 中可选地后跟一个数字和一个分隔符。此模式可以重复 6 次。 所以匹配模式很明确。 p = r'
我有一个带有时间戳字段“bar”的表“foo”。如何仅获取查询的最旧时间戳,例如: SELECT foo.bar from foo?我尝试执行以下操作: SELECT MIN(foo.bar) fro
在我的 Django 项目中,我有一个 user_manage 应用程序。 我在 user_manage 应用的 model.py 中创建了一个名为 UserManage 的模型: from djan
所以我有这样的输入: 还有一个模板指令,例如: 看来我只获得了 foo 和 bar 的组。 (为什么?我预计我可能会得到第三组 current-group-key() = '')。
我正在尝试扩展 django.contrib.auth 并遇到将用户添加到组中的情况,这可以通过两种方式完成。我只是想知道为什么会这样,以及其中一种相对于另一种的优势是什么。 最佳答案 他们做完全相同
我使用的是旧的 PHP 脚本,并且此查询有错误。由于我没有使用 mysql 的经验,因此无法修复它。 "SELECT COUNT(p.postid) AS pid, p.*, t.* FROM ".T
我有几行 Objective-C 代码,例如: ABAddressBookRef addressBook; CFErrorRef error = NULL; addressBook = ABAddre
我正在使用 MariaDB IMDB 电影数据集,我试图解决以下问题。电影表包含 id、名称、排名和年份列 A decade is a sequence of 10 consecutive years
让我从数据开始,以便更好地描述我的需求。我有一个名为 SUPERMARKET 的表,其中包含以下字段: Field 1: StoreID Field 2: ProductCategory Field
你好我有这个查询: SELECT DISTINCT a.id, a.runcd, (SELECT SUM(b.CALVAL) FROM GRS b WHERE b.PCode=11000 AND a.
我想在 xquery 中使用 Group By。有人可以告诉我如何在 Marklogic 中使用 Group By 吗? 最佳答案 或者,您可以使用 xdmp:xslt-invoke 调用 XSLT或
因此,当通过 from sequelize 请求组时,如下所示: return models.WorkingCalendar .findAll({
我希望我解释正确。 我有 2 个表,有 第一个表(table1) +------------+------+-------+-------+ | Date | Item | Block |
我的表 MYTABLE 有 2 列:A 和 B 我有以下代码片段: SELECT MYTABLE.A FROM MYTABLE HAVING SUM(MYTABLE.B) > 100
我有一个简单的行分组查询,需要 0.0045 秒。 300.000 行 从表 GROUP BY cid 中选择 cid 当我添加 MAX() 进行查询时,需要 0.65 秒才能返回。 从表 GROUP
我是一名优秀的程序员,十分优秀!