按小时分割时间间隔

时间:2017-10-04 15:44:17

标签: sql ms-access

我有一个数据集:

  tbldataid          TS                     EndTS
> HX32.3401 10/2/2017 11:49:34 PM   10/3/2017 12:01:57 AM
> HX32.3403 10/3/2017 12:02:48 AM   10/3/2017 12:08:34 AM
> HX32.3425 10/3/2017 2:50:57 AM    10/3/2017 2:50:58 AM
> HX32.3428 10/3/2017 4:06:15 AM    10/3/2017 6:09:19 AM

我想基本上分开'这些时间间隔,例如:

tbldataid               TS                EndTS
HX32.3401   10/2/2017 11:49:34 PM   10/2/2017 11:59:99 PM
HX32.3401   10/2/2017 12:00:00 PM   10/3/2017 12:01:57 AM
HX32.3403   10/3/2017 12:02:48 AM   10/3/2017 12:08:34 AM
HX32.3425   10/3/2017 2:50:57 AM    10/3/2017 2:50:58 AM
HX32.3428   10/3/2017 4:06:15 AM    10/3/2017 4:59:99 AM
HX32.3428   10/3/2017 5:00:00 AM    10/3/2017 5:59:99 AM
HX32.3428   10/3/2017 6:00:00 AM    10/3/2017 6:09:19 AM

到目前为止,这是我的代码(这个想法归功于@Dumitrescu Bogdan,Split call data to 15 minute intervals):

SELECT [tbldataid],[TS],
   IIF([EndTS]<=dateadd("n",60*((datediff("n",0,[TS]/60))+.99),0), 
        [EndTS], 
        dateadd("n", 60*(datediff("n",0,[TS]/60)+.99),0)) 
        as [End] 
FROM tbldata


UNION ALL 

SELECT t2.[tbldataid], t2.[TS],
   IIF(t1.[EndTS]<=dateadd("n",60*((datediff("n",0, t2.[EndTS])/60)+1),0), 
         t1.[EndTS], 
         dateadd("n",60*((datediff("n",0,t2.[EndTS])/60)+1),0)) 
         as [End] 
FROM 
   tbldata t1 
   LEFT JOIN tbldata t2
   ON t1.[tbldataid]=t2.[tbldataid]
Where t2.[EndTS]<IIF(t1.[EndTS]<=dateadd("n",60*((datediff("n",0,t2.[EndTS])/60)+1),0),
                                   t1.[EndTS],
                                   dateadd("n",60*((datediff("n",0,t2.[EndTS])/60)+1),0));

下半场(在&#39; all all&#39;之后)没有产生任何结果,上半场产生以下结果:

tbldataid             TS                     End
HX32.3401   10/2/2017 11:49:34 PM   10/2/2017 11:59:00 PM
HX32.3403   10/3/2017 12:02:48 AM   10/3/2017 12:08:34 AM
HX32.3425   10/3/2017 2:50:57 AM    10/3/2017 2:50:58 AM
HX32.3428   10/3/2017 4:06:15 AM    10/3/2017 4:59:00 AM

谢谢,我是初学者。我理解第一部分;诚然,我不明白下半场

我对其他解决方案持开放态度。

我正在使用MS-Access 2010

1 个答案:

答案 0 :(得分:2)

这可以通过笛卡尔查询来完成:

SELECT DISTINCT 
    tblData.tblDataId, 
    IIf([TimeStart] > DateAdd("h",[Factor],CDate(Fix([Timestart]*24)/24)),
        [TimeStart],
        DateAdd("h",[Factor],CDate(Fix([Timestart]*24)/24))) AS TSStart, 
    IIf([TimeEnd] < DateAdd("s",3599,DateAdd("h",[Factor],CDate(Fix([Timestart]*24)/24))),
        [TimeEnd],
        DateAdd("s",3599,DateAdd("h",[Factor],CDate(Fix([Timestart]*24)/24)))) AS TSEnd
FROM 
    qdxFactor, 
    tblData
WHERE
    qdxFactor.Factor Between 0 And DateDiff("h",[TimeStart],[TimeEnd]);

使用其他已保存的笛卡尔查询( qdxFactor ):

SELECT DISTINCT 
    [Tens]+[Ones] AS Factor, 
    10*Abs([Deca].[id] Mod 10) AS Tens, 
    Abs([Uno].[id] Mod 10) AS Ones
FROM 
    MSysObjects AS Uno, 
    MSysObjects AS Deca;

结果:

tblDataId   TSStart             TSEnd
    3401    2017-10-02 23:49:34 2017-10-02 23:59:59
    3401    2017-10-03 00:00:00 2017-10-03 00:01:57
    3403    2017-10-03 00:02:48 2017-10-03 00:08:34
    3425    2017-10-03 02:50:57 2017-10-03 02:50:58
    3428    2017-10-03 04:06:15 2017-10-03 04:59:59
    3428    2017-10-03 05:00:00 2017-10-03 05:59:59
    3428    2017-10-03 06:00:00 2017-10-03 06:09:19

第二个例子:

tblDataId   TimeStart           TimeEnd
    3430    2017-10-07 02:08:24 2017-10-07 14:09:30

结果:

tblDataId   TSStart             TSEnd
    3430    2017-10-07 02:08:24 2017-10-07 02:59:59
    3430    2017-10-07 03:00:00 2017-10-07 03:59:59
    3430    2017-10-07 04:00:00 2017-10-07 04:59:59
    3430    2017-10-07 05:00:00 2017-10-07 05:59:59
    3430    2017-10-07 06:00:00 2017-10-07 06:59:59
    3430    2017-10-07 07:00:00 2017-10-07 07:59:59
    3430    2017-10-07 08:00:00 2017-10-07 08:59:59
    3430    2017-10-07 09:00:00 2017-10-07 09:59:59
    3430    2017-10-07 10:00:00 2017-10-07 10:59:59
    3430    2017-10-07 11:00:00 2017-10-07 11:59:59
    3430    2017-10-07 12:00:00 2017-10-07 12:59:59
    3430    2017-10-07 13:00:00 2017-10-07 13:59:59
    3430    2017-10-07 14:00:00 2017-10-07 14:09:30