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

使用sqlite将平均时间添加到查询的最后一行,sqlite,report,spiceworks,Sqlite,Report,Spiceworks,我有一份每周在Spiceworks服务器上运行的报告,供我们的IS和IT部门用作我们分配帮助台票证所需时间的指标。它选择以下选项: 票号 票证创建日期 计算分配所用的时间(分配的时间-创建时间) 票是分配给谁的 谁分配的票 where子句查找带有“分配给”信息的注释,只获取过去7天分配给IT部门用户的票证 我正在尝试向该查询添加最后一行,该行将计算部门分配票证所需的平均时间。我读过rollup()函数,但它不是公认的Sqlite方法 任何提示或建议都会很好!谢谢 --Based on rep

我有一份每周在Spiceworks服务器上运行的报告,供我们的IS和IT部门用作我们分配帮助台票证所需时间的指标。它选择以下选项:

  • 票号
  • 票证创建日期
  • 计算分配所用的时间(分配的时间-创建时间)
  • 票是分配给谁的
  • 谁分配的票
where子句查找带有“分配给”信息的注释,只获取过去7天分配给IT部门用户的票证

我正在尝试向该查询添加最后一行,该行将计算部门分配票证所需的平均时间。我读过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;​
——基于Spiceworks Inc.本布鲁克郡的报告。
选择t.id作为id,
t、 在创建时创建了\u作为TicketCreated,
--当x=w1时,情况为r1,当x=w2时,情况为r2,其他情况为r3结束
案例
当((strftime('%s',c.created_at)-strftime('%s',t.created_at))/60小于1)然后(strftime('%s',c.created_at)-strftime('%s',t.created_at))| |“秒”
当((strftime('%s',c.created_at)-strftime('%s',t.created_at))/3600)小于1时,则取整((strftime('%s',c.created_at)-strftime('%s',t.created_at))/60,3)|“分钟”
当((strftime('%s',c.created_at)-strftime('%s',t.created_at))/3600)大于1时,则取整((strftime('%s',c.created_at)-strftime('%s',t.created_at))/3600.0,3)|“小时”
当((strftime('%s',c.created_at)-strftime('%s',t.created_at))/3600)大于24时,则取整((strftime('%s',c.created_at)-strftime('%s',t.created_at))/86400.0,3)天
“错误”
--将((strftime(“%s”,c.created_at)-strftime(“%s”,t.created_at))/3600.0,3)取整为“分配时间(小时)”,
以“分配时间”结束,
u、 指定给的电子邮件,
u2.指定的电子邮件
从票t
c.ticket_id=t.id上的左连接注释c
在u.id=t上左加入用户u
在u2.id=c.created\u上左加入用户u2
其中c.body类“%Assigned to%”
和c.在日期(“现在”,“6天”)和日期(“现在”)之间创建
和(u.email=”todd@email.com“或u.email=”andy@email.com")
按t.id订购;​

使用单独的查询计算平均值,并使用以下方法添加该记录:

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