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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:11:18+00:00 2026-05-11T22:11:18+00:00

This may not be possible, but I thought I’d throw it out here: Given

  • 0

This may not be possible, but I thought I’d throw it out here:

Given the following table:

ID, Begin, End
123, 1, N

Where N is an integer, write a query to return the following result set:

ID, Begin, End
123, 1, 1
123, 1, 2
123, 1, 3
.
.
.
123, 1, N

The platform we are using is SQL Server 2005, but if you can do it with another flavor of SQL, I’d still be interested in the solution.

  • 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-11T22:11:19+00:00Added an answer on May 11, 2026 at 10:11 pm

    try this:

    create table #smalltable (id int, [begin] int, [end] int)
    insert into #smalltable values (123,1,4)
    insert into #smalltable values (124,1,12)
    insert into #smalltable values (125,1,7)
    
    ;WITH digits (d) AS (
        SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION
        SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
        SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION
        SELECT 0)
    SELECT
        s.id, s.[begin], n.Number AS [End]
        FROM (SELECT i.d + ii.d * 10 + iii.d * 100 + iv.d * 1000 +
                  v.d * 10000 + vi.d * 100000 AS Number
                  FROM digits            i
                      CROSS JOIN digits  ii
                      CROSS JOIN digits  iii
                      CROSS JOIN digits  iv
                      CROSS JOIN digits  v
                      CROSS JOIN digits  vi
             ) AS N
            INNER JOIN #smalltable                                    s  ON 1=1
            INNER JOIN (SELECT MAX([end]) AS MaxEnd FROM #smalltable) dt ON 1=1
       WHERE n.Number > 0 AND n.Number<=dt.MaxEnd
        AND n.Number<=s.[end]
       ORDER BY s.id,n.Number
    

    comments

    • don’t name your columns the reserved words: “begin” and “end”, you’ll thank me some day.
    • if you plan to run this in production many times, create a Numbers table
      and use this query instead:

    have to have a table Numbers before this will work (see link above)

    SELECT
        s.id,s.[begin],n.Number AS [End]
        FROM Numbers                n
            INNER JOIN #smalltable  s ON 1=1
       WHERE  n.Number > 0 AND n.Number<=s.[end]
       ORDER BY s.id,number
    

    it will run better.

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

Sidebar

Related Questions

This may not be the correct way to use controllers, but I did notice
This may not really sound hard to do but it is currently killing me.
Okay, this may be a dumb question, but I've not been able to find
This may be a silly question to a graphics guru (which I am not),
This is one of those meta-programming questions that may or may not belong on
I hope this question does not come off as broad as it may seem
This may be a no-brainer for the WPF cognoscenti, but I'd like to know
This may seem like a daft question, but i was wondering about how to
This may seem like a programming 101 question and I had thought I knew
This may be simple one, but 5 mins of Googling didn't give me the

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.