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

  • Home
  • SEARCH
  • 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 7847569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:56:36+00:00 2026-06-02T17:56:36+00:00

Background I have a table set up in a SQL Server environment that contains

  • 0

Background

I have a table set up in a SQL Server environment that contains a log of various activity that I’m tracking. Particular log items use unique codes to categorize what activity is taking place and a datetime field tracks when that activity occurred.

Problem

I would like to, using either a single query or a stored procedure, get an average of hourly counts of activity, grouped by day of the week. Example:

Day      | Hour | Average Count
-------------------------------
Monday   | 8    | 5
Monday   | 9    | 5
Monday   | 10   | 9
...
Tuesday  | 8    | 4
Tuesday  | 9    | 3
...etc

Right now I’ve got a query setup that spits out the counts per hour per day, but my problem is taking it a step further and getting average by day of week. Here’s my current query:

SELECT CAST([time] AS date) AS ForDate,
   DATEPART(hour, [time]) AS OnHour,
   COUNT(*) AS Totals
FROM [log] WHERE [code] = 'tib_imp.8'
GROUP BY CAST(time AS date),
   DATEPART(hour,[time])
   ORDER BY ForDate Asc, OnHour Asc

Any suggestions as to how I might accomplish this?

Thanks in advance!

  • 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-06-02T17:56:38+00:00Added an answer on June 2, 2026 at 5:56 pm

    Guessing here:

    SELECT [Day], [Hour], [DayN], AVG(Totals) AS [Avg]
    FROM
      (
            SELECT 
              [Day]  = DATENAME(WEEKDAY, [time]),
              [DayN] = DATEPART(WEEKDAY, [time]),
              [Hour] = DATEPART(HOUR,    [time]),
              Totals = COUNT(*)
            FROM dbo.[log] 
                WHERE [code] = 'tib_imp.8'
            GROUP BY 
              DATENAME(WEEKDAY, [time]),
              DATEPART(WEEKDAY, [time]),
              DATEPART(HOUR,    [time])
      ) AS q
    GROUP BY [Day], [Hour], [DayN]
    ORDER BY DayN; 
    

    Again, without data, I might once again be throwing a handful of mud at the wall and hoping it sticks, but perhaps what you need is:

    SELECT [Day], [Hour], [DayN], AVG(Totals) AS [Avg]
    FROM
    (
        SELECT 
      w = DATEDIFF(WEEK, 0, [time]),
          [Day]  = DATENAME(WEEKDAY, [time]),
          [DayN] = DATEPART(WEEKDAY, [time]),
          [Hour] = DATEPART(HOUR,    [time]),
          Totals = COUNT(*)
        FROM dbo.[log] 
          WHERE [code] = 'tib_imp.8'
        GROUP BY 
      DATEDIFF(WEEK, 0, [time]),
          DATENAME(WEEKDAY, [time]),
          DATEPART(WEEKDAY, [time]),
          DATEPART(HOUR,    [time])
      ) AS q
    GROUP BY [Day], [Hour], [DayN]
    ORDER BY DayN; 
    

    This is also going to produce integer-based averages, so you may want to cast the Totals alias on the inner query to DECIMAL(something,something).

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

Sidebar

Related Questions

Background: For various reasons I'm creating a translation table in my sql server 2008
Background I have a dimension table that has a single record for each day.
I have the following code that produces a background image in a table cell.
Some background I have a set of data that represents the alchemy ingredients and
Background: I have a Windows 2008 R2 box set up with SQL 2008 R2
I have a table in a report and a textbox that changes its background
I have a table where the columns have a different background set by a
I have a table cell in which i have to set a background image,
I have a table set up such that each row has a checkbox as
I have a table where i want to change cell background on mouse over

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.