简体   繁体   English

基于间隔15分钟的时间对记录进行分组(MS SQL)

[英]Grouping records based on intervals 15 min of time (MS SQL)

I've got a monitoring system that is collecting data every 1 minutes. 我有一个监控系统,每1分钟收集一次数据。 I'd like to aggregate the collected data by 15 minute intervals. 我想以15分钟的间隔汇总收集的数据。 I used this: (DATEPART (minute,date_report) / 15 ) but it does not work Is there any way to do? 我用过这个:( DATEPART(分钟,date_report)/ 15)但它不起作用有什么办法吗? Any help is much appreciated! 任何帮助深表感谢!

My current query is: 我目前的查询是:

SELECT DISTINCT fio,date_report, vocheredi,PS
 FROM
    (
    select distinct TempTable.fio,TempTable.date_report, count (distinct id) as vocheredi
,(CASE  WHEN TempTable.workgroup LIKE 'Group East' THEN 'East'
        WHEN TempTable.workgroup LIKE 'Group west' THEN 'West'
        WHEN TempTable.workgroup LIKE 'Group Centre' THEN 'Centre' END) as PS

    FROM dbo.TempTable

        WHERE
        TempTable.fio = 'employee'

        and (TempTable.workgroup = 'East' 
        or TempTable.workgroup = 'west' 
            or TempTable.workgroup = 'Centre')

            GROUP BY  TempTable.fio
                ,(CASE  WHEN TempTable.workgroup LIKE 'Group East' THEN 'East'
                    WHEN TempTable.workgroup LIKE 'Group west' THEN 'West'
                    WHEN TempTable.workgroup LIKE 'Group Centre' THEN 'Centre' END)
                        ,TempTable.date_report

                ) table
            WHERE
        PS = (@ReportParameter2)
    GROUP BY
(DATEPART (minute,date_report) / 15 ) ,fio,date_report, vocheredi,PS
ORDER BY date_report DESC

I want it to show me something like: 我想让它给我看一些像:

fio, date_report,vocheredi,PS
employee, 16:15:00-16:30:00 , 19,East

在此输入图像描述

First, I don't thing you want 16:15:00 but 16:15:00 through 16:29:59 (or something like that). 首先,我不是你想要的16:15:00而是16:15:00到16:29:59(或类似的东西)。 I'm not going to rewrite your entire script, but this will provide the grouping on the quarter hour 我不打算重写你的整个脚本,但这将在四分之一小时内提供分组

DATEADD(MINUTE,DATEPART(MINUTE,DateReport)%15*-1,DateReport) 

to understand this DATEPART(MINUTES,DateReport) deliver this minutes (1 through 59) The % or mod function figure how many minutes are left over from an even 15. 理解这个DATEPART(MINUTES,DateReport)提供这个分钟(1到59)%或mod函数表示从偶数15分钟剩下多少分钟。

DATEADd substracts this to get the starting quarter hour. DATEADd将其减去这个以获得起始的四分之一小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM