使用sqlite添加到最后一行查询的平均时间

时间:2014-04-14 20:06:15

标签: sqlite report spiceworks

我每周都会在Spiceworks服务器上运行以下报告,以便我们的IS和IT部门将其用作衡量我们分配服务台票证所需时间的指标。它选择以下内容:

  • 门票ID
  • 门票创建日期
  • 计算分配所花费的时间(分配时间 - 创建时间)
  • 将故障单分配给谁
  • 分配了票证的

where子句查找带有“已分配给”信息的注释,并且只获取分配给IT部门用户的过去7天内的票证。

我正在尝试向此查询添加最后一行,以平均部门分配故障单所花费的时间。我读到了rollup()函数,但它不是一个公认的Sqlite方法。

任何提示或建议都会很棒!谢谢!

--Based on report by Ben Brookshire, Spiceworks Inc.
SELECT t.id as ID,
   t.created_at as TicketCreated,

  --CASE WHEN x=w1 THEN r1 WHEN x=w2 THEN r2 ELSE r3 END
  CASE
  WHEN (((strftime('%s',c.created_at) - strftime('%s',t.created_at))/60) < 1) THEN   (strftime('%s',c.created_at) - strftime('%s',t.created_at)) || " seconds"
  WHEN (((strftime('%s',c.created_at) - strftime('%s',t.created_at))/3600) < 1) THEN round((strftime('%s',c.created_at) - strftime('%s',t.created_at))/60,3) || " minutes"
  WHEN (((strftime('%s',c.created_at) - strftime('%s',t.created_at))/3600) > 1) THEN round((strftime('%s',c.created_at) - strftime('%s',t.created_at))/3600.0,3) || " hours"
  WHEN (((strftime('%s',c.created_at) - strftime('%s',t.created_at))/3600) > 24) THEN round((strftime('%s',c.created_at) - strftime('%s',t.created_at))/86400.0,3) || " days"
 ELSE "error"
 --round((strftime('%s',c.created_at) - strftime('%s',t.created_at))/3600.0,3) as "Time to Assign (hours)",
 END as "Time to Assign",
 u.email as AssignedTo,
 u2.email as AssignedBy

FROM tickets t

  LEFT JOIN comments c ON c.ticket_id = t.id
  LEFT JOIN users u ON u.id = t.assigned_to
  LEFT JOIN users u2 ON u2.id = c.created_by

WHERE c.body LIKE "%Assigned to%"
  AND c.created_at BETWEEN date('now', '-6 days') AND date('now')
  AND (u.email = "todd@email.com" OR u.email = "andy@email.com")
ORDER BY t.id;​

1 个答案:

答案 0 :(得分:0)

使用单独的查询计算平均值,并使用compound query添加该记录:

SELECT t.id, ...
FROM ...
WHERE ...
UNION ALL
SELECT 999999999, NULL,
       AVG(strftime('%s', c.created_at) - strftime('%s', t.created_at)),
       NULL, NULL
FROM ...
WHERE ...
ORDER BY 1