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 rows as columns

Transpose rows as columns

Scheduled Pinned Locked Moved Database
helpquestion
3 Posts 3 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.
  • U Offline
    U Offline
    User 4005525
    wrote on last edited by
    #1

    Hi I have a table with columns like Table1: 1.ID as int 2.TableID as int 3.ColumnName Varchar(50) 4.ColumnValue Varchar(50) Here TableID is not unique.I want to create a new table. Each ColumnName value of Table1 should become Column of New Table. Each ColumnValue should become the row value of corresponding column. Table1 values: ID TableID ColumnNAme ColumnValue 1 1 FirstNAme Javio 2 1 LastName Choprakhush 3 1 DateOfBirth 13/09/1986 4 1 Phone 6222226779 And i want to create a table like this FirstName LastNAme DateOfBirth Phone JAvio Choprashush 13/09/1986 6222226779 Can anyone Help ME? Thanks in advance sri

    A A 2 Replies Last reply
    0
    • U User 4005525

      Hi I have a table with columns like Table1: 1.ID as int 2.TableID as int 3.ColumnName Varchar(50) 4.ColumnValue Varchar(50) Here TableID is not unique.I want to create a new table. Each ColumnName value of Table1 should become Column of New Table. Each ColumnValue should become the row value of corresponding column. Table1 values: ID TableID ColumnNAme ColumnValue 1 1 FirstNAme Javio 2 1 LastName Choprakhush 3 1 DateOfBirth 13/09/1986 4 1 Phone 6222226779 And i want to create a table like this FirstName LastNAme DateOfBirth Phone JAvio Choprashush 13/09/1986 6222226779 Can anyone Help ME? Thanks in advance sri

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      This sould put you on the right track

      /* create test table and populate */
      create table UserArea(Country varchar(20))
      insert into UserAreaselect 'India'
      union all
      select 'USA'
      union all
      select 'India'
      union all
      select 'UK'
      /* now the actual code */
      DECLARE @SQL nvarchar(4000)
      SET @SQL=''
      SELECT @SQL= @SQL +'SUM(CASE WHEN Country=''' + a.Country + ''' THEN 1 ELSE 0 END) AS [' + a.Country + '],'
      FROM (select distinct Country from UserArea) as a
      select @SQL = left(@SQL,len(@SQL)-1)
      SET @SQL='SELECT ' + @SQL + ' FROM UserArea'EXEC(@SQL)
      /* tidy up */
      drop table UserArea

      Bob Ashfield Consultants Ltd

      1 Reply Last reply
      0
      • U User 4005525

        Hi I have a table with columns like Table1: 1.ID as int 2.TableID as int 3.ColumnName Varchar(50) 4.ColumnValue Varchar(50) Here TableID is not unique.I want to create a new table. Each ColumnName value of Table1 should become Column of New Table. Each ColumnValue should become the row value of corresponding column. Table1 values: ID TableID ColumnNAme ColumnValue 1 1 FirstNAme Javio 2 1 LastName Choprakhush 3 1 DateOfBirth 13/09/1986 4 1 Phone 6222226779 And i want to create a table like this FirstName LastNAme DateOfBirth Phone JAvio Choprashush 13/09/1986 6222226779 Can anyone Help ME? Thanks in advance sri

        A Offline
        A Offline
        A Wong
        wrote on last edited by
        #3

        You can try using PIVOT if this is SQL2005.

        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