Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在每个月的指定时间间隔内计算列的总数?_Sql_Tsql - Fatal编程技术网

Sql 如何在每个月的指定时间间隔内计算列的总数?

Sql 如何在每个月的指定时间间隔内计算列的总数?,sql,tsql,Sql,Tsql,我必须在指定的时间间隔(20:30-22:00)内计算每天一列的总数,或者计算一个月内该小时的平均值,你能帮我吗?谢谢 通过这个查询,我无法得到我需要的总数 Select COUNT (Qt_Tot_CallType) from [Reporting].[dbo].[TLC_STAT_INBOUND_CC] where (CONVERT(char(19), Dt_Rif, 20)BETWEEN '2019-01-01 20:30' AND '2019-01-31 22:00') AND (Cd_

我必须在指定的时间间隔(20:30-22:00)内计算每天一列的总数,或者计算一个月内该小时的平均值,你能帮我吗?谢谢

通过这个查询,我无法得到我需要的总数

Select COUNT (Qt_Tot_CallType)
from [Reporting].[dbo].[TLC_STAT_INBOUND_CC]
where (CONVERT(char(19), Dt_Rif, 20)BETWEEN '2019-01-01 20:30' AND '2019-01-31 22:00') AND (Cd_Servizio IN (71,72,73,75,76,77,78,79,80,81,82,83,84,85,87,88,90,91,96,97,98,99,100,101,109,110,111,112,116,133,139,140,142,144,152,153,154,156,157,159,160))
group by (Dt_Rif)
count
count(Qt\u Tot\u CallType)
表示数值或非空行,其值位于列
Qt\u Tot\u CallType

你需要一笔钱吗

  Select Dt_Rif, SUM(Qt_Tot_CallType)
  from [Reporting].[dbo].[TLC_STAT_INBOUND_CC]
  where CONVERT(char(19), Dt_Rif, 20)
       BETWEEN '2019-01-01 20:30' AND '2019-01-31 22:00' 
  AND Cd_Servizio IN (71,72,73,75,76,77,78,79,80,81,82,83,84,85,87,88,90,91
      ,96,97,98,99,100,101,109,110,111,112,116
      ,133,139,140,142,144,152,153,154,156,157,159,160)
  group by (Dt_Rif)

cast
首先将日期时间列转换为时间

Select COUNT (Qt_Tot_CallType),  month(Dt_Rif)
from [Reporting].[dbo].[TLC_STAT_INBOUND_CC]
where cast(Dt_Rif as time) BETWEEN '20:30:00' and '22:00:00' AND (Cd_Servizio IN (71,72,73,75,76,77,78,79,80,81,82,83,84,85,87,88,90,91,96,97,98,99,100,101,109,110,111,112,116,133,139,140,142,144,152,153,154,156,157,159,160))
group by month(Dt_Rif)

日期和时间范围最好与
=
更新您的问题添加适当的数据样本和预期结果这很好,但我需要指定时间内每个月所有Cd_服务总和的平均值
select
  convert(date, dtrif) as day,
  sum(qt_tot_calltype) as total,
  avg(qt_tot_calltype) as average
from reporting.dbo.tlc_stat_inbound_cc
where cd_servizio in (...)
and dtrif >= '2019-01-01' and dtrif < '2019-02-01'
and convert(time, dtrif) >= '20:30:00' and convert(time, dtrif) < '22:00:00'
group by convert(date, dtrif)
order by convert(date, dtrif);