sql >> Database teknologi >  >> RDS >> Sqlserver

Gruppér bordet i intervaller på 15 minutter

;with cte_max as 
(
   select dateadd(mi, -15, max(StatusEndDateTime)) as EndTime, min(StatusSetDateTime) as StartTime
   from AgentActivityLog
), times as
(
    select StartTime as Time from cte_max
    union all
    select dateadd(mi, 15, c.Time)
    from times as c
        cross join cte_max as cm
    where c.Time <= cm.EndTime
)
select
    t.Time, A.UserID, A.Status,
    case
        when t.Time = A.StatusEndDateTime then 0
        else A.StatusDuration / (count(*) over (partition by A.StatusSetDateTime, A.UserID, A.Status) - 1)
    end as Duration
from AgentActivityLog as A
    left outer join times as t on t.Time >= A.StatusSetDateTime and t.Time <= A.StatusEndDateTime

sql violindemo



  1. Mærkelig karakter i frisk WAMP-installation?

  2. PHP/PDO/MySQL:indsættelse i MEDIUMBLOB gemmer dårlige data

  3. Hvordan viser jeg en MySQL-fejl i PHP for en lang forespørgsel, der afhænger af brugerens input?

  4. Sekvens i SQL Select