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

Need urgent help Expand / Collapse
Author
Message
Posted Friday, April 5, 2013 6:26 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 21, 2014 5:07 AM
Points: 25, Visits: 238
Hi All,

I have a table like this. but its have 5-6k rows.


1 AppName
2 AppVersion
3 Product
4 Date1
5 Date2
6 Date3
7 AppName
8 AppVersion
9 Product
10 Date1
11 Date2
12 Date3
13 AppName
14 AppVersion
15 Product
16 Date1
17 Date2
18 Date3
.
.
.
.
.

and I want result like this. the logic is that after each 6 records we have a complete row. you can also say that column name are associated with records.

AppName AppVersion Product Date1 Date2 Date3
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18

Thanks for your help.

Sneh


Post #1439172
Posted Friday, April 5, 2013 6:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, August 16, 2014 8:16 AM
Points: 107, Visits: 513
In keeping with the terseness of your post...

PIVOT

Post #1439176
Posted Friday, April 5, 2013 6:46 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:00 AM
Points: 2,433, Visits: 7,501
You need to have some way to order the original data.

Here's your data set up so others can use it: -
IF object_id('tempdb..#testEnvironment') IS NOT NULL
BEGIN
DROP TABLE #testEnvironment;
END;

SELECT id, col
INTO #testEnvironment
FROM (VALUES(1,'AppName'),(2,'AppVersion'),(3,'Product'),
(4,'Date1'),(5,'Date2'),(6,'Date3'),
(7,'AppName'),(8,'AppVersion'),(9,'Product'),
(10,'Date1'),(11,'Date2'),(12,'Date3'),
(13,'AppName'),(14,'AppVersion'),(15,'Product'),
(16,'Date1'),(17,'Date2'),(18,'Date3')
)a(id,col);

Here's how to get your expected results: -
SELECT 
MAX(CASE WHEN col = 'AppName' THEN id ELSE NULL END) AS AppName,
MAX(CASE WHEN col = 'AppVersion' THEN id ELSE NULL END) AS AppVersion,
MAX(CASE WHEN col = 'Product' THEN id ELSE NULL END) AS Product,
MAX(CASE WHEN col = 'Date1' THEN id ELSE NULL END) AS Date1,
MAX(CASE WHEN col = 'Date2' THEN id ELSE NULL END) AS Date2,
MAX(CASE WHEN col = 'Date3' THEN id ELSE NULL END) AS Date3
FROM #testEnvironment
GROUP BY (id-1)/6;

Note that because the "id" column is sequential, I've just grouped them into sixes by dividing by 6. You will need to provide more information about your data so that we can see how to achieve the same result for you - possibly a ROW_NUMBER ranking function?

The results that the above produces is: -
AppName     AppVersion  Product     Date1       Date2       Date3
----------- ----------- ----------- ----------- ----------- -----------
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1439178
Posted Friday, April 5, 2013 7:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 21, 2014 5:07 AM
Points: 25, Visits: 238
its working fine.

Thanks for your help.

Thanks
Sneh
Post #1439207
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse