对 SQL 查询中的分割范围求和

发布于 2024-07-07 02:34:58 字数 1224 浏览 5 评论 0原文

我有一个包含我的服务器状态的表,

create table ServerStatus
(
    ServerId int, 
    StartTime datetime, 
    Seconds int,
    [State] char(8) 
)

我想要一个给定开始和结束日期的查询将总结服务器在此期间在每个状态下花费的时间。 我还希望查询返回服务器处于未知状态的时间量。

因此,例如为以下数据

1   2008-01-01 00:00:00.000 120 broken  
1   2008-01-02 00:00:00.000 120 off     
1   2008-01-03 00:00:00.000 240 burning 
1   2008-01-04 00:00:00.000 60  off     
1   2008-01-05 00:00:00.000 60  off     
2   2008-01-01 00:00:00.000 60  broken  
2   2008-01-02 00:00:00.000 30  off     
2   2008-01-03 00:00:00.000 20  burning 
2   2008-01-04 00:00:00.000 600 off     
3   2007-01-04 00:00:00.000 600 off     
4   2007-12-12 00:00:00.000 999999999   onfire  

提供了范围。

select  @start = dateadd(second, 60, '2008-01-01'), 
@fin = dateadd(second, 60, '2008-01-04')

我想返回结果:

1   broken      60
1   burning     240
1   off         180
1   unknown     258720
2   burning     20
2   off         90
2   unknown     259090
4   onfire      259200

这个问题与以下内容有些相关:组合SQL 查询中的分割日期范围

I have a table which contains my server status

create table ServerStatus
(
    ServerId int, 
    StartTime datetime, 
    Seconds int,
    [State] char(8) 
)

I would like a query that given a start and end date will summarize the time the server spends in each state during that time. I would also like the query to return the amount of time the servers spend in an unknown state.

So, for example for the following data

1   2008-01-01 00:00:00.000 120 broken  
1   2008-01-02 00:00:00.000 120 off     
1   2008-01-03 00:00:00.000 240 burning 
1   2008-01-04 00:00:00.000 60  off     
1   2008-01-05 00:00:00.000 60  off     
2   2008-01-01 00:00:00.000 60  broken  
2   2008-01-02 00:00:00.000 30  off     
2   2008-01-03 00:00:00.000 20  burning 
2   2008-01-04 00:00:00.000 600 off     
3   2007-01-04 00:00:00.000 600 off     
4   2007-12-12 00:00:00.000 999999999   onfire  

Provided the range.

select  @start = dateadd(second, 60, '2008-01-01'), 
@fin = dateadd(second, 60, '2008-01-04')

I would like to return the results:

1   broken      60
1   burning     240
1   off         180
1   unknown     258720
2   burning     20
2   off         90
2   unknown     259090
4   onfire      259200

This question is somewhat related to: Combining split date ranges in a SQL query

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-07-14 02:34:58

这是迄今为止我想到的最好的:

declare @start datetime
declare @fin datetime 

select  @start = dateadd(second, 60, '2008-01-01'), @fin = dateadd(second, 60, '2008-01-04')

select ServerId, State, Total = sum(
    case when StartTime > @start and DateAdd(second, Seconds, StartTime) <= @fin
        then Seconds
    when StartTime < @start and DateAdd(second, Seconds, StartTime) <= @fin
        then DateDiff(second, @start, DateAdd(second, Seconds, StartTime)) 
    when StartTime < @start and DateAdd(second, Seconds, StartTime) >= @fin
        then DateDiff(second, @start, @fin)
    else 
        DateDiff(second,StartTime,@fin)
    end)
into #t
from ServerStatus 
where @start < dateadd(second, Seconds, StartTime) 
and @fin > StartTime
group by ServerId, State

insert #t
select ServerId, 'unknown', DateDiff(second, @start, @fin) - sum(Total) 
from #t 
group by ServerId
having DateDiff(second, @start, @fin) > sum(Total)

select * from #t 
order by ServerId, State

This is the best I have come up with so far:

declare @start datetime
declare @fin datetime 

select  @start = dateadd(second, 60, '2008-01-01'), @fin = dateadd(second, 60, '2008-01-04')

select ServerId, State, Total = sum(
    case when StartTime > @start and DateAdd(second, Seconds, StartTime) <= @fin
        then Seconds
    when StartTime < @start and DateAdd(second, Seconds, StartTime) <= @fin
        then DateDiff(second, @start, DateAdd(second, Seconds, StartTime)) 
    when StartTime < @start and DateAdd(second, Seconds, StartTime) >= @fin
        then DateDiff(second, @start, @fin)
    else 
        DateDiff(second,StartTime,@fin)
    end)
into #t
from ServerStatus 
where @start < dateadd(second, Seconds, StartTime) 
and @fin > StartTime
group by ServerId, State

insert #t
select ServerId, 'unknown', DateDiff(second, @start, @fin) - sum(Total) 
from #t 
group by ServerId
having DateDiff(second, @start, @fin) > sum(Total)

select * from #t 
order by ServerId, State
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文