GroupBy Datepart函数缺少数据

时间:2016-08-30 17:35:32

标签: sql sql-server-2008 ssms

所以我有一个查询,我正在尝试查找总和和平均值,同时按时间戳分组到分钟。但是,如果时间戳具有SAME分钟但在不同的小时内,那么行数据不会显示。下面是我的查询和输出以及各行:

NumCardsPassed  ProcessingTime  ProcessingDate
     10              4         2016-08-29 11:13:44.000
      1              0         2016-08-29 11:49:43.000
      2              1         2016-08-29 12:19:42.000

以下是没有所有分组的各行。注意上面的查询中如何缺少12:13和22:13的时间戳(上面的NumCardsPassed应该等于10,它将它添加到11:13传递给10的numcards)

NumCardsPassed      ProcessingTime     processingdate
     1                    2         2016-08-29 11:13:44.000
     1                    0         2016-08-29 11:49:43.000
     8                    10        2016-08-29 12:13:44.000
     1                    3         2016-08-29 12:19:42.000
     1                    0         2016-08-29 22:13:47.000

Select  sum(numcardspassed) as "NumCardsPassed", 
avg(processingtime) as "ProcessingTime",  min(ProcessingDate) as "ProcessingDate" 
From orderprocessormetrics
where ProcessingDate >= '8/27/2016' and ProcessingDate < '8/30/2016' and PreprocessorType = 'SOP' and numcardsPassed > 0 and clientid = 6820
Group by  DatePart(minute, orderprocessormetrics.processingdate) 
order by processingdate

所以基本上因为datePart基于分钟,它将所有行分组在一起,无论小时数都相同。有没有办法考虑时间和日期,而不仅仅是几分钟。我仍然需要按分钟而不是秒或毫秒来分组。

4 个答案:

答案 0 :(得分:0)

  

但是,如果时间戳具有相同的分钟但在不同的小时内,那么行数据不会显示

试试这个..

group by 
DatePart(minute, orderprocessormetrics.processingdate),
DatePart(hour, orderprocessormetrics.processingdate),
DatePart(day, orderprocessormetrics.processingdate)

答案 1 :(得分:0)

您需要将日期/时间截断为分钟。这是一种方式:

select dateadd(minute, datediff(minute, 0, processingdate), 0) as to_the_minute,
       sum(numcardspassed) as NumCardsPassed, 
       avg(processingtime) as ProcessingTime
From orderprocessormetrics
where ProcessingDate >= '2016-08-27' and ProcessingDate < '2016-08-30' and 
      PreprocessorType = 'SOP' and
      numcardsPassed > 0 and
      clientid = 6820
group by dateadd(minute, datediff(minute, 0, processingdate), 0)
order by to_the_minute;

答案 2 :(得分:0)

尝试使用以下查询..

Select  sum(numcardspassed) as "NumCardsPassed", 
avg(processingtime) as "ProcessingTime",  min(ProcessingDate) as "ProcessingDate" 
From orderprocessormetrics
where ProcessingDate >= '8/27/2016' and ProcessingDate < '8/30/2016' and PreprocessorType = 'SOP' and numcardsPassed > 0 and clientid = 6820
Group by  CAST(orderprocessormetrics.processingdate as date)
,DatePart(hour, orderprocessormetrics.processingdate) 
,DatePart(minute, orderprocessormetrics.processingdate) 
order by processingdate

答案 3 :(得分:0)

试试这个(好名字btw lol):

Select  sum(numcardspassed) as "NumCardsPassed", sum(numcardsfailed) as "NumCardsFailed",  
avg(processingtime) as "ProcessingTime",  min(ProcessingDate) as "ProcessingDate" 
From orderprocessormetrics
where ProcessingDate >= '8/27/2016' and ProcessingDate < '8/30/2016' and PreprocessorType = 'SOP' and numcardsPassed > 0 and clientid = 6820
Group by  convert(varchar, ProcessingDate, 108) 
order by processingdate