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 8599583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:25:42+00:00 2026-06-12T01:25:42+00:00

SQL Server 2008. declare @pardate table ( pardateid int, pardatewhen datetime2(3) ) insert into

  • 0

SQL Server 2008.

declare @pardate table ( pardateid int, pardatewhen datetime2(3) )
insert into @pardate values ( 1 , '2011-09-17 12:43' )
insert into @pardate values ( 2 , '2011-09-17 12:44' )
insert into @pardate values ( 3 , '2011-10-11 12:45' )
insert into @pardate values ( 4 , '2011-10-12 12:46' )
insert into @pardate values ( 5 , '2011-10-13 12:47' )
insert into @pardate values ( 6 , '2011-11-20 12:48' )
insert into @pardate values ( 7 , '2011-11-21 12:49' )
insert into @pardate values ( 8 , '2011-11-22 12:50' )
declare @child table ( childid int , pardateid int , childvalue char(6) )
insert into @child values ( 1 , 1 , 'aaaaaa' )
insert into @child values ( 2 , 2 , 'bbbbbb' )
insert into @child values ( 3 , 3 , 'cccccc' )
insert into @child values ( 4 , 4 , 'dddddd' )
insert into @child values ( 5 , 5 , 'cccccc' )
insert into @child values ( 6 , 6 , 'cccccc' )
insert into @child values ( 7 , 7 , 'eeeeee' )
insert into @child values ( 8 , 8 , 'ffffff' )

select pardatewhen , childvalue , COUNT(childvalue)
from @child childtable join @pardate parenttable on childtable.pardateid=parenttable.pardateid
group by pardatewhen , childvalue

I am trying to get a count of @child.childvalue every day, every hour, so there would be 8760 rows in my result.
First pass had a loop and a CONVERT which takes ~5 minutes to run with the actual result set (this is just a sample for illustation). I did create a CTE to make a calendar temp table (using http://www.sqlpointers.com/2006/07/generating-temporary-calendar-tables.html), and thought it could be joined somehow to add “empty values” into the result set.

I need to get a result set that looks like this

date          hour    count
...
2011-09-17    0       0
....
2011-09-17    12      2
....
2011-10-11    12      1

How can that be done efficiently?

Thanks.

  • 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-12T01:25:43+00:00Added an answer on June 12, 2026 at 1:25 am

    try this.

    ;WITH cal AS
    (SELECT CAST('2011-01-01' AS DATETIME) AS cal_date
      UNION ALL
     SELECT DATEADD(hour,1,cal_date)
      FROM cal
      WHERE cal_date < '2011-12-31 23:00'
    )  
    , par AS
    (
        select CAST(pardatewhen AS DATE) AS pardate, DATEPART(hh,pardatewhen) AS parhour , COUNT(childvalue) as num
        from @child childtable 
        join @pardate parenttable on childtable.pardateid=parenttable.pardateid 
        group by CAST(pardatewhen AS DATE), DATEPART(hh,pardatewhen)  
    )
    SELECT CAST(cal.cal_date  AS DATE) AS [date],DATEPART(hh,cal.cal_date) AS [hour],ISNULL(par.num,0) AS [childvalue_count]
    FROM    cal  
    LEFT JOIN par
        ON CAST(cal.cal_date  AS DATE) = par.pardate
            AND DATEPART(hh,cal.cal_date) = par.parhour
    OPTION (MAXRECURSION 9999)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL Server 2008: DECLARE @MyTable TABLE( PersonID INT NOT NULL, Person2ID INT NOT NULL,
I have following tsql code in sql server 2008: declare @ID INT SET @ID
In SQL Server 2008, consider the table create table myTable (id int, filler char(X))
I have a table variable in SQL Server 2008 DECLARE @specsAndModel TABLE ( specName
If You run this script on sql server 2008, works fine. declare @tab table(
I have a SQL Server 2008 R2 table with the following Schema DECLARE @AttributeTable
Using SQL Server 2008, I need to select this XML into a Table. I
Using SQL Server 2008 R2 Consider a declared table variable like: DECLARE @t TABLE
In SQL Server 2008: declare @Value float declare @result float set @Value=7.95 select @result=round(@Value,1)
This is SQL Server 2008. I have these two tables and a join: DECLARE

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.