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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 356k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Normally the Repeater would implement something different, like an IItemsControl… May 14, 2026 at 9:00 am
  • Editorial Team
    Editorial Team added an answer It turns out that the test suite was deleting the… May 14, 2026 at 9:00 am
  • Editorial Team
    Editorial Team added an answer You should probably store phone numbers as a varchar. Phone… May 14, 2026 at 9:00 am

Related Questions

This may not be possible, but I thought I'd throw it out here: Given
I don't know how authoritative this is but I found this: http://www.sqlite.org/cvstrac/wiki?p=PerformanceConsiderations and it
I need to send a C struct over the wire (using UDP sockets, and
Given an aggregation of class instances which refer to each other in a complex,
I have an Infragistics UltraNumericEditor (version 5.3, quite old) control on a form. If

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.