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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:57+00:00 2026-05-22T18:41:57+00:00

This question has already been posted before, however, the correct answer doesn’t seem to

  • 0

This question has already been posted before, however, the correct answer doesn’t seem to work, so I’m asking it again.

This code, should return the last 31 days with zero if there are no records;

   SELECT dates.Date as Dates, isnull(Sum(ElapsedTimeSeconds),0) as ElapsedSeconds
FROM 
[fnDateTable] (dateadd("m",-1,CONVERT(VARCHAR(10),GETDATE(),111)),CONVERT(VARCHAR(10),GETDATE(),111),'day') dates 
LEFT JOIN UsersApplog on dates.date = UsersAppLog.LoggedDate
group by Dates.Date

See the [fnDateTable] here;
fnDateTable

what I get back is JUST the rows which have data. Very annoying.

Thanks for any help you can give. I’m sure it’s a simple solve, but it’s beyond me.

This is some output where there is data from the userapplog;

Dates   ElapsedSeconds
2011-05-17 00:00:00.000 5854
2011-05-18 00:00:00.000 5864
2011-05-21 00:00:00.000 4758
2011-05-22 00:00:00.000 8434
2011-05-23 00:00:00.000 2162
2011-05-25 00:00:00.000 491
2011-05-26 00:00:00.000 260
2011-05-28 00:00:00.000 216

If I run;

 SELECT dates.Date as Dates
   FROM 
   [fnDateTable] (dateadd("m",-   1,CONVERT(VARCHAR(10),GETDATE(),111)),CONVERT(VARCHAR(10),GETDATE(),111),'day') dates 

I get

Dates
2011-04-29 00:00:00.000
2011-04-30 00:00:00.000
2011-05-01 00:00:00.000
2011-05-02 00:00:00.000
2011-05-03 00:00:00.000
2011-05-04 00:00:00.000
2011-05-05 00:00:00.000
.
.
  • 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-22T18:41:58+00:00Added an answer on May 22, 2026 at 6:41 pm

    Since you are using SQL Server 2008, you could try something like as described below using Common Table Expression (CTE) and OUTER APPLY.

    Common Table Expressions can be used to perform recursive functions. In this case, the CTE takes the initial date as the date 1 month prior from current date and then recursively loops by adding 1 day until the loop reaches the current date. Hence, forming the list of dates between two given dates. Using that CTE output, we can use OUTER APPLY to find the sum of elapsed duration for a given date range.

    You can also create the CTE as table-valued function and use it as shown under section Table-valued function. Both options provided below are same and they are separating the functionalities slightly differently.

    Screenshot #1 shows the sample data stored in the table dbo.UsersAppLog and the screenshot #2 displays the output. The output will be same using both approaches because the only difference between below given options is one of them have part of the logic moved to a function.
    .

    Option #1

    Desired output without using table-valued function:

    DECLARE @BeginDate  DATETIME;
    DECLARE @EndDate    DATETIME;
    
    SET @BeginDate  = DATEADD(MONTH, -1, GETDATE());
    SET @EndDate    = GETDATE();
    
    WITH CTE(DateRange) AS
    (
            SELECT  @BeginDate  AS DateRange
        UNION ALL
            SELECT  DATEADD(DAY, 1, DateRange)
            FROM    CTE
            WHERE   DATEADD(DAY, 1, DateRange) <= @EndDate
    )
    SELECT          DATEADD(DAY, 0, DATEDIFF(DAY, 0, CTE.DateRange))    AS DateRange
                ,   COALESCE(UAL.ElapsedDuration, 0)                    AS ElapsedDuration
    FROM            CTE
    OUTER APPLY     (
                        SELECT  SUM(ElapsedSeconds) ElapsedDuration
                        FROM    dbo.UsersAppLog UAL
                        WHERE   DATEDIFF(DAY, UAL.LoggedDate, CTE.DateRange) = 0
                    ) UAL;
    

    Option #2

    Create script for Table-Valued function:
    .

    CREATE FUNCTION [dbo].[fntDateRange]
    (   
            @BeginDate  DATETIME
        ,   @EndDate    DATETIME
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
        WITH CTE(DateRange) AS
        (
                SELECT  @BeginDate  AS DateRange
            UNION ALL
                SELECT  DATEADD(DAY, 1, DateRange)
                FROM    CTE
                WHERE   DATEADD(DAY, 1, DateRange) <= @EndDate
        )
        SELECT          DATEADD(DAY, 0, DATEDIFF(DAY, 0, CTE.DateRange))    AS DateRange
        FROM            CTE
    )
    GO
    

    Desired output using the table-valued function:
    .

    SELECT          RNG.DateRange
                ,   COALESCE(UAL.ElapsedDuration, 0) AS ElapsedDuration
    FROM            dbo.fntDateRange(DATEADD(MONTH, -1, GETDATE()), GETDATE()) RNG
    OUTER APPLY     (
                        SELECT  SUM(ElapsedSeconds) ElapsedDuration
                        FROM    dbo.UsersAppLog UAL
                        WHERE   DATEDIFF(DAY, UAL.LoggedDate, RNG.DateRange) = 0
                    ) UAL;
    

    Hope that helps.

    Screenshot #1:

    Data

    Screenshot #2:

    Output

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

Sidebar

Related Questions

If this question has already been asked I appologies, please point me in the
Preamble So, this question has already been answered, but as it was my first
This question has been asked before ( link ) but I have slightly different
I know this question has been asked before, but I ran into a problem.
I know this question has been asked a bit before. But looking around I
I know this specific question has been asked before , but I am not
This question to which I already found the answer is posted here in case
I'm sure that this question has already been asked, but I don't really see
I am quite sure this question has already been asked several times and I
Well, I'm sure this question has been asked before but I'm yet to find

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.