【发布时间】:2016-02-11 13:40:36
【问题描述】:
我想在 sql 中计算平均值。我在表中有以下记录。
让表名 = frarecord
在上表中,每一列都不为空。就像评论中提到的@peepa 一样,我们可以通过标识符识别哪个开始事件与哪个结束事件一起发生。
我想用以下条件计算时间戳的平均值。
如果 eventid=5 则时间戳以 starttime 结束,
eventid=6 时时间戳以 endtime 结束的情况
现在
时间 = 结束时间 - 开始时间。
和
平均(时间)作为平均值
编辑: 时间戳是毫秒而不是 sql 时间戳。 我尝试使用以下查询来分隔 startTime 和 endTime
查询1:
select
case when f.eventid=5 then f.timestamps end as starttime,
case when f.eventid=6 then f.timestamps end as endtime
from frarecord f
和查询2:
select f1.timestamps as starttime, f2.timestamps as endtime
from frarecord f
left join frarecord f1 on (f1.id=f.id and f.eventid=5)
left join frarecord f2 on (f2.id=f.id and f.eventid=6)
并得到以下输出。
我们将不胜感激。
谢谢。
【问题讨论】:
-
你真的在这里使用 MySQL、MS SQL Server 和 Oracle 吗??? (不要标记未涉及的产品...尤其是因为这 3 个产品以自己的方式处理日期/时间。)
-
我删除了无关的数据库标签。你有足够的代表,你应该知道比在问题上填充不适当的标签更好。
-
我添加了更多标签,以便这个问题可以接触到许多用户。 :)
-
我认为您缺少如何将开始和结束事件的记录链接在一起的信息。如表所示,到目前为止,您还没有将行链接在一起的信息。
-
从数据上看如何,id 是唯一的 id,您需要有类似事务 id 的东西,通过它您可以确定哪个开始事件与哪个结束事件一起发生。
标签: sql