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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:54:45+00:00 2026-05-17T15:54:45+00:00

I have some data on my table like: DAY | QTY | Name 1/1/2010

  • 0

I have some data on my table like:

DAY      | QTY | Name
1/1/2010 |  1  | jack
5/1/2010 |  5  | jack
2/1/2010 |  3  | wendy
5/1/2010 |  2  | wendy

my goal is to have a SP requesting a period of time (example: ‘2010-1-1’ to ‘2010-1-5’), and get no gaps.
Output example:

DAY      | QTY | Name
1/1/2010 |  1  | jack
2/1/2010 |  0  | jack
3/1/2010 |  0  | jack
4/1/2010 |  0  | jack
5/1/2010 |  5  | jack
1/1/2010 |  3  | wendy
2/1/2010 |  0  | wendy
3/1/2010 |  0  | wendy
4/1/2010 |  2  | wendy
5/1/2010 |  0  | wendy

Any gaps is filled with 0-
I know that I can create a loop to will solve me the problem, but is very slow.

Does anyone have any ideas how to optimize this?

  • 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-17T15:54:46+00:00Added an answer on May 17, 2026 at 3:54 pm

    I leave were the correct answer based on the help of everyone

    -- dummy data
    declare @table table
    (
        DAY datetime,
        QTY int,
        Name nvarchar  (500) NULL
    )
    insert @table values('2010-1-1',  1, 'jack')
    insert @table values('2010-1-3',  5,  'jack')
    insert @table values('2010-1-2',  3 , 'wendy')
    insert @table values('2010-1-6',  2 , 'wendy')
    
    
    -- algorithm
    DECLARE @output TABLE (
        DAY datetime,
        Qty int,
        Name varchar(25)
    )
    DECLARE @minMonth datetime, @maxMonth datetime, @lastName varchar(25)
    SET @minMonth = '2010-1-1' 
    SET @maxMonth = '2010-1-6';
    
    WITH cte AS (
            SELECT @minMonth AS DateValue
        UNION ALL
            SELECT DATEADD(day, 1, DateValue)
            FROM cte
            WHERE DATEADD(day, 1, DateValue) <= @maxMonth
    ) 
    INSERT INTO @output 
        SELECT 
            cte.DateValue, 
            ISNULL(tbl.qty,0), 
            tbl.Name
        FROM 
            cte cross JOIN 
            @table tbl
    
    update @output
    set qty = 0
    where cast(DAY as nvarchar)+'@'+cast(Qty as nvarchar)+'@'+Name in 
    (
        select cast(DAY as nvarchar)+'@'+cast(Qty as nvarchar)+'@'+Name from @output
        except
        select cast(DAY as nvarchar)+'@'+cast(Qty as nvarchar)+'@'+Name from @table
    )
    
    SELECT DAY, sum(qty) as qty, Name
    FROM @output
    GROUP BY DAY, Name
    order by 3,1
    

    and the output that I pretend

    2010-01-01 00:00:00.000 1   jack
    2010-01-02 00:00:00.000 0   jack
    2010-01-03 00:00:00.000 5   jack
    2010-01-04 00:00:00.000 0   jack
    2010-01-05 00:00:00.000 0   jack
    2010-01-06 00:00:00.000 0   jack
    2010-01-01 00:00:00.000 0   wendy
    2010-01-02 00:00:00.000 3   wendy
    2010-01-03 00:00:00.000 0   wendy
    2010-01-04 00:00:00.000 0   wendy
    2010-01-05 00:00:00.000 0   wendy
    2010-01-06 00:00:00.000 2   wendy
    

    Although the solution is correct, doesn’t fit my need because recursion limitation.

    Hopefully this script will help anyone with similar questions

    Thank you to all

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

Sidebar

Related Questions

I have an Excel table with some data that I would like to export
I have some data grouped in a table by a certain criteria, and for
I have some data that I have to display as a table. I think
I have some data I am querying. The table is composed of two columns
i have some rows of data displayed in a table and on mouse over
I have to delete some rows from a data table. I've heard that it
I have a data table which already has some values, plus it is getting
Ok, I have a table which contains multiple rows. Each row contains some data
I have a table containing data about some users. Many of them use our
I have a database table full of some really ugly and messy data. In

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.