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


Need urgent help


Need urgent help

Author
Message
snsingh
snsingh
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 279
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
lnardozi 61862
lnardozi 61862
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 615
In keeping with the terseness of your post...

PIVOT

w00t
Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3900 Visits: 8472
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



Forever 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


Craig Wilkinson - Software Engineer
LinkedIn
snsingh
snsingh
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 279
its working fine.

Thanks for your help.

Thanks
Sneh
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