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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:13:31+00:00 2026-06-01T03:13:31+00:00

I have a noobie question: I need to write SQL script that returns 3

  • 0

I have a noobie question:

I need to write SQL script that returns 3 columns results set from a given one column table @T with integer field N, e.g.:

declare @t table(n int) 
insert into @t(n) values (3),(4),(5),
                         (6),(7),(8),
                         (9),(10),(11),
                         (13),(14),(15),
                         (16),(17),(18), (19)

The result 3 columns table should contains data from the table @T row by row in ascending order. If there is a missed values (e.g. {2,4} 3 is missing here) in the table @T, then the corresponding cell in result set should be NULL.

Example 1:
For table with values {2,3,4,5} the result table should looks like:

NULL    2   3 
4    5  NULL 

Example 2:
For table with values {2,4} the result table should looks like:

NULL    2   NULL 
4    NULL  NULL 
  • 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-06-01T03:13:32+00:00Added an answer on June 1, 2026 at 3:13 am

    You can use a numbers table like this:

    select T1.n, 
           T2.n, 
           T3.n 
    from Numbers as N
      left outer join @T as T1
        on N.Number * 3 + 1 = T1.n
      left outer join @T as T2
        on N.Number * 3 + 2 = T2.n
      left outer join @T as T3
        on N.Number * 3 + 3 = T3.n
    where N.Number between 0 and (select max(n) from @T) / 3
    

    Working sample using master..spt_values as a numbers table.

    declare @t table(n int) 
    insert into @t(n) values (3),(4),(5),
                             (6),(7),(8),
                             (9),(10),(11),
                             (13),(14),(15),
                             (16),(17),(18), (19)
    
    
    ;with Numbers(Number) as
    (
      select number
      from master..spt_values
      where type = 'P'
    )
    
    select T1.n, 
           T2.n, 
           T3.n 
    from Numbers as N
      left outer join @T as T1
        on N.Number * 3 + 1 = T1.n
      left outer join @T as T2
        on N.Number * 3 + 2 = T2.n
      left outer join @T as T3
        on N.Number * 3 + 3 = T3.n
    where N.Number between 0 and (select max(n) from @T) / 3
    

    Or you can use a numbers table with pivot:

    select [1], [2], [3]
    from
      (
        select (N.Number - 1) / 3 as Number,
               T.n,
               1 + ((N.Number - 1) % 3) as rn
        from Numbers as N
          left outer join @T as T on
            N.Number = T.n
        where N.Number between 1 and (select max(n) from @T)
      ) as T
    pivot
      (min(n) for rn in ([1], [2], [3])) as P
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Noobie question: I have sensitive content (images) that I want to place in a
I am a jQuery noobie, and have been trying to add a script to
this is a noobie question regarding tree maps. I have read through the Java
So, the noobie question of the day... I have an XML file with the
This may be a noobie questions, but... So I have a pyc file that
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have had to write my first proper multithreaded coded recently, and realised just how
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the
I've looked at the routing on StackOverflow and I've got a very noobie question,
Have converted devise new session from erb to Haml but doens't work, this is

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.