使用 SQL 数据库查询以不同的时间间隔减去相同的列值
Posted
技术标签:
【中文标题】使用 SQL 数据库查询以不同的时间间隔减去相同的列值【英文标题】:Query to subtract same column value at different interval of day with SQL database 【发布时间】:2019-05-27 08:32:23 【问题描述】:在 mysql 中,我想根据另一列“时间戳”减去不同时间间隔的列值。
table structure is :
id | generator_id | timestamp | generated_value
1 | 1 | 2019-05-27 06:55:20 | 123456
2 | 1 | 2019-05-27 07:55:20 | 234566
3 | 1 | 2019-05-27 08:55:20 | 333456
..
..
20 | 1 | 2019-05-27 19:55:20 | 9876908
从上表中我想获取 generate_value 列值,它应该是第一个时间戳的日期和日期的最后一个值的时间戳的差异。
在上面的示例中,我正在查找应该输出为 9,753,452 (9876908 - 123456) 的查询。
一般来说,为了获取我在查询下面使用的第一个值和最后一个值的单个记录
// Below will give me end day value
SELECT * FROM generator_meters where generator_id=1 and timestamp like '2019-05-27%' order by timestamp desc limit 1 ;
//this will give me last day value
SELECT * FROM generator_meters where generator_id=1 and timestamp like '2019-05-27%' order by timestamp limit 1 ;
问题是我应该如何通过从一天的最后一个值减去一天的第一个值来获得最终的生成值。
Expected Output
generator_id | generated_value
1 | 9753452
提前致谢!!
【问题讨论】:
查看这个问题的答案:***.com/questions/26817139/… 标记适当的数据库。 @mkRabbani 用数据库名称作为 SQL 编辑了问题,谢谢!! 从上述结果集中发布您的预期输出。 @mkRabbani : 更新了预期的输出表,谢谢!! 【参考方案1】:在您的示例中,值变得越来越大。如果保证是这样,您可以使用
select max(generated_value) - min(generated_value) as result
from sun_electric.generator_meters
where generator_id = 1
and date(timestamp) = date '2019-05-27';
或者对于多个 ID:
select generator_id, max(generated_value) - min(generated_value) as result
from sun_electric.generator_meters
and date(timestamp) = date '2019-05-27'
group by generator_id
order by generator_id;
如果值不是升序的,那么您可以对 ID 1 使用以下查询:
select last_row.generated_value - first_row.generated_value as result
from
(
select *
from sun_electric.generator_meters
where generator_id = 1
and date(timestamp) = date '2019-05-27'
order by timestamp
limit 1
) first_row
cross join
(
select *
from sun_electric.generator_meters
where generator_id = 1
and date(timestamp) = date '2019-05-27'
order by timestamp desc
limit 1
) last_row;
这是获取多个 ID 的结果的一种方法:
select
minmax.generator_id,
(
select generated_value
from sun_electric.generator_meters gm
where gm.generator_id = minmax.generator_id
and gm.timestamp = minmax.max_ts
) -
(
select generated_value
from sun_electric.generator_meters gm
where gm.generator_id = minmax.generator_id
and gm.timestamp = minmax.min_ts
) as result
from
(
select generator_id, min(timestamp) as min_ts, max(timestamp) as max_ts
from sun_electric.generator_meters
where date(timestamp) = date '2019-05-27'
group by generator_id
) minmax
order by minmax.generator_id;
如果您更喜欢这个,您也可以将子查询移动到 from 子句并加入它们。另一种方法是使用从 MySQL 8 开始可用的窗口函数。
【讨论】:
【参考方案2】:以下脚本将返回过滤后的 ID 和日期的预期结果-
SELECT generator_id,CAST(timestamp AS DATE) ,
(
SELECT generated_value
FROM sun_electric.generator_meters B
WHERE timestamp = max(timestamp)
)
-
(
SELECT generated_value
FROM sun_electric.generator_meters B
WHERE timestamp = min(timestamp)
) AS Diff
FROM sun_electric.generator_meters
WHERE generator_id = 1
AND CAST(timestamp AS DATE) = '2019-05-27'
GROUP BY generator_id,CAST(timestamp AS DATE) ;
如果您希望使用 GROUP BY ID 和日期获得相同的结果,只需删除过滤器,如下所示-
SELECT generator_id,CAST(timestamp AS DATE) ,
(
SELECT generated_value
FROM sun_electric.generator_meters B
WHERE timestamp = max(timestamp)
)
-
(
SELECT generated_value
FROM sun_electric.generator_meters B
WHERE timestamp = min(timestamp)
) AS Diff
FROM sun_electric.generator_meters
GROUP BY generator_id,CAST(timestamp AS DATE) ;
【讨论】:
以上是关于使用 SQL 数据库查询以不同的时间间隔减去相同的列值的主要内容,如果未能解决你的问题,请参考以下文章