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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:47:25+00:00 2026-05-26T04:47:25+00:00

I am using MS SQL Server 2005. I think PIVOT could help me here,

  • 0

I am using MS SQL Server 2005.

I think PIVOT could help me here, but I can’t figure it out. I must be over thinking.
Here is the input

create table #myrows (id char(1), seq_i int, val char(10))

insert into #myrows values('A',1, 'A1') 
insert into #myrows values('A',2, 'A2') 
insert into #myrows values('A',3, 'A3') 
insert into #myrows values('A',4, 'A4') 
insert into #myrows values('A',5, 'A5') 
insert into #myrows values('A',6, 'A6') 
insert into #myrows values('A',7, 'A7') 
insert into #myrows values('A',8, 'A8') 
insert into #myrows values('A',9, 'A9') 
insert into #myrows values('A',10, 'A10') 

insert into #myrows values('B',1, 'B1') 
insert into #myrows values('B',2, 'B2') 
insert into #myrows values('B',3, 'B3') 
insert into #myrows values('B',4, 'B4') 
insert into #myrows values('B',5, 'B5') 
insert into #myrows values('B',6, 'B6') 

insert into #myrows values('C',1, 'C1') 
insert into #myrows values('C',2, 'C2') 
insert into #myrows values('C',3, 'C3') 

I can do it with T-SQL when I passing the id. But I seems like there is an easy sql view that I could create that doesn’t require me to send it the id. Here is the T-SQL that gets me the output I want for one id:

DECLARE @max_hierarchy int
DECLARE @code CHAR(1)

select @code = 'C'

SELECT  @max_hierarchy = max(seq_i)
FROM #myrows
WHERE id=@code

SELECT top 1
(SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy) AS 'Level1',
(SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-1) AS 'Level2',
(SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-2) AS 'Level3',
(SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-3) AS 'Level4',
(SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-4) AS 'Level5'
from #myrows
WHERE id=@code

Ideally this would be be my output of SQL I am looking for:

Code Level1      Level2      Level3      Level4      Level5
---- ----------- ----------- ----------- ----------- -----------
A    A10         A9          A8          A7          A6
B    B6          B5          B4          B3          B2
C    C3          C2          C1          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-05-26T04:47:25+00:00Added an answer on May 26, 2026 at 4:47 am

    Since you want only the top 5 levels, you don’t need any dynamic pivoting and this should work:

    with cte as (
    select id, seq_i, val, ROW_NUMBER() over (partition by id order by seq_i desc) rn
    from #myrows
    )
    select 
      id,
      max(case rn when 1 then val else null end) Level1,
      max(case rn when 2 then val else null end) Level2,
      max(case rn when 3 then val else null end) Level3,
      max(case rn when 4 then val else null end) Level4,
      max(case rn when 5 then val else null end) Level5
    from cte
    group by id
    

    Update

    Now, if you want to make things more interesting and have your levels dynamically, here’s the not-so-trivial-yet-lot-of-fun-on-coding solution:

    create table #cte (id char(1), seq_i int, val char(10), level varchar(10))
    
    ;with cte as (
    select id, seq_i, val, ROW_NUMBER() over (partition by id order by seq_i desc) rn
    from #myrows
    )
    insert into #cte (id, seq_i, val, level)
    select id, seq_i, val, 
           'Level' + right('000' + cast(rn as varchar), 4) from cte 
    
    DECLARE @cols VARCHAR(1000)
    DECLARE @sqlquery VARCHAR(2000)
    
    SELECT  @cols = STUFF(( SELECT distinct  ',' + QuoteName(level)
                            FROM #cte FOR XML PATH('')  ), 1, 1, '') 
    
    select @cols 
    
    SET @sqlquery = 'SELECT * FROM
          (SELECT id, level, val
           FROM #cte ) base
           PIVOT (max(val) FOR [level]
           IN (' + @cols + ')) AS finalpivot'
    
    EXECUTE ( @sqlquery )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using SQL Server 2005, but this question can be for any RDBMS
When using SQL Server Management Studio from SQL Server 2005, I can connect to
I'm using SQL Server 2005. I have a field that must either contain a
i am using sql server 2005 and I think Linq generating queries for a
Using SQL Server 2005 and Management Studio how do I insert a picture into
I am using SQL Server 2005. I have a table with a text column
I'm using SQL Server 2005, and I would like to know how to access
We're using SQL Server 2005 in a project. The users of the system have
I am using sql server 2005. I just want to know is there something
I am using SQL Server 2005. I want to constrain the values in a

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.