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

  • Home
  • SEARCH
  • 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 8951067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:34:39+00:00 2026-06-15T13:34:39+00:00

Is there a T-SQL (SQL Server 2008R2) query to transform TABLE_1 into the expected

  • 0

Is there a T-SQL (SQL Server 2008R2) query to transform TABLE_1 into the expected resultset?

TABLE_1

+----------+-------------------------+---------+------+
| IdDevice | Timestamp               | M300    | M400 |
+----------+-------------------------+---------+------+
| 3        | 2012-12-05 16:29:51.000 | 2357,69 | 520  |
| 6        | 2012-12-05 16:29:51.000 | 1694,81 | 470  |
| 1        | 2012-12-05 16:29:51.000 | 2046,33 | 111  |
+----------+-------------------------+---------+------+

Expected resultset

+-------------------------+---------+--------+---------+--------+---------+--------+
|        Timestamp        | 3_M300  | 3_M400 | 6_M300  | 6_M400 | 6_M300  | 6_M400 |
+-------------------------+---------+--------+---------+--------+---------+--------+
| 2012-12-05 16:29:51.000 | 2357,69 |    520 | 1694,81 |    470 | 2046,33 |    111 |
+-------------------------+---------+--------+---------+--------+---------+--------+
  • 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-15T13:34:40+00:00Added an answer on June 15, 2026 at 1:34 pm

    This is still a PIVOT query but before you PIVOT you must perform an UNPIVOT of your columns.

    First, you perform the UNPIVOT which takes your current multiple columns and transforms them into two columns – one with the value and the other with the column name. The key for the UNPIVOT is that the datatypes be the same, so in the subquery I cast any columns to the same datatype:

    select timestamp,
        value, cast(iddevice as varchar(10)) + '_'+col as col
      from
      (
        select iddevice,
          timestamp,
          cast(m300 as varchar(10)) m300,
          cast(m400 as varchar(10)) m400
        from yourtable
      ) src
      unpivot
      (
        value
        for col in (m300, m400)
      ) unpiv
    

    See SQL Fiddle with Demo

    Result:

    |                       TIMESTAMP |   VALUE |    COL |
    ------------------------------------------------------
    | December, 05 2012 16:29:51+0000 | 2357,69 | 3_m300 |
    | December, 05 2012 16:29:51+0000 |     520 | 3_m400 |
    | December, 05 2012 16:29:51+0000 | 1694,81 | 6_m300 |
    | December, 05 2012 16:29:51+0000 |     470 | 6_m400 |
    | December, 05 2012 16:29:51+0000 | 2046,33 | 1_m300 |
    | December, 05 2012 16:29:51+0000 |     111 | 1_m400 |
    

    Once you complete the unpivot, then you can apply the PIVOT function:

    select *
    from
    (
      select timestamp,
        value, cast(iddevice as varchar(10)) + '_'+col as col
      from
      (
        select iddevice,
          timestamp,
          cast(m300 as varchar(10)) m300,
          cast(m400 as varchar(10)) m400
        from yourtable
      ) src
      unpivot
      (
        value
        for col in (m300, m400)
      ) unpiv
    ) src1
    pivot
    (
      max(value)
      for col in ([3_m300], [3_m400],
                  [6_m300], [6_m400],
                  [1_m300], [1_m400])
    ) piv
    

    See SQL Fiddle with Demo

    Results:

    |                       TIMESTAMP |  3_M300 | 3_M400 |  6_M300 | 6_M400 |  1_M300 | 1_M400 |
    --------------------------------------------------------------------------------------------
    | December, 05 2012 16:29:51+0000 | 2357,69 |    520 | 1694,81 |    470 | 2046,33 |    111 |
    

    If you have an unknown number of IdDevices that you want to transform into columns, then you can use dynamic SQL:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT DISTINCT ',' 
                          + quotename(cast(t.IdDevice as varchar(10)) +'_'
                                      +c.name)
                        from yourtable t
                         cross apply sys.columns as C
                       where C.object_id = object_id('yourtable') and
                             C.name not in ('IdDevice', 'Timestamp')
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    set @query = 'SELECT timestamp,' + @cols + ' from 
                 (
                   select timestamp,
                    value, cast(iddevice as varchar(10)) + ''_''+col as col
                  from
                  (
                    select iddevice,
                      timestamp,
                      cast(m300 as varchar(10)) m300,
                      cast(m400 as varchar(10)) m400
                    from yourtable
                  ) src
                  unpivot
                  (
                    value
                    for col in (m300, m400)
                  ) unpiv
                ) x
                pivot
                (
                  max(value)
                  for col in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Edit, if you need a totals field for each m value, then you can use:

    select timestamp,
      [3_m300], [3_m400],
      [6_m300], [6_m400],
      [1_m300], [1_m400],
      [1_m300] + [3_m300] + [6_m300] Total_m300,
      [1_m400] + [3_m400] + [6_m400] Total_m400
    from
    (
      select timestamp,
        value, cast(iddevice as varchar(10)) + '_'+col as col
      from
      (
        select iddevice,
          timestamp,
          m300,
          m400
        from yourtable
      ) src
      unpivot
      (
        value
        for col in (m300, m400)
      ) unpiv
    ) src1
    pivot
    (
      sum(value)
      for col in ([3_m300], [3_m400],
                  [6_m300], [6_m400],
                  [1_m300], [1_m400])
    ) piv
    

    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

Is there a way to force SQL Server 2008R2 to create a stored procedure
I'm trying to run a query from ADO.NET using SQL Server 2008R2. I'm using
Using SQL Server 2008R2, I have here is a simple query: SELECT * FROM
I am using SQL Server 2008R2. I've got the following setup: -- Query #1
Is there MS SQL Server function that counts the number of times a particular
In SQL Server there is a way to join tables from multiple sql servers
In sql-server there is function to auto-increment guid fields. CREATEGUID() how can I do
Is there any API SQL Server 2008 offers for .net application to create and
In connection string to SQL Server there is max pool size option. My question
IN SQL Server 2008 is there a way to generate a report as to

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.