Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
I need a query in sq l server which converts rows into columns dynamically..
 

please provide a solution
Posted 15-Sep-12 0:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This operation is called matrix transposition in algebra. Since you specified only SQL-Server in the tags, I looked on google T-SQL solutions, but there are many approaches:
- Generic approach: http://www.extensionmethod.net/Details.aspx?ID=152[^]
- ASP.NET sample: Transpose a DataTable using C#[^], http://codemaverick.blogspot.hu/2008/02/transpose-datagrid-or-gridview-by.html[^]
- T-SQL: http://sqlandme.com/2011/04/20/tsql-transpose-data-using-using-pivot-and-unpivot/[^]
  Permalink  
Comments
vithal wadje at 17-Sep-12 3:27am
   
thanks u
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
You can achieve this by using Pivot. See the below example. You might be get the idea.
 
http://www.simple-talk.com/blogs/2007/09/14/pivots-with-dynamic-columns-in-sql-server-2005/[^]
 
Thanks,
Viprat
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

after lots of querying i got query that i want this query displays the output in cross tab format dynamically
 
consider i have table named SalesRepor having fields Month,Branch,Sales
 
 
declare @columns varchar(max)
declare @convert varchar(max)
select   @columns = stuff (( select distinct'],[' +  Month
                    from SalesReport
                  
                    for xml path('')), 1, 2, '') + ']'
 

set @convert =
'select * from (select Month,Branch,Sales from SalesReport) SalesRpt
    pivot(sum(Sales) for Month
    in ('+@columns+')) as pivottable'
 

execute (@convert)
 

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150326.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100