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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:54:14+00:00 2026-05-15T04:54:14+00:00

I have the following table which records a value per day. The problem is

  • 0

I have the following table which records a value per day. The problem is that sometimes days are missing. I want to write a SQL query that will:

  1. Return the missing days
  2. Calculate the missing value using linear interpolation

So from the following source table:

Date           Value
--------------------
2010/01/10     10
2010/01/11     15
2010/01/13     25
2010/01/16     40

I want to return:

 Date           Value
 --------------------
 2010/01/10     10
 2010/01/11     15
 2010/01/12     20
 2010/01/13     25
 2010/01/14     30
 2010/01/15     35
 2010/01/16     40

Any help would be greatly appreciated.

  • 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-15T04:54:14+00:00Added an answer on May 15, 2026 at 4:54 am
    declare @MaxDate date
    declare @MinDate date
    
    select @MaxDate = MAX([Date]),
            @MinDate = MIN([Date])
    from Dates
    
    declare @MaxValue int
    declare @MinValue int
    
    select @MaxValue = [Value] from Dates where [Date] = @MaxDate
    select @MinValue = [Value] from Dates where [Date] = @MinDate
    
    declare @diff int
    select @diff = DATEDIFF(d, @MinDate, @MaxDate)
    
    declare @increment int
    set @increment = (@MaxValue - @MinValue)  / @diff
    
    select @increment
    
    declare @jaggedDates as table
    (
        PID INT IDENTITY(1,1) PRIMARY KEY,
        ThisDate date,
        ThisValue int
    )
    
    declare @finalDates as table
    (
        PID INT IDENTITY(1,1) PRIMARY KEY,
        [Date] date,
        Value int
    )
    
    declare @thisDate date
    declare @thisValue int
    declare @nextDate date
    declare @nextValue int
    
    declare @count int
    insert @jaggedDates select [Date], [Value] from Dates
    select @count = @@ROWCOUNT
    
    declare @thisId int 
    set @thisId = 1
    declare @entryDiff int
    declare @missingDate date
    declare @missingValue int
    
    while @thisId <= @count
    begin
        select @thisDate = ThisDate,
                @thisValue = ThisValue
        from @jaggedDates
        where PID = @thisId
    
        insert @finalDates values (@thisDate, @thisValue)
    
        if @thisId < @count
        begin
            select @nextDate = ThisDate,
                @nextValue = ThisValue
            from @jaggedDates
            where PID = @thisId + 1
    
            select @entryDiff = DATEDIFF(d, @thisDate, @nextDate)
            if  @entryDiff > 1
            begin
                set @missingDate = @thisDate
                set @missingValue = @thisValue
                while @entryDiff > 1
                begin
                    set @missingDate = DATEADD(d, 1, @missingDate)
                    set @missingValue = @missingValue + @increment
                    insert @finalDates values (@missingDate, @missingValue)
                    set @entryDiff = @entryDiff - 1
                end
            end
        end
    
        set @thisId = @thisId + 1
    end
    
    select * from @finalDates
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following sql which is show previous days records from table. SELECT *
I have the following statement that I need to run on a table which
I have a table that allows records to be inserted which are very similar
I have a table called TableA which has the following records: TableA: ID Jan
I have the following table structure inside a view which gets displayed in the
I have the following situation: An Conversations entity/table which has multiple Tags associated to
I have the following table structure, which is imported into an Entity Framework project:
I have the following hsqldb table, in which I map UUIDs to auto incremented
I have an sql table which has the following rows. (4081, 3, '', 'contrapreneurship.jpg',
i have a table Students which contains the following colums: Id,FirstName,LastName,Adress . The colum

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.