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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:34:33+00:00 2026-05-20T20:34:33+00:00

Given a table with schema like this: id1 id2 day number How can I

  • 0

Given a table with schema like this:

id1    id2    day    number

How can I turn this:

a    b    day1    2
a    b    day5    4
a    b    day9    8
c    d    day2    1
c    d    day3    2
c    d    day5    4
c    d    day8    3

Into this?:

a    b    day1    2
a    b    day2    2
a    b    day3    2
a    b    day4    2
a    b    day5    4
a    b    day6    4
a    b    day7    4
a    b    day8    4
a    b    day9    8
c    d    day2    1
c    d    day3    2
c    d    day4    2
c    d    day5    4
c    d    day6    4
c    d    day7    4
c    d    day8    3

To clarify, for each group of id1 and id2, I need to fill in the missing rows
with dates ranging from the minimum date for that grouping to the maximum
date. Furthermore, the rows that get filled in must use the previous entry’s
number column for it’s number column.

I need this to run as fast as possible.

Bonus points if you can do it in LINQ to SQL (assuming the class exists for
the table).

EDIT: The day column is actually an int that represents the day, but for the sake of argument, it could be a date.

I’ve done the naive approach of iterating over each group and adding in the missing days, but it just seems terribly inefficient. I have to think that there’s something faster or that someone has encountered this situation before.

  • 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-20T20:34:34+00:00Added an answer on May 20, 2026 at 8:34 pm
    WITH    dates (id1, id2, ds, de) AS
            (
            SELECT  id1, id2, MIN(d), MAX(d)
            FROM    mytable m
            GROUP BY
                   id1, id2
            UNION ALL
            SELECT  d.id1, d.id2, DATEADD(d, 1, ds), de
            FROM    dates d
            WHERE   ds < de
            )
    SELECT  id1, id2, ds, m.number
    FROM    dates d
    CROSS APPLY
            (
            SELECT  TOP 1 number
            FROM    mytable m
            WHERE   m.id1 = d.id1
                    AND m.id2 = d.id2
                    AND m.d <= d.ds
            ORDER BY
                    m.d DESC
            ) m
    OPTION (MAXRECURSION 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table named person (in a MySQL database/schema), kind of like this one:
Given a table like: CREATE TABLE MyTable ( MyColumn NUMBER NOT NULL ); I
I store time-series simulation results in PostgreSQL. The db schema is like this. table
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
I have got a post table and it's schema is like this: CREATE TABLE
My database schema looks like this: Table t1: id valA valB Table t2: id
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE
Given: CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id)); I'd like to
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
I have a table that looks like this: Id (PK, int, not null) ReviewedBy

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.