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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:45:19+00:00 2026-05-16T14:45:19+00:00

I have a table of data that looks a bit like this: Name StartTime

  • 0

I have a table of data that looks a bit like this:

Name    StartTime              FinishTime              Work
Bob     2010-08-03 08:00:00    2010-08-03 12:00:00     4
Bob     2010-08-03 13:00:00    2010-08-03 16:00:00     3
Pete    2010-08-04 08:00:00    2010-08-04 12:00:00     4
Mark    2010-08-04 10:00:00    2010-08-04 12:00:00     2

None of these date ranges should ever span over midnight.
I want to write SQL that will give me the following output, given an input Start Date of 2010-08-02 and a Finish Date of 2010-08-05

Date          Name   TotalWork
2010-08-03    Bob    7
2010-08-03    Pete   3
2010-08-04    Pete   4
2010-08-04    Mark   2 

I could live with, and in fact may ultimately need, to have any days that do not have work associated also be represented in the results set, maybe as a row like this:

2010-08-05     NULL   0

I’m not quite sure how to iterate through dates in SQL in the same way that I would with other languages.

To give this some context, the output of this will ultimately plug into a Stacked Chart .Net control.

Could someone give me a clue, a link to a tutorial or some other help? Otherwise I think I’ll be fiddling with this for days!

Thank you!

Jonathan

  • 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-16T14:45:20+00:00Added an answer on May 16, 2026 at 2:45 pm

    Try this:

    Select DateAdd(day, 0, DateDiff(day, 0, StartDate)) Date,
        Name, Sum (Work) TotalWork
    From TableData
    Group By Name, DateAdd(day, 0, DateDiff(day, 0, StartDate)) 
    

    To get the missing days is harder.

       Declare @SD DateTime, @ED DateTime  -- StartDate and EndDate variables
       Select @SD = DateAdd(day, 0, DateDiff(day, 0, Min(StartDate))),
              @ED = DateAdd(day, 0, DateDiff(day, 0, Max(StartDate)))
       From TableData
       Declare @Ds Table (aDate SmallDateTime)
       While @SD <= @ED Begin 
           Insert @Ds(aDate ) Values @SD
           Set @SD = @SD + 1
       End 
    -- ----------------------------------------------------
     Select DateAdd(day, 0, DateDiff(day, 0, td.StartDate)) Date,
        td.Name, Sum (td.Work) TotalWork
     From @Ds ds Left Join TableData td
        On DateAdd(day, 0, DateDiff(day, 0, tD.StartDate)) = ds.aDate 
     Group By Name, DateAdd(day, 0, DateDiff(day, 0, tD.StartDate)) 
    

    EDIT, I am revisiting this with a solution that uses a Common Table Expression (CTE). This does NOT require use of a dates table.

        Declare @SD DateTime, @ED DateTime
        Declare @count integer = datediff(day, @SD, @ED)
        With Ints(i) As
          (Select 0 Union All
        Select i + 1 From Ints
        Where i < @count )  
         Select DateAdd(day, 0, DateDiff(day, 0, td.StartDate)) Date,
             td.Name, Sum (td.Work) TotalWork
         From Ints i 
            Left Join TableData d
               On DateDiff(day, @SD, d.StartDate) = i.i
         Group By d.Name, DateAdd(day, 0, DateDiff(day, 0, d.StartDate)) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with some data that looks like: id | type |
Hei guys i have this JQuery Ajax call from my view and it looks
I have a table on a iSeries(IBM-i/AS400) which has some constraints. The table is
I have a db related to horse racing performance and want to compare a
No worries! It looks more complex than it actually is! Just get down to
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
I'm currently debugging a huge MySql call which joins a large amount of tables
Edit: Building off of aL3xa's answer below, I've modified his syntax below. Not perfect,
I'm working on a database for stat keeping for a sports club where I

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.