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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:30:50+00:00 2026-06-12T18:30:50+00:00

Possible Duplicate: SQL Server dynamic PIVOT query? I have a dataset that has the

  • 0

Possible Duplicate:
SQL Server dynamic PIVOT query?

I have a dataset that has the below structure.

CREATE TABLE #TempTable
  (
     Measure_ID  INT,
     measurement DECIMAL(18, 4)
  )

INSERT INTO #TempTable
VALUES
(1,2.3)
,(1,3.4)
,(1,3.3)
,(2,3)
,(2,2.3)
,(2,4.0)
,(2,4.5)

I need to produce output that will look like this.

1,2.3,3.4,3.3
2,3,2.3,4.0,4.5

Basically its a pivot on Measure_ID. Unfortunately, there can be an unlimited number of measure_id’s. So Pivot is out.

I’m hoping to avoid CURSORS, but will if that turns out to be the best approach.

  • 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-12T18:30:52+00:00Added an answer on June 12, 2026 at 6:30 pm

    If you have an unknown number of values, then you can use a PIVOT with dynamic SQL:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' 
                            + QUOTENAME('Measurement_' + cast(rn as varchar(10))) 
                        from temptable
                        cross apply
                        (
                          select row_number() over(partition by measure_id order by measurement) rn
                          from temptable
                        ) x
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT measure_id, ' + @cols + ' from 
                 (
                    select measure_id, measurement,
                      ''Measurement_''
                        + cast(row_number() over(partition by measure_id order by measurement) as varchar(10)) val
                    from temptable
                ) x
                pivot 
                (
                    max(measurement)
                    for val in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle With Demo

    If you have a known number of values, then you can hard-code the values, similar to this:

    SELECT measure_id, [Measurement_1], [Measurement_2], 
                   [Measurement_3], [Measurement_4]
    from 
    (
      select measure_id, measurement,
        'Measurement_'
         + cast(row_number() over(partition by measure_id order by measurement) as varchar(10)) val
      from temptable
    ) x
    pivot 
    (
       max(measurement)
       for val in ([Measurement_1], [Measurement_2], 
                   [Measurement_3], [Measurement_4])
    ) p 
    

    See SQL Fiddle With Demo

    Both queries will produce the same results:

    MEASURE_ID | MEASUREMENT_1 | MEASUREMENT_2 | MEASUREMENT_3 | MEASUREMENT_4
    ==========================================================================
    1          | 2.3           | 3.3           | 3.4           | (null)
    2          | 2.3           | 3             | 4             | 4.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: Row Offset in SQL Server I have a query which has a
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: delete duplicate records in SQL Server I have a table in which
Possible Duplicate: dynamic sql pivot in sql server How to alter this stored procedure
Possible Duplicate: SQL Comments on Create Table on SQL Server 2008 I just want
Possible Duplicate: Simulating group_concat MySQL function in MS SQL Server 2005? I have a
Possible Duplicate: Saving changes after table edit in SQL Server Management Studio I need

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.