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. Converting last N columns to rows in sql server

Converting last N columns to rows in sql server

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelptutorial
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.
  • S Offline
    S Offline
    sumit7034
    wrote on last edited by
    #1

    Hi My sql query result is

    Store month sale purchase
    A Jan 2000 150

    I want my result like

    Store month Expense value
    A Jan Sale 2000
    A Jan Purchase 150

    Please help me how to get this result using sql query. Thanks in advance

    M P J 3 Replies Last reply
    0
    • S sumit7034

      Hi My sql query result is

      Store month sale purchase
      A Jan 2000 150

      I want my result like

      Store month Expense value
      A Jan Sale 2000
      A Jan Purchase 150

      Please help me how to get this result using sql query. Thanks in advance

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

      Use 2 queries with a UNION between them.

      Never underestimate the power of human stupidity RAH

      S 1 Reply Last reply
      0
      • M Mycroft Holmes

        Use 2 queries with a UNION between them.

        Never underestimate the power of human stupidity RAH

        S Offline
        S Offline
        sumit7034
        wrote on last edited by
        #3

        Could you tell me the approach of using 2 queries with a UNION. Or tell me the syntex how to apply union to get the result. Thanks

        M 1 Reply Last reply
        0
        • S sumit7034

          Could you tell me the approach of using 2 queries with a UNION. Or tell me the syntex how to apply union to get the result. Thanks

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

          Seriously - you do not have BOL or google where you are!

          Select House, 'Sale', Sale as Value
          from Table
          union
          Select House, 'Purchase', Purchase as Value
          from Table

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • S sumit7034

            Hi My sql query result is

            Store month sale purchase
            A Jan 2000 150

            I want my result like

            Store month Expense value
            A Jan Sale 2000
            A Jan Purchase 150

            Please help me how to get this result using sql query. Thanks in advance

            P Offline
            P Offline
            Pravin Patil Mumbai
            wrote on last edited by
            #5

            You can use UNION operator for this. 1. http://www.w3schools.com/sql/sql_union.asp[^] 2. http://www.sql-tutorial.com/sql-union-sql-tutorial/[^] 3. http://www.1keydata.com/sql/sqlunion.html[^] Hope this helps. All the best.

            1 Reply Last reply
            0
            • S sumit7034

              Hi My sql query result is

              Store month sale purchase
              A Jan 2000 150

              I want my result like

              Store month Expense value
              A Jan Sale 2000
              A Jan Purchase 150

              Please help me how to get this result using sql query. Thanks in advance

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              You can use a union, as already suggested, and with SQL Server you can also use the UNPIVOT command.

              WITH myData(Store , Mth , Sale , Purchase )
              AS
              (
              SELECT 'A','Jan', 2000,150
              UNION SELECT 'B','Jan', 1000,200
              )
              SELECT Store, Mth, Expense, Value
              FROM
              (
              Select Store, Mth, Sale, Purchase
              FROM myData
              ) p
              UNPIVOT
              ( Value FOR Expense IN (Sale, Purchase) ) AS unpvt;

              Let me explain that a bit. The first bit (Starting 'WITH') just gives us some example data, I have added a second row onto your original data to help confirm the result. The second bit (Starting 'SELECT') Selects the data from an unpivoted view of your SALE and Purchase columns. The result of the above query is

              A Jan Sale 2000
              A Jan Purchase 150
              B Jan Sale 1000
              B Jan Purchase 200

              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