3

我有一个查询

select
  *,
  right(
    convert(varchar, cast(JOB_DONE_time-JOB_send_time as datetime), 121),
    12
  ) [TimeTaken]
from dbo.insert_data
order by job_id desc  

这个查询的结果就像

JOB_ID     JOB_DONE_TIME            JOB_SEND_TIME            TimeTaken
---------  -----------------------  -----------------------  ------------
001-99900  2011-12-28 13:36:57.860  2011-12-28 13:36:57.593  00:00:00.267
001-99800  2011-12-28 13:36:57.843  2011-12-28 13:36:57.593  00:00:00.250
001-99700  2011-12-28 13:36:57.687  2011-12-28 13:36:57.547  00:00:00.140
001-99600  2011-12-28 13:36:57.593  2011-12-28 13:36:57.547  00:00:00.047
001-99500  2011-12-28 13:36:57.563  2011-12-28 13:36:57.437  00:00:00.127
001-99400  2011-12-28 13:36:57.547  2011-12-28 13:36:57.437  00:00:00.110
001-99300  2011-12-28 13:36:57.483  2011-12-28 13:36:57.377  00:00:00.107
001-99200  2011-12-28 13:36:57.437  2011-12-28 13:36:57.377  00:00:00.060
001-99100  2011-12-28 13:36:57.407  2011-12-28 13:36:57.187  00:00:00.220
001-99000  2011-12-28 13:36:57.360  2011-12-28 13:36:57.187  00:00:00.173
001-9900   2011-12-28 13:32:22.657  2011-12-28 13:32:22.500  00:00:00.127

通过这个查询,我得到了TimeTaken专栏。现在我想计算加法和平均值。的列timetaken。我尝试过使用SUM()功能,但它给了我错误:

消息 8117,级别 16,状态 1,行 1 操作数数据类型 varchar 对于求和运算符无效。

怎么计算???

4

4 回答 4

1

正如您已经被告知的那样,问题与您尝试应用AVG()SUM()应用于字符串列的事实有关,而实际上您只能在数字列上使用这些函数。

作为一个非常特定于 SQL Server 的解决方案,您可以暂时将结果转换JOB_DONE_time - JOB_send_timefloat、计算AVG()和/或SUM()然后将结果转换回datetime,然后转换为varchar,然后提取最后 12 个字符,就像您现在对非聚合结果所做的那样的减法。

这是我的意思的一个例子:

SELECT
  JOB_ID,
  JOB_DONE_TIME,
  JOB_SEND_TIME,
  TimeTaken      = RIGHT(CONVERT(varchar(30), CAST(    TimeTakenFP          AS datetime), 121), 12),
  AvgTimeTaken   = RIGHT(CONVERT(varchar(30), CAST(AVG(TimeTakenFP) OVER () AS datetime), 121), 12),
  TotalTimeTaken = RIGHT(CONVERT(varchar(30), CAST(SUM(TimeTakenFP) OVER () AS datetime), 121), 12)
FROM (
  SELECT
    *,
    TimeTakenFP = CAST(JOB_DONE_TIME - JOB_SEND_TIME AS float)
  FROM insert_data
) s
;

对于您的示例数据,上述查询返回以下结果集:

JOB_ID JOB_DONE_TIME JOB_SEND_TIME TimeTaken AvgTimeTaken TotalTimeTaken
--------- ------------------------ ------------------ ----- ------------ ------------ --------------
001-99900 2011-12-28 13:36:57.860 2011-12-28 13:36:57.593 00:00:00.267 00:00:00.150 00:00:01.657
001-99800 2011-12-28 13:36:57.843 2011-12-28 13:36:57.593 00:00:00.250 00:00:00.150 00:00:01.657
001-99700 2011-12-28 13:36:57.687 2011-12-28 13:36:57.547 00:00:00.140 00:00:00.150 00:00:01.657
001-99600 2011-12-28 13:36:57.593 2011-12-28 13:36:57.547 00:00:00.047 00:00:00.150 00:00:01.657
001-99500 2011-12-28 13:36:57.563 2011-12-28 13:36:57.437 00:00:00.127 00:00:00.150 00:00:01.657
001-99400 2011-12-28 13:36:57.547 2011-12-28 13:36:57.437 00:00:00.110 00:00:00.150 00:00:01.657
001-99300 2011-12-28 13:36:57.483 2011-12-28 13:36:57.377 00:00:00.107 00:00:00.150 00:00:01.657
001-99200 2011-12-28 13:36:57.437 2011-12-28 13:36:57.377 00:00:00.060 00:00:00.150 00:00:01.657
001-99100 2011-12-28 13:36:57.407 2011-12-28 13:36:57.187 00:00:00.220 00:00:00.150 00:00:01.657
001-99000 2011-12-28 13:36:57.360 2011-12-28 13:36:57.187 00:00:00.170 00:00:00.150 00:00:01.657
001-9900 2011-12-28 13:32:22.657 2011-12-28 13:32:22.500 00:00:00.157 00:00:00.150 00:00:01.657

此查询使用窗口聚合函数计算聚合结果,以便您可以看到聚合和非聚合结果。我只使用了窗口函数,因为我想向您展示如何将显示时间的方法应用于非聚合结果和聚合结果,以便您可以看到表达式的哪一部分根据您需要的结果类型发生变化转换为时间表示。实际上,如果这是您的要求,您可以使用“正常”汇总结果(如有必要,还可以进行分组)。

您可以在 SQL Fiddle上进行查询的“实时”演示。

于 2012-07-28T14:39:36.720 回答
0

在 sql 中使用 avg 函数,这将为您提供列的 avg

SELECT AVG(column name) FROM tabelname;

和这样的总和使用

 SELECT sum(column name)  FROM tabelname;
于 2012-07-28T09:22:12.967 回答
0

您可以确定完成和发送时间列的日期差异(以毫秒为单位),然后对这些结果进行平均或求和

select sum(datediff(ms, JOB_send_time, JOB_DONE_time)) as SUM_Timing
,      avg(datediff(ms, JOB_send_time, JOB_DONE_time)) as AVG_Timing
from   dbo.insert_data
于 2012-07-28T09:36:57.817 回答
0

我认为(抱歉,现在无法试用)您需要 DATEDIFF。

在你的情况下:

select avg(DATEDIFF(Millisecond, JOB_DONE_time, JOB_send_time)) from dbo.insert_data
于 2012-07-28T09:37:44.540 回答