Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18

    pull records at 15 min intervals

    Hello,



    in SSMS:

    My database has 3 columns . I can get the timestamp column OK, but I get stuck when trying to retrieve the rest of the record
    This code works to get only the timestamp:
    Code:
    select min([timestamp]) as T2
    from dbo.NM_Data where timestamp between '2020-01-01 00:00:00' and '2023-10-02 00:00:00'
    group by DATEpart(year, [timestamp]),
    DATEpart(month, [timestamp]),
    DATEpart(day, [timestamp]),
    DATEpart(hour, [timestamp]),
    (DATEpart(minute, [timestamp]) / 15)
    order by T2;
    I tried a few things that didn't work - I get aggregate errors.
    How do I get col2 & col 3 into the query too??

    Code:
    col 1                   col2        col3
    10/15/2022 9:15:10 PM    8          A
    10/15/2022 9:30:10 PM    6          A
    10/15/2022 9:45:10 PM    3          C
    10/15/2022 10:00:10 PM   9          B

    Thanks
    Last edited by LadyDee; 11-21-2024 at 08:54 AM.

  2. #2
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    559
    Do you mean GROUP them into 15 minute intervals?
    Code:
    SELECT ...
    FROM (SELECT [Date] As SaleDate
    	,SaleTime
    	,mins = DATEDIFF(minute,'1899-12-30',[SaleTime])
    	,bin = DATEDIFF(minute,'1899-12-30',[SaleTime])/15
    FROM SquareSales) ss
    WHERE bin = 2;
    (basically, the time stamp from the same date at midnight.)

  3. #3
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,175
    What do you want to do with the rest of the columns? All columns in a groups query need to be part of the group by instruction or be included in a grouping function (min;max; sum; avg; ...). If you want a mix of grouping results and individual records: look at the windowing functions using over(partion by ...)

  4. #4
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    559
    Quote Originally Posted by LadyDee View Post
    Hello,

    in SSMS:

    My database has 3 columns . I can get the timestamp column OK, but I get stuck when trying to retrieve the rest of the record
    This code works to get only the timestamp:
    Code:
    select min([timestamp]) as T2
    from dbo.NM_Data where timestamp between '2020-01-01 00:00:00' and '2023-10-02 00:00:00'
    group by DATEpart(year, [timestamp]),
    DATEpart(month, [timestamp]),
    DATEpart(day, [timestamp]),
    DATEpart(hour, [timestamp]),
    (DATEpart(minute, [timestamp]) / 15)
    order by T2;
    I tried a few things that didn't work - I get aggregate errors.
    How do I get col2 & col 3 into the query too??

    Code:
    col 1                   col2        col3
    10/15/2022 9:15:10 PM    8          A
    10/15/2022 9:30:10 PM    6          A
    10/15/2022 9:45:10 PM    3          C
    10/15/2022 10:00:10 PM   9          B
    Thanks
    Could you post a CREATE TABLE script and an INSERT script so that we have some data? And then post the expected output? That would make this much easier to answer. But like Noella said already, you may want a windowing function to do it, because you don't have to aggregate the data. Otherwise, you'd have to create a totals query, and then join that totals query back to the original table and include the other columns that weren't part of the aggregation.

  5. #5
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by madpiet View Post
    Do you mean GROUP them into 15 minute intervals?
    Code:
    SELECT ...
    FROM (SELECT [Date] As SaleDate
        ,SaleTime
        ,mins = DATEDIFF(minute,'1899-12-30',[SaleTime])
        ,bin = DATEDIFF(minute,'1899-12-30',[SaleTime])/15
    FROM SquareSales) ss
    WHERE bin = 2;
    (basically, the time stamp from the same date at midnight.)
    Yes, I have a timestamp for every minute (huge file). I only want to report on every fifteen minute interval - on every quarter of an hour when minute is: 0,15,30,45.
    The timestamp is pulling correctly on every quarter of an hour, but the remaining fields of the record (col2 & col3) gets an error when I place them in the select.
    Thanks

  6. #6
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    Hi. This is the expected output:

    Code:
    col 1                   col2        col3
    10/15/2022 9:15:10 PM    8          A
    10/15/2022 9:30:10 PM    6          A
    10/15/2022 9:45:10 PM    3          C
    10/15/2022 10:00:10 PM   9          B
    This is what i am getting now with the code I showed initially:

    Code:
    col 1    
    10/15/2022 9:15:10 PM  
    10/15/2022 9:30:10 PM   
    10/15/2022 9:45:10 PM    
    10/15/2022 10:00:10 PM
    I need col2 & col3 to be included in the query.
    If I add them to the select I get a group by error.

  7. #7
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    Hi NoellaG,
    I don't know what you mean by windowing functions.
    Is it a command? Can you give me a link or an example?

    Thanks

  8. #8
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by madpiet View Post
    Do you mean GROUP them into 15 minute intervals?
    Code:
    SELECT ...
    FROM (SELECT [Date] As SaleDate
        ,SaleTime
        ,mins = DATEDIFF(minute,'1899-12-30',[SaleTime])
        ,bin = DATEDIFF(minute,'1899-12-30',[SaleTime])/15
    FROM SquareSales) ss
    WHERE bin = 2;
    (basically, the time stamp from the same date at midnight.)

    Hi madpiet,
    This code look close but how do I add the remainder of the record - col2 & col3?
    I need to pull it when minute is 0,15,30,45. like your code says bin =15, but why then does it say WHERE bin = 2?
    This is the intended output. Currently only col1 is coming out through the query I initially posted:
    I have hundreds of thousands rows of data:
    Code:
    col 1                   col2        col3
    10/15/2022 9:15:10 PM    8          A
    10/15/2022 9:30:10 PM    6          A
    10/15/2022 9:45:10 PM    3          C
    10/15/2022 10:00:10 PM   9          B

  9. #9
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    Do you want test data?

  10. #10
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    559
    Quote Originally Posted by LadyDee View Post
    Do you want test data?
    The CREATE TABLE and INSERT statements to give us some data to work with would help a lot. (Don't need all the data, only enough to reproduce the problem).

  11. #11
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by madpiet View Post
    The CREATE TABLE and INSERT statements to give us some data to work with would help a lot. (Don't need all the data, only enough to reproduce the problem).
    I have it prepared in an excel file. I hope this helps. zip is in Attachments
    Attached Files Attached Files

  12. #12
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    559
    What's the expected output? And what are the column names?

  13. #13
    LadyDee is offline Novice
    Windows 10 Access 2019
    Join Date
    Mar 2024
    Posts
    18
    the intended output is
    Code:
    Timestamp                 value       code
    10/15/2022 9:15:10 PM        8          A
    10/15/2022 9:30:10 PM        6          A
    10/15/2022 9:45:10 PM        3          C
    10/15/2022 10:00:10 PM       9          B
    Thank you

  14. #14
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    Given your test data in a table called Test_1 With Fields Called [Timestamp], NValue, TxtCode, the following does what you want.

    Code:
    SELECT 
        [Timestamp],  NValue, TxtCode
        FROM
        (
        SELECT 
        ROW_NUMBER() OVER(Partition By Interval_15  Order BY [TimeStamp] ) as RowNum,*
    
        FROM (
            SELECT 
            DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE, '2020-01-01', [TIMESTAMP]) / CAST(15 AS DECIMAL)) * 15, '2020-01-01') AS Interval_15,* 
            From 
            Test_1
            ) as q1
    
        ) as q2
      Where RowNum = 1
    Results
    Code:
    Timestamp               NValue          TxtCode
    2021-01-11 11:30:09.000    0.9014763960    A
    2021-01-11 11:45:09.000    0.8264825220    B
    2021-01-11 12:00:09.000    0.0000000000    C
    2021-01-11 12:15:09.000    0.0000000000    A
    The Row_Number is a T_SQL window function. If you just run the centre query (q1) on it's own, then the second one (q2) with the first one it will become obvious how they work.

    You could probably mash it all into one massive Select statement but it's easier to see what is happening with the sub queries.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  15. #15
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,175
    About SQL windowing functions: the included zip file contains my basic SQL training - you'll find examples of windowing functions on page 33
    Attached Files Attached Files

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 10-08-2020, 04:31 PM
  2. Replies: 8
    Last Post: 03-14-2012, 05:40 PM
  3. Replies: 1
    Last Post: 01-17-2012, 02:51 PM
  4. Urgent issue! Create field for intervals
    By Bjorn in forum Queries
    Replies: 3
    Last Post: 02-10-2010, 10:26 AM
  5. Help with Time Intervals
    By ddog171 in forum Queries
    Replies: 3
    Last Post: 03-07-2006, 06:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums