Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to select an single rows to multiple columns Expand / Collapse
Author
Message
Posted Thursday, January 31, 2013 4:22 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, March 31, 2014 3:07 AM
Points: 99, Visits: 40
I am Ramanathan and working in SQL SERVER 2008….Please help me
Table1
Location--Field
A1
A2
A3
B1
B2
B3
How to change an below format from table1
A B--Field
A1 B1
A2 B2
A3 B3
Post #1413985
Posted Thursday, January 31, 2013 4:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, August 28, 2013 2:37 AM
Points: 5,075, Visits: 4,834
declare @tab table (col1 char(2))
insert into @tab values
('A1'),
('A2'),
('A3'),
('B1'),
('B2'),
('B3')

SELECT
A.Col1,
B.Col1
FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY LEFT(Col1,1) ORDER BY Col1) AS RowNum,
Col1
FROM
@tab
WHERE
LEFT(Col1,1) = 'A'
) AS A
FULL OUTER JOIN
(
SELECT
ROW_NUMBER() OVER(PARTITION BY LEFT(Col1,1) ORDER BY Col1) AS RowNum,
Col1
FROM
@tab
WHERE
LEFT(Col1,1) = 'B'
) AS B
ON
A.RowNum = B.RowNum





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1413993
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse