Author |
Topic |
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2013-08-28 : 12:11:09
|
Hi friends,I have a two columns in table Customer in this wayCity NameHouston Johndallas JamesAustin JackI would like to reaarane the data wherehouston Dallas AustinJohn James Jackwhere the city names become column headers..Is there a way to do it?Thank you very much |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-28 : 12:21:24
|
you need to use pivotSELECT *FROM Table tPIVOT (MAX(Name) FOR City IN ([Houston],[Dallas],[Austin]))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-28 : 12:23:58
|
If you have more than one name in a given city, add something else to distinguish them - for example, a row number like this:SELECT *FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY city ORDER BY name) AS RN FROM Table) tPIVOT (MAX(Name) FOR City IN ([Houston],[Dallas],[Austin]))p |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2013-08-28 : 14:37:11
|
Thank You visakh16. I could get teh idea now.but the dynamic sql of mine has parameter CorpIDwhere its is EXEC dbo.dynamic_pivot 'SELECT City, nameFROM CustomerWHERE Convert(uniqueidentifier, @CorpID) = CorpID,'City','Name',Can you show me the proper syntax for the above query--eroor at @CorpID |
|
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2013-08-28 : 15:56:16
|
Thank you visakh 16 i solved my issue. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-29 : 03:36:24
|
quote: Originally posted by akpaga Thank you visakh 16 i solved my issue.
you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
kameswararao polireddy
Starting Member
19 Posts |
Posted - 2013-08-29 : 04:27:16
|
Hi,This is the sample data i had created while i am working with DYNAMIC PIVOT in one of my task.I think this may meet your requirement.You can replace Your MAIN TABLE with the #table used in this example.if object_id('tempdb..#Table') is not nulldrop table #Table CREATE TABLE #Table ( ID INT, ColumnName VARCHAR(250), Value VARCHAR(250))INSERT INTO #Table SELECT 1,'name','Peter'INSERT INTO #Table SELECT 1,'phone','12345678'INSERT INTO #Table SELECT 1,'email','peter@host.com'INSERT INTO #Table SELECT 2,'name','John' INSERT INTO #Table SELECT 2,'phone','87654321'INSERT INTO #Table SELECT 2,'email','john@host.com'INSERT INTO #Table SELECT 3,'name','Sarah'INSERT INTO #Table SELECT 3,'phone','55667788'INSERT INTO #Table SELECT 3,'email','sarah@host.com'select * from #Table---I assumed your tablename as TESTTABLE---DECLARE @cols NVARCHAR(2000)DECLARE @query NVARCHAR(4000)SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + t.ColumnName FROM #Table AS t --ORDER BY '],[' + t.ID FOR XML PATH('') ), 1, 2, '') + ']'SELECT @colsSET @query = N'SELECT ID,'+ @cols +' FROM(SELECT t1.ID,t1.ColumnName , t1.Value FROM #Table AS t1 where t1.id=1) pPIVOT (MAX([Value]) FOR ColumnName IN ( '+ @cols +' ))AS pvt;'EXECUTE(@query)P.Kameswara rao |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2013-08-29 : 06:39:38
|
Use PivotSELECT [Houston],[Dallas],[Austin]FROM (SELECT City,Name FROM TableName)tPIVOT(MAX([Name]) FOR City in([Houston],[Dallas],[Austin]))pveeranjaneyulu |
|
|
|