May 10, 2011 at 3:47 am
Hi All ,
I have a table with columns
ProjectID ,YearofOperation WeekNo,PorjectParameterSchedule, ProjectParameterScope,ProjectParameterResources,ProjectParameterQuality
1 2011 18 2 10 5 18
1 2011 19 6 8 9 45
1 2011 20 9 10 7 25
2 2011 18 2 89 7 12
2 2011 19 2 9 7 23
we will diplay one ProjectID at a time Eg; ProjectID =1
I need the data in the following format
[18] [19] [20]
PorjectParameterSchedule 2 6 9
ProjectParameterScope 10 8 10
ProjectParameterResources 5 9 7
ProjectParameterQuality 18 45 25
Thanks
Roshan
May 10, 2011 at 7:05 am
Please read the articles linked to in my signature:
For better assistance in answering your questions
How to ask a question
Cross Tabs / Pivot Tables, Part 1
Cross Tabs / Pivot Tables, Part 2
-- See how you start off by actually creating a table
-- and then inserting the data into it? Doing this really
-- makes things a lot easier for all the people you want to
-- help you. So, HELP US HELP YOU by doing this for us! See
-- http://www.sqlservercentral.com/articles/Best+Practices/61537/
-- for more details on how to do all of this.
IF OBJECT_ID('tempdb..#test','U') IS NOT NULL DROP TABLE #test;
CREATE TABLE #test (ProjectID INT,YearofOperation INT, WeekNo INT, PorjectParameterSchedule INT, ProjectParameterScope INT,ProjectParameterResources INT,ProjectParameterQuality int);
INSERT INTO #test
VALUES
(1, 2011, 18, 2, 10, 5, 18),
(1, 2011, 19, 6, 8, 9, 45),
(1, 2011, 20, 9, 10, 7, 25),
(2, 2011, 18, 2, 89, 7, 12),
(2, 2011, 19, 2, 9, 7, 23);
-- whew!!! now that the test data has been made,
-- let's see how to do what you're asking for!
SELECT ProjectId,
YearOfOperation,
Item = 'PorjectParameterSchedule',
[18] = MAX(CASE WHEN WeekNo = 18 THEN PorjectParameterSchedule ELSE NULL END),
[19] = MAX(CASE WHEN WeekNo = 19 THEN PorjectParameterSchedule ELSE NULL END),
[20] = MAX(CASE WHEN WeekNo = 20 THEN PorjectParameterSchedule ELSE NULL END)
FROM #test
GROUP BY ProjectID, YearofOperation
UNION ALL
SELECT ProjectId,
YearOfOperation,
Item = 'ProjectParameterScope',
[18] = MAX(CASE WHEN WeekNo = 18 THEN ProjectParameterScope ELSE NULL END),
[19] = MAX(CASE WHEN WeekNo = 19 THEN ProjectParameterScope ELSE NULL END),
[20] = MAX(CASE WHEN WeekNo = 20 THEN ProjectParameterScope ELSE NULL END)
FROM #test
GROUP BY ProjectID, YearofOperation
UNION ALL
SELECT ProjectId,
YearOfOperation,
Item = 'ProjectParameterResources',
[18] = MAX(CASE WHEN WeekNo = 18 THEN ProjectParameterResources ELSE NULL END),
[19] = MAX(CASE WHEN WeekNo = 19 THEN ProjectParameterResources ELSE NULL END),
[20] = MAX(CASE WHEN WeekNo = 20 THEN ProjectParameterResources ELSE NULL END)
FROM #test
GROUP BY ProjectID, YearofOperation
UNION ALL
SELECT ProjectId,
YearOfOperation,
Item = 'ProjectParameterQuality',
[18] = MAX(CASE WHEN WeekNo = 18 THEN ProjectParameterQuality ELSE NULL END),
[19] = MAX(CASE WHEN WeekNo = 19 THEN ProjectParameterQuality ELSE NULL END),
[20] = MAX(CASE WHEN WeekNo = 20 THEN ProjectParameterQuality ELSE NULL END)
FROM #test
GROUP BY ProjectID, YearofOperation
ORDER BY ProjectID, YearofOperation, item ;
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 15, 2011 at 5:26 pm
roshanmaugustine (5/10/2011)
Hi All ,I have a table with columns
ProjectID ,YearofOperation WeekNo,PorjectParameterSchedule, ProjectParameterScope,ProjectParameterResources,ProjectParameterQuality
1 2011 18 2 10 5 18
1 2011 19 6 8 9 45
1 2011 20 9 10 7 25
2 2011 18 2 89 7 12
2 2011 19 2 9 7 23
we will diplay one ProjectID at a time Eg; ProjectID =1
I need the data in the following format
[18] [19] [20]
PorjectParameterSchedule 2 6 9
ProjectParameterScope 10 8 10
ProjectParameterResources 5 9 7
ProjectParameterQuality 18 45 25
Thanks
Roshan
You need a "dynamic Cross -Tab" for this. Please see the following article to familiarize yourself with the process. If you still have problems with making this "non-hard-coded", post back.
http://www.sqlservercentral.com/articles/Crosstab/65048/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply