SQL 中每个月的第一个值
我有一个包含时间戳数据值的数据库,每天有多个样本,即
05/01/11 01:00:00 - 1.23
05/01/11 01:12:34 - 0.99
....
05/01/11 23:59:59 - 2.34
05/02/11 00:11:22 - 4.56
我
试图获取每个月第一个样本的列表。我设法在每个月的 1 号获得每个样本的列表:
SELECT
"RecordTime", "FormattedValue"
FROM
CDBHistoric
WHERE
"Id" = 12345 AND EXTRACT (DAY FROM "RecordTime") = 1
这提供了类似的数据
03/01/11 00:00:01 - 1.23
03/01/11 00:12:34 - 0.99
....
04/01/11 00:00:34 - 2.34
04/01/11 00:11:22 - 4.56
,但我无法获得每组的第一个值。
I have a database containing time stamped data values, with multiple samples per day, i.e.
05/01/11 01:00:00 - 1.23
05/01/11 01:12:34 - 0.99
....
05/01/11 23:59:59 - 2.34
05/02/11 00:11:22 - 4.56
etc
I am trying to get a list of the first sample of each month. I have managed to get a list of every sample on the 1st of each month:
SELECT
"RecordTime", "FormattedValue"
FROM
CDBHistoric
WHERE
"Id" = 12345 AND EXTRACT (DAY FROM "RecordTime") = 1
This gives data like
03/01/11 00:00:01 - 1.23
03/01/11 00:12:34 - 0.99
....
04/01/11 00:00:34 - 2.34
04/01/11 00:11:22 - 4.56
but I have been unable to get the first value of each group.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 SQL Server 的解决方案。这个想法是使用相关子查询来识别
每年和每月的最短
RecordTime
。将这个想法转化为您喜欢的 DBMS 应该很容易。编辑:如果您的数据集很大,那么加入子查询应该会快得多,但可能更难转化为您的 DBMS 风格:
编辑 2:以下代码是我上面第一个查询的标准 SQL-92 版本(在 mySQL 上测试):
Here's a solution for SQL Server. The idea is to use a correlated subquery to identify
the minimum
RecordTime
for each year and month. It should be easy to translate the idea to your flavour of DBMS.Edit: If your dataset is large then joining on the subquery should be substantially faster, but might be harder to translate into your flavour of DBMS:
Edit 2: The following code is the Standard SQL-92 version of my first query above (tested on mySQL):