简体   繁体   中英

Split time intervals on the hour

I have a data set:

  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

I would like to to essentially 'split' these intervals on the hour, such as:

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

Here is my code so far (credit for the idea goes to @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));

The second half (after 'union all') doesn't produce any result, the first half produces the following:

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

Thanks, I'm a beginner. I understand the first part; admittedly I don't understand the second half

I'm open to other solutions.

I'm using MS-Access 2010

This can be done with a Cartesian query:

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]);

using this other saved Cartesian query ( 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;

Result:

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

Second example with:

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

Result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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