SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Rows Value into Columns


Rows Value into Columns

Author
Message
patelmohamad
patelmohamad
SSC Veteran
SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)SSC Veteran (299 reputation)

Group: General Forum Members
Points: 299 Visits: 765
hi Guys,

Need your help to sort out the Query, i have fix columns Field1 to Field10, and Variant Rows may be 1 ,5 ,10 , 20 etc...
i want the RowIndex as ColumnName
eg [1] ,[2] ,[3] ,[4] etc, and Respective Row and Gate1_Name(Column data) ,Gate2_Name ,Gate3_Name in RowIndex([1]) ,RowIndex([2]) ,RowIndex([3]) Column ,


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

SELECT
IDENTITY(BIGINT ,1 ,1) AS RowIndex
,CONVERT(VARCHAR(22) ,GETDATE() ,114) AS Gate1_Name
,CONVERT(VARCHAR(22) ,GETDATE() ,114) AS Gate2_Name
,CONVERT(VARCHAR(22) ,GETDATE() ,114) AS Gate3_Name
,CONVERT(VARCHAR(22) ,GETDATE() ,114) AS Gate4_Name
,CONVERT(VARCHAR(22) ,GETDATE() ,114) AS Gate5_Name
INTO #RMC_BDCBPW_ReturnTable

TRUNCATE TABLE #RMC_BDCBPW_ReturnTable

INSERT INTO #RMC_BDCBPW_ReturnTable(Gate1_Name ,Gate2_Name ,Gate3_Name ,Gate4_Name ,Gate5_Name)
SELECT
CONVERT( VARCHAR(20) ,GETDATE() + 1 ,121) AS Gate1_Name
,CONVERT( VARCHAR(20) ,GETDATE() + 2 ,121) AS Gate2_Name
,CONVERT( VARCHAR(20) ,GETDATE() + 3 ,121) AS Gate3_Name
,CONVERT( VARCHAR(20) ,GETDATE() + 4 ,121) AS Gate4_Name
,CONVERT( VARCHAR(20) ,GETDATE() + 5 ,121) AS Gate5_Name
WAITFOR DELAY '00:00:00.700'
GO 10
-- Execute the Query for Ten times

SELECT * FROM #RMC_BDCBPW_ReturnTable
SELECT
Tab1.FieldName AS [1]
,Tab2.FieldName AS [2]
,Tab3.FieldName AS [3]
FROM
(SELECT 1 AS RowNo ,Gate1_Name AS FieldName FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 1
UNION SELECT 2 AS RowNo ,Gate2_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 1
UNION SELECT 3 AS RowNo ,Gate3_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 1
UNION SELECT 4 AS RowNo ,Gate4_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 1
UNION SELECT 5 AS RowNo ,Gate5_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 1
) AS Tab1
INNER JOIN
(SELECT 1 AS RowNo ,Gate1_Name AS FieldName FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 2
UNION SELECT 2 AS RowNo ,Gate2_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 2
UNION SELECT 3 AS RowNo ,Gate3_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 2
UNION SELECT 4 AS RowNo ,Gate4_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 2
UNION SELECT 5 AS RowNo ,Gate5_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 2
) AS Tab2
ON Tab2.RowNo = Tab1.RowNo
INNER JOIN
(SELECT 1 AS RowNo ,Gate1_Name AS FieldName FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 3
UNION SELECT 2 AS RowNo ,Gate2_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 3
UNION SELECT 3 AS RowNo ,Gate3_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 3
UNION SELECT 4 AS RowNo ,Gate4_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 3
UNION SELECT 5 AS RowNo ,Gate5_Name FROM #RMC_BDCBPW_ReturnTable WITH (NOLOCK) WHERE RowIndex = 3
) AS Tab3
ON Tab3.RowNo = Tab1.RowNo
--Etc....




Output i need described in Attachment(s)

Thanks

Patel Mohamad
Attachments
Acutal Data.PNG (33 views, 21.00 KB)
Required Data.PNG (34 views, 20.00 KB)
MMartin1
MMartin1
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7039 Visits: 2033
Hi. Have you tried the pivot function?

----------------------------------------------------
How to post forum questions to get the best help
pietlinden
pietlinden
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13630 Visits: 14113
You could also do this in Reporting Services with a Matrix...
Mansfield
Mansfield
Say Hey Kid
Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)

Group: General Forum Members
Points: 680 Visits: 394
This is a classic cross-tab or pivot query.

The hardcoded way:
http://www.sqlservercentral.com/articles/T-SQL/63681/

The dynamic way:
http://www.sqlservercentral.com/articles/Crosstab/65048/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search