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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:02:26+00:00 2026-06-14T04:02:26+00:00

I have table: ID Note 1 1 aaa 2 1 bbb 3 1 ccc

  • 0

I have table:

    ID   Note
1   1    aaa
2   1    bbb
3   1    ccc
4   2    ddd
5   2    eee
6   2    fff

I need to return it as:

   ID   Note1   Note2   Note3
1  1    aaa     bbb     ccc
2  2    ddd     eee     fff

Thank you!

  • 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-14T04:02:27+00:00Added an answer on June 14, 2026 at 4:02 am

    You can use the PIVOT function for this type of query. If you have a known number of columns, then you can hard-code the values:

    select *
    from
    (
      select id, note,
        'Note' + 
           cast(row_number() over(partition by id order by id) as varchar(10)) col
      from yourtable
    ) x
    pivot
    (
      max(note)
      for  col in ([Note1], [Note2], [Note3])
    ) p
    

    See SQL Fiddle with Demo

    If you are going to have an unknown number of notes that you want to turn into columns, then you can use dynamic sql:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' 
                          + QUOTENAME('Note' + 
                           cast(row_number() over(partition by id order by id) as varchar(10))) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT id,' + @cols + ' from 
                 (
                   select id, note,
                    ''Note'' + 
                       cast(row_number() over(partition by id order by id) as varchar(10)) col
                  from yourtable
                ) x
                pivot 
                (
                    max(note)
                    for col in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both will produce the same results.

    | ID | NOTE1 | NOTE2 | NOTE3 |
    ------------------------------
    |  1 |   aaa |   bbb |   ccc |
    |  2 |   ddd |   eee |   fff |
    

    Or if you do not want to use the PIVOT function, then you can use an aggregate function with a CASE statement:

    select id,
      max(case when rn = 1 then note else '' end) Note1,
      max(case when rn = 2 then note else '' end) Note2,
      max(case when rn = 3 then note else '' end) Note3
    from
    (
      select id, note,
        row_number() over(partition by id order by id) rn
      from yourtable
    ) src
    group by id
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have created a new table including a column note. The default is varchar(255)
Let say that I have table TableA(ColumnCode, ColumnA2, ColumnA3, ColumnA4, ColumnA5). Note: ColumnCode(PrimaryKey)is not
I have a table like this : (note that id_pack is not auto incremented)
I have a table called 'notes', on this table I need to track who
I have two table Images and Note both is having Noteid column so how
I have a data model like this in SQL Server: table Note - varchar
I have a table of purchase list with fields: ItemName, Quantity, UnitPrice, Amount. Note
I have 2 tables, user & country NOTE: table name is singular ADOdb_Active_Record::TableKeyBelongsTo( 'user',
I have a legacy database with the following table (note: no primary key) It
I have table with USER VARCHAR(100) CHARACTER SET WIN1250, NOTE BLOB SUB_TYPE 1 CHARACTER

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.