I have the following calculation written out that returns the highest score from my data where certain conditions are true. This portion seems to be working properly in returning the highest score, but I am having trouble when trying to get the name of the person who has the high score to return instead of the score itself. I have fields for first name and last name ( [@field:first] & [@field:last] ), so I would be trying to return [@field:first] + ' ' + [@field:last] for the person who has the highest score. Eventually I would also like to build in the option to specify the names of the individuals with the second or third highest scores as well, but I have not been able to get that sorted yet either. Any help would be greatly appreciated!
The code that returns the score is listed below:
SELECT TOP 1 total_score
FROM (
SELECT (
(SELECT
CASE
WHEN Q1_Value = target.[@field:1] THEN Q1_Score
WHEN Q2_Value = target.[@field:1] THEN Q2_Score
WHEN Q3_Value = target.[@field:1] THEN Q3_Score
WHEN Q4_Value = target.[@field:1] THEN Q4_Score
WHEN Q5_Value = target.[@field:1] THEN Q5_Score
ELSE 0
END
) +
(SELECT
CASE
WHEN Q1_Value = target.[@field:2] THEN Q1_Score
WHEN Q2_Value = target.[@field:2] THEN Q2_Score
WHEN Q3_Value = target.[@field:2] THEN Q3_Score
WHEN Q4_Value = target.[@field:2] THEN Q4_Score
WHEN Q5_Value = target.[@field:2] THEN Q5_Score
ELSE 0
END
) +
(SELECT
CASE
WHEN Q1_Value = target.[@field:3] THEN Q1_Score
WHEN Q2_Value = target.[@field:3] THEN Q2_Score
WHEN Q3_Value = target.[@field:3] THEN Q3_Score
WHEN Q4_Value = target.[@field:3] THEN Q4_Score
WHEN Q5_Value = target.[@field:3] THEN Q5_Score
ELSE 0
END
)
) AS total_score
FROM My_Table_Name
) AS subquery
ORDER BY total_score DESC
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
Question
Connonymous
I have the following calculation written out that returns the highest score from my data where certain conditions are true. This portion seems to be working properly in returning the highest score, but I am having trouble when trying to get the name of the person who has the high score to return instead of the score itself. I have fields for first name and last name ( [@field:first] & [@field:last] ), so I would be trying to return [@field:first] + ' ' + [@field:last] for the person who has the highest score. Eventually I would also like to build in the option to specify the names of the individuals with the second or third highest scores as well, but I have not been able to get that sorted yet either. Any help would be greatly appreciated!
The code that returns the score is listed below:
SELECT TOP 1 total_score
FROM (
SELECT (
(SELECT
CASE
WHEN Q1_Value = target.[@field:1] THEN Q1_Score
WHEN Q2_Value = target.[@field:1] THEN Q2_Score
WHEN Q3_Value = target.[@field:1] THEN Q3_Score
WHEN Q4_Value = target.[@field:1] THEN Q4_Score
WHEN Q5_Value = target.[@field:1] THEN Q5_Score
ELSE 0
END
) +
(SELECT
CASE
WHEN Q1_Value = target.[@field:2] THEN Q1_Score
WHEN Q2_Value = target.[@field:2] THEN Q2_Score
WHEN Q3_Value = target.[@field:2] THEN Q3_Score
WHEN Q4_Value = target.[@field:2] THEN Q4_Score
WHEN Q5_Value = target.[@field:2] THEN Q5_Score
ELSE 0
END
) +
(SELECT
CASE
WHEN Q1_Value = target.[@field:3] THEN Q1_Score
WHEN Q2_Value = target.[@field:3] THEN Q2_Score
WHEN Q3_Value = target.[@field:3] THEN Q3_Score
WHEN Q4_Value = target.[@field:3] THEN Q4_Score
WHEN Q5_Value = target.[@field:3] THEN Q5_Score
ELSE 0
END
)
) AS total_score
FROM My_Table_Name
) AS subquery
ORDER BY total_score DESC
Link to comment
Share on other sites
1 answer to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.