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. pivoting more than one column in sql server

pivoting more than one column in sql server

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
6 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.
  • C Offline
    C Offline
    chencoh
    wrote on last edited by
    #1

    hi everyone, i have sql server db with a table which contains a field for a workplace and the number of people employed in a specific month and year separated by gender. it kinda looks like : workplace month year male female Microsoft feb 2011 5 1 Google feb 2011 6 2 Intel feb 2011 7 3 Microsoft march 2011 8 4 obviously with different actual data. is there a way to concatenate the year and month columns and pivoting the data so it'll look something like : workplace date male female date male female Microsoft feb 2011 5 1 march 2011 8 4 Google feb 2011 6 2 Intel feb 2011 7 3 sorry if that was a bit too much, I'll be happy to explain myself more clearly if needed.

    D M 2 Replies Last reply
    0
    • C chencoh

      hi everyone, i have sql server db with a table which contains a field for a workplace and the number of people employed in a specific month and year separated by gender. it kinda looks like : workplace month year male female Microsoft feb 2011 5 1 Google feb 2011 6 2 Intel feb 2011 7 3 Microsoft march 2011 8 4 obviously with different actual data. is there a way to concatenate the year and month columns and pivoting the data so it'll look something like : workplace date male female date male female Microsoft feb 2011 5 1 march 2011 8 4 Google feb 2011 6 2 Intel feb 2011 7 3 sorry if that was a bit too much, I'll be happy to explain myself more clearly if needed.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Select columnA + columnB from someTable

      This concatenates the column vakues. Have you tried using this in your pivot query?

      "Your code will never work, Luc's always will.", Richard MacCutchan[^]

      1 Reply Last reply
      0
      • C chencoh

        hi everyone, i have sql server db with a table which contains a field for a workplace and the number of people employed in a specific month and year separated by gender. it kinda looks like : workplace month year male female Microsoft feb 2011 5 1 Google feb 2011 6 2 Intel feb 2011 7 3 Microsoft march 2011 8 4 obviously with different actual data. is there a way to concatenate the year and month columns and pivoting the data so it'll look something like : workplace date male female date male female Microsoft feb 2011 5 1 march 2011 8 4 Google feb 2011 6 2 Intel feb 2011 7 3 sorry if that was a bit too much, I'll be happy to explain myself more clearly if needed.

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

        There are some excellent articles [^]around on that subject.

        Never underestimate the power of human stupidity RAH

        J C 2 Replies Last reply
        0
        • M Mycroft Holmes

          There are some excellent articles [^]around on that subject.

          Never underestimate the power of human stupidity RAH

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          Mycroft Holmes wrote:

          There are some excellent articles

          Shameless plug, excellent. :-D 5 for that. And it is a very useful article.

          List of common misconceptions

          1 Reply Last reply
          0
          • M Mycroft Holmes

            There are some excellent articles [^]around on that subject.

            Never underestimate the power of human stupidity RAH

            C Offline
            C Offline
            chencoh
            wrote on last edited by
            #5

            Hey, Sorry for the late response, I wasn't around a computer. While article you added really shed some light about the topic, I still have a small problem. the article showed how to concatenate to columns into one and then pivot the column, what I want to do is to pivot them both. So if i refer to the article, instead of having a column for "1996-F" and one for "1996-M" I want two columns, one for "M" and one for "F" and "1996" wrapping them. something like: 1996 M F 6 5

            M 1 Reply Last reply
            0
            • C chencoh

              Hey, Sorry for the late response, I wasn't around a computer. While article you added really shed some light about the topic, I still have a small problem. the article showed how to concatenate to columns into one and then pivot the column, what I want to do is to pivot them both. So if i refer to the article, instead of having a column for "1996-F" and one for "1996-M" I want two columns, one for "M" and one for "F" and "1996" wrapping them. something like: 1996 M F 6 5

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

              Now you are trying to put UI/presentation logic into the database operation. Adding the additional row with the year is not a database operation it needs to be done in your application.

              Never underestimate the power of human stupidity RAH

              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