计算指定范围的列值 - SQL

时间:2012-08-24 13:22:01

标签: sql sql-server

在我的SQL查询中,我返回列,GetDate()和列之间的区别为" OriginalFaultOccuredOn" (DataType是DateTime)。我已经完成了一些Converion,因此它会在5.56格式(5小时56分钟)中显示差异

结果就像

<Column Name>

3.45
5.55
1.23
10.65

我希望以“Shud Print Result”(

)的方式修改它
More Than 6 Hours  More THan 12 Hours More than 24 Hours
3                  5                  6 

我的SQL查询看起来像: -

    Select Floor(Convert(Numeric(18,2),Convert(Numeric(18,2),DateDiff(MI, OriginalFaultOccurredOn, GetDate()))/60)) + 
Convert(Numeric(18,2),((Convert(Numeric(18,2),Convert(Numeric(18,2),DateDiff(MI, OriginalFaultOccurredOn, GetDate()))/60) -
Floor(Convert(Numeric(18,2),Convert(Numeric(18,2),DateDiff(MI, OriginalFaultOccurredOn, GetDate()))/60)))*0.6))[Time] 
From Ticket Where Closed=0

DataBase是MS-SQL Server

1 个答案:

答案 0 :(得分:1)

您可以通过每次找到匹配项时添加一个来计算落在指定范围内的事件:

select sum(case when [time] > 6 and [time] <= 12 then 1 end) [More than 6 hours],
       sum(case when [time] > 12 and [time] <= 24 then 1 end) [More than 12 hours],
       sum(case when [time] > 24 then 1 end) [More than 24 hours]
from
(
 { your-query-here }
) a