Jump to content
  • 0

Pulling Information from Field based on Calculated Value


Connonymous

Question

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

  • 0

In case anyone else is struggling with this same thing down the line, I was able to figure out how to accomplish this. The main sticking point for me was that I was trying to use the ORDER BY in the same query where I defined the total_score column. Once I realized I needed to nest these, I was able to get it working as expected:

SELECT full_name
FROM (
  SELECT full_name, total_score, ROW_NUMBER() OVER (ORDER BY total_score DESC) AS RowNum
  FROM (
    SELECT first + ' ' + last AS full_name, total_score
    FROM (
      SELECT first, last,
        (
          (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
      WHERE Employee_ID != target.[@field:Employee_ID]
    ) AS Subquery
  ) AS OrderedSubquery
) AS OuterQuery
WHERE RowNum = 1
 

Link to comment
Share on other sites

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.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...