【问题标题】:How can I calculate average with condition in sql如何在sql中使用条件计算平均值
【发布时间】: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


【解决方案1】:

不太确定这是否是您所追求的,但由于提供的数据,这可能是我能想到的最好的,这假设您一次只能运行一个“表单名称”。 这不是一个完整的答案,但可能是朝着正确方向迈出的一步?

;with tablecte as (
select 
case  when f.eventid=5 then f.timestamps end as starttime,
case  when f.eventid=6 then f.timestamps end as endtime,
f.formname
from frarecord f 
order by ISNULL(starttime,endtime)

--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)

), combine as (

select  t2.starttime, (select top 1 t1.endtime from tablecte t1 where t1.endtime > t2.starttime and t1.formname = t2.formname) as endtime from tablecte t2


)
select AVG(starttime - endtime) from combine

【讨论】:

  • 感谢您的努力。明天我会详细看到你的答案。
【解决方案2】:

首先我将所有记录按identifier 分组,并在Gordon Linoff 的帮助下使用以下查询将starttimeendtime 分开。答案是Here

我使用的查询:

with f1 as (
  select (case when f.eventid=5 then f.timestamps end) as starttime,
         (case when f.eventid=6 then f.timestamps end) as endtime,
        f.instanceidentifier as identifier,
        f.formid as formid, 
        f.formname as formname
  from frarecord f  
 )
select min(starttime) as starttime, max(endtime) as endtime, identifier
from f1
group by identifier

运行上述查询后,我得到以下输出:

现在我可以在减去以下方式后计算平均时间(我得到了完成的时间)。

时间 = 结束时间 - 开始时间

下面给出了计算平均值的完整查询。

select   AVG (endtime-starttime) as average from
(
with f1 as (
  select (case when f.eventid=5 then f.timestamps end) as starttime,
         (case when f.eventid=6 then f.timestamps end) as endtime,
        f.instanceidentifier as identifier,
        f.formid as formid, 
        f.formname as formname
  from frarecord f   
 )
select min(starttime) as starttime, max(endtime) as endtime, identifier
from f1
group by identifier
) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多