SQL查找两行之间的差异
我有一个 Informix 数据库,其中包含多个不同位置的测量温度值。 所有位置每 15 分钟进行一次测量,然后将时间戳加载到同一个表中。 表格如下所示:
locId dtg temp aaa 2009-02-25 10:00 15 bbb 2009-02-25 10:00 20 ccc 2009-02-25 10:00 24 aaa 2009-02-25 09:45 13 ccc 2009-02-25 09:45 16 bbb 2009-02-25 09:45 18 ddd 2009-02-25 09:45 12 aaa 2009-02-25 09:30 11 ccc 2009-02-25 09:30 14 bbb 2009-02-25 09:30 15 ddd 2009-02-25 09:30 10
现在我想要一个查询,向我显示所有站点最后两次测量之间的温度变化。 而且,只有那些具有更新测量值的。 例如,在上表中,不会包含位置 ddd。 所以结果就变成了:
locId change aaa 2 bbb 2 ccc 8
我尝试了很多,但找不到任何好的解决方案。 实际上,网页会询问大约 700 个位置,因此我认为查询需要相当高效。
非常感谢一些帮助!
//杰斯帕
I have an Informix database containing measured temperature values for quite a few different locations. The measurements are taken every 15 min for all locations and then loaded with a timestamp into the same table. Table looks like this:
locId dtg temp aaa 2009-02-25 10:00 15 bbb 2009-02-25 10:00 20 ccc 2009-02-25 10:00 24 aaa 2009-02-25 09:45 13 ccc 2009-02-25 09:45 16 bbb 2009-02-25 09:45 18 ddd 2009-02-25 09:45 12 aaa 2009-02-25 09:30 11 ccc 2009-02-25 09:30 14 bbb 2009-02-25 09:30 15 ddd 2009-02-25 09:30 10
Now I would like a query that present me with the change in temperature between the two last measurements for all stations. And also, only the ones that has an updated measurement. For example in the table above, location ddd would not be included.
So the result becomes:
locId change aaa 2 bbb 2 ccc 8
I have tried alot but I can´t find any good solution. In reality it is about 700 locations that is asked from a web page so I think the query needs to be fairly efficient.
Would really appreciate some help!
//Jesper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设所有时间都是准确的
assumes that all times will be exact
感谢uglysmurf 提供SQL 格式的数据。
使用 IDS (IBM Informix Dynamic Server) 版本 11.50,以下查询有效。
结果(比请求的列更多,但您可以轻松修剪选择列表):
请注意,此解决方案不依赖于 CURRENT(或 NOW); 它适用于最新记录的数据。 SELECT 语句中 IDS 特定的唯一部分是“
+ 15 UNITS MINUTE
”; 在 Informix 中也可以写为“+ INTERVAL(15) MINUTE TO MINUTE
”,在标准 SQL 中也可以写为“+ INTERVAL '15' MINUTE
”(如果 DBMS支持 INTERVAL 类型)。 表中 DATETIME YEAR TO MINUTE 的使用是 Informix 特定的; 在这样的上下文中,不存储您不感兴趣的信息(例如秒)是有用的。Thanks to uglysmurf for providing the data in an SQL format.
Using IDS (IBM Informix Dynamic Server) version 11.50, the following query works.
Results (more columns than requested, but you can easily trim the select list):
Note that this solution does not depend on CURRENT (or NOW); it works on the latest recorded data. The only part of the SELECT statement that is IDS-specific is the '
+ 15 UNITS MINUTE
'; that could also be written as '+ INTERVAL(15) MINUTE TO MINUTE
' in Informix, and as '+ INTERVAL '15' MINUTE
' in standard SQL (if the DBMS supports INTERVAL types). The use of DATETIME YEAR TO MINUTE in the table is Informix-specific; in a context like this, it is useful not to store information you are not interested in (such as the seconds).在伪 SQL 中,您可以执行以下查询:
不确定您是否将其定义为“好的”解决方案,但如果每个位置有两个数据点,它应该可以工作。
In pseudo-SQL you could do the query:
Not sure if you define this as a 'good' solution, but it should work providing there are two data points for each location.
编辑:基本上和 BCS 一样,打败我!
Edit: same as BCS basically, beat me to it!
我不相信 Informix 具有像 Oracle 这样的分析函数,但如果有的话,这将是使用它们的绝佳场所。 以下是使用分析函数 lag 和 max 的 Oracle 示例。
设置脚本:
使用分析函数的 Oracle 特定查询:
结果:
有关 Oracle 分析的良好资源:http:// /www.psoug.org/reference/analytic_functions.html
I don't believe Informix has analytic functions like Oracle, but if it did this would be a excellent place to use them. What follows is an Oracle example using the analytic functions lag and max.
Setup script:
Oracle-specific query using analytic functions:
Result:
Good resource on Oracle analytics: http://www.psoug.org/reference/analytic_functions.html
尝试一下这样的事情。 它可能不是超级高效,但与其他一些答案不同,它会返回每个 LocID 的 diff
警告:我使用 tSQL 编写了此代码,但为了可移植到 Informix,尝试尽可能坚持标准 ANSI SQL。
Give something like this a try. It may not be super efficient, but unlike some of the other answers it will return the dif for each LocID
Caveat: I wrote this using tSQL, but tried to stick to standard ANSI SQL as much as possible for portability to Informix.