Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:20:16+00:00 2026-05-16T11:20:16+00:00

I need to query a large number of rows (containing a timestamp column) and

  • 0

I need to query a large number of rows (containing a timestamp column) and aggregate the results by day. The trick is I need the aggregate functions to be grouped for each day from 6AM until next day at 6AM, not from midnight to midnight.

Obviously, I can do some sort of “group by DATEPART(day,Timestamp-6 hours)” but for millions of rows this seems to add quite a bit of work to the server. Actually, it will slow the query from a couple of seconds to over 2 minutes and will eventually timeout.

What is a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:20:17+00:00Added an answer on May 16, 2026 at 11:20 am

    The T-SQL below will do what you want in SQL Server. However, performance will not be great over a large number of rows as no index can be used due to the functions wrapping the dt column. For performance reasons you could consider adding a new computed column to your table to store the date with the time offset (-6 hours). This new column could then be indexed and performance should be fine.

    if exists (select * from sys.objects WHERE object_id = object_id(N’dbo.t’) AND type in (N’U’))
    drop table dbo.t

    create table dbo.t
    (dt datetime not null)

    insert into dbo.t values (‘20100819 05:00’)
    insert into dbo.t values (‘20100819 07:00’)
    insert into dbo.t values (‘20100819 23:00’)
    insert into dbo.t values (‘20100820 04:00’)
    insert into dbo.t values (‘20100820 11:00’)
    insert into dbo.t values (‘20100820 13:00’)
    insert into dbo.t values (‘20100821 00:45’)

    select convert(date,dateadd(HH,-6,dt)) as [Date],
    COUNT(dt) as [Count]
    from dbo.t
    group by convert(date,dateadd(HH,-6,dt))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a query that will group a large number of records
I need to transfer a large number of rows from a SQL Server database
I need to fetch a number of rows between two column values, using a
I'm using Postgres, and I have a large number of rows that need to
I have a rather large table which I need to query for a reporting
I need to query a database which is designed so what a NVARCHAR column
I need to query the database to get the column names , not to
I need to efficiently load a large number of entities based on a list
Similar to this question , I need to group a large number of records
I have two tables: Dictionary, Names. Both tables have large number of rows (like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.