GroupBy Datepart 函数缺少数据

GroupBy Datepart function missing data

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

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 numcards passed at 10)

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 基于分钟,所以它会将具有相同分钟的所有行分组在一起,而不考虑小时数。有没有办法考虑时间和日期而不仅仅是分钟。我仍然需要它按分钟而不是秒或毫秒分组。

However, if the timestamp has the SAME minute but in a different hour, that rows data doesn't show up

试试这个..

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

您需要将 date/time 截断到分钟。这是一种方法:

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;

尝试使用以下查询..

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

试试这个(好听的名字 btw 哈哈):

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