Hello Everyone,
I need help writing this query. I have a table named "PMP" that stores up to 10 objective ID's:
Code Snippet
SELECT ObjectiveID1,ObjectiveID2,ObjectiveID3,ObjectiveID4,... ObjectiveID10
FROM PMP
WHERE PMPID = @.PMPID
I have a child table named "METRICS" that stores the details for each objective:
Code Snippet
SELECT ObjectiveID, TitleText, Description, KPIvalue
FROM METRICS
WHERE ObjectiveID IN (query the PMP table)
I need to write a query that will return all the Metrics that are used for a given PMPID. Any ideas how this can be done?
Thank You
My first suggestion is to correct a bad design mistake. You have a severly de-normalized table.
You most likely 'should' NOT have columns Objective1-Objective10.
You most likely would benefit from having a separate table for Objectives.
THEN issues such as this one would become MUCH easier to solve.
If you are using SQL 2005, you may find the UNPIVOT statement to be useful.
|||Which version of SQL Server are you using?
AMB
|||I am using SQL Server 2005 Express Edition
I understand why you would think this table is de-normalized, but it's the right structure for the solution. My organization allows a maximum of 10 objectives to be identified per request. I need to send my crystal report 1 record.
Can someone help me with the UNPIVOT query?
Thank You
|||I figured it out:
SELECT * FROM COACHING.METRICS
WHERE OBJECTIVEID IN (
SELECT OBJECTIVEID
FROM
(SELECT RESULT1_OBJECTIVEID,
RESULT2_OBJECTIVEID,
RESULT3_OBJECTIVEID,
RESULT4_OBJECTIVEID,
RESULT5_OBJECTIVEID,
RESULT6_OBJECTIVEID,
RESULT7_OBJECTIVEID,
RESULT8_OBJECTIVEID,
RESULT9_OBJECTIVEID,
RESULT10_OBJECTIVEID
FROM COACHING.PMPFORMS
WHERE PMPID = 271 ) OBJ
UNPIVOT
(OBJECTIVEID FOR PMPID IN (RESULT1_OBJECTIVEID,
RESULT2_OBJECTIVEID,
RESULT3_OBJECTIVEID,
RESULT4_OBJECTIVEID,
RESULT5_OBJECTIVEID,
RESULT6_OBJECTIVEID,
RESULT7_OBJECTIVEID,
RESULT8_OBJECTIVEID,
RESULT9_OBJECTIVEID,
RESULT10_OBJECTIVEID)) VALS)
|||
No comments:
Post a Comment