Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Display the record in column wise

Author  Topic 

rameshduraikannu
Starting Member

10 Posts

Posted - 2009-12-25 : 04:59:30
Hai friends

I want to display the data in column wise

the actual is look like this

quote:

location
-------
loc1
loc2
loc3
loc4
loc5
loc6



i want to display the like this way
quote:

location location location
------- ------- --------
loc1 loc2 loc3
loc4 loc5 loc6




want to display 3 column and 3 row.

it is possible to display the data like this way

rameshduraikannu

sql-programmers
Posting Yak Master

190 Posts

Posted - 2009-12-25 : 07:08:16
Hi,

Use need to use PIVOT table concept to display like this.

I have a blog article to maybe assist you with your question:

Blog - SQL Server Programmers - Using PIVOT and UNPIVOT
__________________
SQL Server Programmers and Consultants
http://www.sql-programmers.com/

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-12-26 : 00:22:45
go through this link
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-12-28 : 00:40:53
Hi Try this,

IF OBJECT_ID('tempdb..#T') IS NOT NULL
DROP TABLE #T
IF OBJECT_ID('tempdb..#TT') IS NOT NULL
DROP TABLE #TT

CREATE TABLE #T( ID INT )
INSERT INTO #T
SELECT number FROM Master..spt_Values WHERE Type = 'P' AND number BETWEEN 1 and 23
DECLARE @Cnt INT
SELECT @Cnt = (COUNT(*)/2) + (COUNT(*)%2) FROM #T
SELECT *, ROW_NUMBER() OVER (PARTITION BY CASE WHEN @Cnt < ID THEN 1 ELSE 2 END
ORDER BY ID) AS 'ROW'
, CASE WHEN @Cnt < ID THEN 2 ELSE 1 END AS 'seq'
INTO #TT
FROM #T

DECLARE @Sql VARCHAR(MAX)
DECLARE @Cols VARCHAR(MAX)

SELECT @Sql = '', @Cols = ''

SELECT @Cols = @Cols + ', MAX(CASE WHEN ROW = ' + cast(ROW as varchar(10)) + 'THEN ID END) AS "' + cast(ROW as varchar(10)) + '"'
FROM ( SELECT DISTINCT ROW FROM #TT ) A

print @Cols

SELECT @Sql = 'SELECT ' + STUFF(@Cols, 1, 1, '')
+ ' FROM #TT
GROUP BY seq '

EXEC(@Sql)

Go to Top of Page
   

- Advertisement -