Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Transpose multiple rows into multiple columns in SQL Server

Transpose multiple rows into multiple columns in SQL Server

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Davidemazzuuu
    wrote on last edited by
    #1

    From: ObjectID - PropertyID - PropertyListValueID 1828 - 41 - 171 1828 - 41 - 170 1828 - 46 - 184 1828 - 47 - 189 To: ObjectID - 41 - 46 - 47 1828 - 170 - 184 - 189 1828 - 171 - 184 - 189 It's possible? Thanks

    C M 2 Replies Last reply
    0
    • D Davidemazzuuu

      From: ObjectID - PropertyID - PropertyListValueID 1828 - 41 - 171 1828 - 41 - 170 1828 - 46 - 184 1828 - 47 - 189 To: ObjectID - 41 - 46 - 47 1828 - 170 - 184 - 189 1828 - 171 - 184 - 189 It's possible? Thanks

      C Offline
      C Offline
      Corporal Agarn
      wrote on last edited by
      #2

      Which version of SQL Server? You could try PIVOT

      D 1 Reply Last reply
      0
      • D Davidemazzuuu

        From: ObjectID - PropertyID - PropertyListValueID 1828 - 41 - 171 1828 - 41 - 170 1828 - 46 - 184 1828 - 47 - 189 To: ObjectID - 41 - 46 - 47 1828 - 170 - 184 - 189 1828 - 171 - 184 - 189 It's possible? Thanks

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        I love the opportunity for self promotion, this article [^]will be of use!

        Never underestimate the power of human stupidity RAH

        D 1 Reply Last reply
        0
        • M Mycroft Holmes

          I love the opportunity for self promotion, this article [^]will be of use!

          Never underestimate the power of human stupidity RAH

          D Offline
          D Offline
          Davidemazzuuu
          wrote on last edited by
          #4

          With PIVOT i get: ObjectID - 41 - 46 - 47 -------------------------------------- 1828 - 171 - 184 - 189 or ObjectID - 41 - 46 - 47 -------------------------------------- 1828 - 171 - 184 - 189 1828 - 170 - NULL - NULL I must do: select ObjectID from table where [41]=171 and [46]=184 select ObjectID from table where [41]=170 and [46]=184

          M M 2 Replies Last reply
          0
          • C Corporal Agarn

            Which version of SQL Server? You could try PIVOT

            D Offline
            D Offline
            Davidemazzuuu
            wrote on last edited by
            #5

            Sql server 2012 pivot without aggregation

            1 Reply Last reply
            0
            • D Davidemazzuuu

              With PIVOT i get: ObjectID - 41 - 46 - 47 -------------------------------------- 1828 - 171 - 184 - 189 or ObjectID - 41 - 46 - 47 -------------------------------------- 1828 - 171 - 184 - 189 1828 - 170 - NULL - NULL I must do: select ObjectID from table where [41]=171 and [46]=184 select ObjectID from table where [41]=170 and [46]=184

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Then you need to prepare your data differently, 46 and 47 do not have values for 170 so you need to add them to the result set. It is a very weird structure you are asking for where missing values reflect the previous value.

              Never underestimate the power of human stupidity RAH

              D 1 Reply Last reply
              0
              • D Davidemazzuuu

                With PIVOT i get: ObjectID - 41 - 46 - 47 -------------------------------------- 1828 - 171 - 184 - 189 or ObjectID - 41 - 46 - 47 -------------------------------------- 1828 - 171 - 184 - 189 1828 - 170 - NULL - NULL I must do: select ObjectID from table where [41]=171 and [46]=184 select ObjectID from table where [41]=170 and [46]=184

                M Offline
                M Offline
                Member 10262330
                wrote on last edited by
                #7

                hi, use this table as example:

                COD surname name CODCAU from from_time to to_time period
                1 ROSSI MARCO 301 19/09/2005 0.00 23/09/2005 0.00 p1
                1 ROSSI MARCO 301 09/12/2005 0.00 09/12/2005 0.00 p2
                1 BIANCHI FABIO 301 12/01/2004 0.00 16/01/2004 0.00 p1
                1 BIANCHI FABIO 301 02/04/2004 0.00 02/04/2004 0.00 p2
                1 BIANCHI FABIO 301 02/05/2004 0.00 10/05/2004 0.00 p3

                then to pivot:

                select *
                FROM
                (
                --concatenate to create a period on unique column...
                SELECT surname,name,COD,from+' '+from_time+' '+to+' '+to_time as date,period
                FROM [pivot]
                ) as s
                PIVOT
                (
                --pivot aggregate by the columns different by 'date/period' -> (surname,name,COD)
                max(date)
                FOR period IN ([p1],[p2],[p3])
                )AS p

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  Then you need to prepare your data differently, 46 and 47 do not have values for 170 so you need to add them to the result set. It is a very weird structure you are asking for where missing values reflect the previous value.

                  Never underestimate the power of human stupidity RAH

                  D Offline
                  D Offline
                  Davidemazzuuu
                  wrote on last edited by
                  #8

                  I must filter/select every ObjectID with PropertyID=QueryString value and PropertyListValueID=QueryString value Initial structure: +----------+------------+---------------------+ | ObjectID | PropertyID | PropertyListValueID | +----------+------------+---------------------+ | 1828 | 41 | 171 | | 1828 | 41 | 170 | | 1828 | 46 | 184 | | 1828 | 47 | 189 | +----------+------------+---------------------+

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups