2016-08-30 78 views
0

所以我有一個查詢,我試圖找到總和和平均值,而按時間戳分組到分鐘。但是,如果時間戳記具有SAME分鐘但在不同的小時內,那麼行數據不會顯示。以下是我的查詢和輸出以及單獨的行:GroupBy Datepart函數缺少數據

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 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以分鐘爲基礎,它將所有行分組在一起,而不管小時數。有沒有辦法考慮小時和日期,而不僅僅是分鐘。我仍然需要它按分鐘分組,而不是幾秒或幾毫秒。

回答

0

(順便說一句好聽的名字大聲笑)試試這個:

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 
0

然而,如果時間戳具有在同一分鐘,但在不同的時間,這行數據顯示不出來

試試這個..

group by 
DatePart(minute, orderprocessormetrics.processingdate), 
DatePart(hour, orderprocessormetrics.processingdate), 
DatePart(day, orderprocessormetrics.processingdate) 
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; 
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