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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:26:19+00:00 2026-06-03T13:26:19+00:00

I have raw data that looks as follows: I want to perform a T-SQL

  • 0

I have raw data that looks as follows:

enter image description here

I want to perform a T-SQL query to put the data in the following form:

enter image description here

It’s almost like an UnPivot type query, but I don’t know how to go about doing it. Also, one clarification. The value for the LastModifiedTime column can be any one of the five values for each SubGroupId. It wouldn’t matter exactly which one it was.

Thanks very much for your help.

  • 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-03T13:26:22+00:00Added an answer on June 3, 2026 at 1:26 pm

    There are two ways you can do this, either using a Static PIVOT where you hard code the values in that you need to PIVOT:

    create table temp
    (
        subgroupid int,
        coater varchar(10),
        metricTypeDescriptor varchar(5),
        subgroupvalue decimal(20, 6),
        valuedescription varchar(50),
        lastmodfiedtime datetime
    )
    
    insert into temp values(28099, 'VA_A', 'CdSim', 2.99886, 'Average', '2012-05-03 20:55:02.14')
    insert into temp values(28099, 'VA_A', 'CdSim', 2.57201, 'Minimum', '2012-05-03 20:55:02.14')
    insert into temp values(28099, 'VA_A', 'CdSim', 3.1646, 'Maximum', '2012-05-03 20:55:02.143')
    insert into temp values(28099, 'VA_A', 'CdSim', 0.22082, 'Standard Deviation', '2012-05-03 20:55:02.153')
    insert into temp values(28099, 'VA_A', 'CdSim', 0.59259, 'xWebRange', '2012-05-03 20:55:02.157')
    
    insert into temp values(28102, 'VA_A', 'CdSim', 2.9091, 'Average', '2012-05-03 21:00:01.27')
    insert into temp values(28102, 'VA_A', 'CdSim', 2.67619, 'Minimum', '2012-05-03 21:00:01.27')
    insert into temp values(28102, 'VA_A', 'CdSim', 3.17471, 'Maximum', '2012-05-03 21:00:01.27')
    insert into temp values(28102, 'VA_A', 'CdSim', 0.191868, 'Standard Deviation', '2012-05-03 21:00:01.27')
    insert into temp values(28102, 'VA_A', 'CdSim', 0.49852, 'xWebRange', '2012-05-03 21:00:01.27')
    
    insert into temp values(28104, 'VA_A', 'CdSim', 3.05835, 'Average', '2012-05-03 21:05:01.383')
    insert into temp values(28104, 'VA_A', 'CdSim', 2.80336, 'Minimum', '2012-05-03 21:05:01.387')
    insert into temp values(28104, 'VA_A', 'CdSim', 3.17943, 'Maximum', '2012-05-03 21:05:01.387')
    insert into temp values(28104, 'VA_A', 'CdSim', 0.158743, 'Standard Deviation', '2012-05-03 21:05:01.39')
    insert into temp values(28104, 'VA_A', 'CdSim', 0.37607, 'xWebRange', '2012-05-03 21:05:01.40')
    
    select subgroupid, coater, metrictypedescriptor
        , [average]
        , [maximum]
        , [minimum]
        , [standard deviation]
        , lastmodfiedtime
    from 
    (
        select t1.subgroupid, t1.coater, t1.metrictypedescriptor, t1.valuedescription, t1.subgroupvalue, t2.lastmodfiedtime
        from temp t1
        left join 
        (
            select subgroupid, max(lastmodfiedtime) as lastmodfiedtime
            from temp 
            group by subgroupid
        ) t2
            on t1.subgroupid = t2.subgroupid
    ) x
    pivot
    (
        max(subgroupvalue)
        for valuedescription in([Average], [Minimum], [Maximum], [Standard Deviation])
    ) p
    
    
    drop table temp
    

    Or you can do this via a dynamic Pivot where you create the list of columns on the fly and then PIVOT them:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX);
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.valuedescription) 
                FROM temp c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT subgroupid, coater, metrictypedescriptor, ' + @cols + ', lastmodfiedtime from 
                (
                    select t1.subgroupid, t1.coater, t1.metrictypedescriptor, t1.valuedescription, t1.subgroupvalue, t2.lastmodfiedtime
                    from temp t1
                    left join 
                    (
                        select subgroupid, max(lastmodfiedtime) as lastmodfiedtime
                        from temp 
                        group by subgroupid
                    ) t2
                        on t1.subgroupid = t2.subgroupid
                    ) x
                pivot
                (
                    max(subgroupvalue)
                    for valuedescription in(' + @cols + ')
                ) p '
    
    execute(@query)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a raw image file that is saved in binary data (no encoding).
I have a C program that mines a huge data source (20GB of raw
I have to parse an XML document that looks like this: <?xml version=1.0 encoding=UTF-8
I have a web service that returns a raw string of JSON data (sample
I have a class that looks roughly like this: template<std::size_t dim> class Foo {
I have a sheet full of some raw data, about 20,000 rows and 50
I have an array of raw sound data samples, and I'm trying to make
I need to write data into drive. I have two options: write raw sectors.(_write(handle,
You have 2000 raw images, 52x52 pixels, RGB 24 bits, that correspond to the
I'm looking to make an HTTP post request given the raw data that I

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.