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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:37:39+00:00 2026-06-18T09:37:39+00:00

I have a scenario where I wish to display the column values(Val1) for each

  • 0

I have a scenario where I wish to display the column values(Val1) for each unique column value (Val2) as an individual column, with a max of 10 columns.

CREATE TABLE #TEMP1 (Val1 NVARCHAR(4), Val2 NVARCHAR(10));

insert into #Temp1 Values ('S01','00731')
insert into #Temp1 Values ('S02','00731')
insert into #Temp1 Values ('S03','00731')
insert into #Temp1 Values ('S04','00731')
insert into #Temp1 Values ('S05','00731')
insert into #Temp1 Values ('S06','00731')
insert into #Temp1 Values ('S07','00731')
insert into #Temp1 Values ('S08','00731')
insert into #Temp1 Values ('S09','00731')
insert into #Temp1 Values ('S07','00731')
insert into #Temp1 Values ('S04','00741')
insert into #Temp1 Values ('S01','00746')
insert into #Temp1 Values ('S01','00770')
insert into #Temp1 Values ('S01','00771')
insert into #Temp1 Values ('S02','00771')

Val1    Val2
--------------------------
S01     00731
S02     00731
S03     00731
S04     00731
S05     00731
S06     00731
S07     00731
S08     00731
S09     00731
S07     00731
S04     00741
S01     00746
S01     00770
S01     00771
S02     00771

I then use a pivot column to show each unique Val2 value and with a max of 10 Val1 values as columns.

SELECT [Val2],
c1, c2, c3, c4, c5, c6, c7, c8, c9, c10
FROM
(SELECT Val1, Val2
FROM         #TEMP1) AS PivotTable
PIVOT
(
MAX([PivotTable].[Val1])
FOR
Val1
IN
(C1, c2, c3, c4, c5, c6, c7, c8, c9, c10)
) AS PivotTable;

I wish to have results like:

Val2    c1  c2  c3  c4  c5  c6  c7  c8  c9  c10
--------------------------------------------------------------------------------------
00731  S01  S02 S03 S04 S05 S06 S07 S08 S09 S07 
00741  S04  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00746  S01  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00770  S01  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00771  S01  S02 NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

But i actually just get all NULL values for the columns:

Val2    c1  c2  c3  c4  c5  c6  c7  c8  c9  c10
--------------------------------------------------------------------------------------
00731  NULL NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00741  NULL NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00746  NULL NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00770  NULL NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
00771  NULL NULL    NULL    NULL    NULL    NULL    NULL    NULL    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-18T09:37:40+00:00Added an answer on June 18, 2026 at 9:37 am

    Your requirements are not totally clear but it looks like you are trying to create a new column named c with then a row_number() associated with it — c1, c2 c3, etc.

    If you were to use the following in your subquery:

    SELECT Val1, Val2,
      'C'+ cast(row_number() over(partition by Val2 
                                  order by val1) as varchar(10)) col
    FROM TEMP1
    

    See SQL Fiddle with Demo

    You would get the result:

    | VAL1 |  VAL2 | COL |
    ----------------------
    |  S01 | 00731 |  C1 |
    |  S02 | 00731 |  C2 |
    |  S03 | 00731 |  C3 |
    |  S04 | 00731 |  C4 |
    |  S05 | 00731 |  C5 |
    |  S06 | 00731 |  C6 |
    |  S07 | 00731 |  C7 |
    |  S07 | 00731 |  C8 |
    |  S08 | 00731 |  C9 |
    |  S09 | 00731 | C10 |
    |  S04 | 00741 |  C1 |
    |  S01 | 00746 |  C1 |
    |  S01 | 00770 |  C1 |
    |  S01 | 00771 |  C1 |
    |  S02 | 00771 |  C2 |
    

    Which seems to be the result that you then want to PIVOT. You would then apply the PIVOT to this using:

    SELECT Val2,
       c1, c2, c3, c4, c5, c6, c7, c8, c9, c10
    FROM
    (
      SELECT Val1, Val2,
        'C'+ cast(row_number() over(partition by Val2 
                                    order by val1) as varchar(10)) col
      FROM TEMP1
    ) src
    PIVOT
    (
      MAX(Val1)
      FOR col IN (C1, C2, C3, C4, C5, C6, C7, C8, C9, C10)
    ) piv;
    

    See SQL Fiddle with Demo. Your final result is then:

    |  VAL2 |  C1 |     C2 |     C3 |     C4 |     C5 |     C6 |     C7 |     C8 |     C9 |    C10 |
    ------------------------------------------------------------------------------------------------
    | 00731 | S01 |    S02 |    S03 |    S04 |    S05 |    S06 |    S07 |    S07 |    S08 |    S09 |
    | 00741 | S04 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    | 00746 | S01 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    | 00770 | S01 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    | 00771 | S01 |    S02 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    

    Note: my results are slightly different from what you are requesting as the desired result because I am performing an ORDER BY val1 which causes the S07 values to be grouped together.

    There is no order of data in a database unless you request one, so there is no guarantee that one of the S07 values will appear as C10. You could use the following to get the result but there is no guarantee that the result will always be in the correct order:

    SELECT Val2,
      c1, c2, c3, c4, c5, c6, c7, c8, c9, c10
    FROM
    (
      SELECT Val1, Val2,
        'C'+ cast(row_number() over(partition by Val2 
                                    order by (select 1)) as varchar(10)) col
      FROM TEMP1
    ) src
    PIVOT
    (
      MAX(Val1)
      FOR col IN (C1, C2, C3, C4, C5, C6, C7, C8, C9, C10)
    ) piv;
    

    See SQL Fiddle with Demo. Using the order by (select 1) alters the order of the data but it does not guarantee that it will always be in that order. The result is:

    |  VAL2 |  C1 |     C2 |     C3 |     C4 |     C5 |     C6 |     C7 |     C8 |     C9 |    C10 |
    ------------------------------------------------------------------------------------------------
    | 00731 | S01 |    S02 |    S03 |    S04 |    S05 |    S06 |    S07 |    S08 |    S09 |    S07 |
    | 00741 | S04 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    | 00746 | S01 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    | 00770 | S01 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    | 00771 | S01 |    S02 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario where combobox can have same string values. for exa combo
I shall attempt to explain the scenario. I have a gridview I wish to
We have a scenario where we wish to analyse the visitors to our web
I have the following scenario in a backbone view. I wish to set the
Is it ok to have two objects as delgates of each other..? My scenario
In my scenario, I have 100 nodes. Each time a random node out of
We have a scenario where we wish to plot a large number (possibly up
I have scenario where I need to host a web service (WCF) on Azure
I have scenario, I have two update panels on the page (both have update
I have scenario where I have to use the same XSD element for different

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.