How to get the column name of the result of a least function?

后端 未结 4 1025
北海茫月
北海茫月 2021-01-27 03:24

I have an employee table that looks like this:

| id | name | q1 | q2 | q3 | q4 |
+----+------+----+----+----+----+
| 1  | John | 20 | 30 | 10 | 4  |
| 2  | Ram           


        
4条回答
  •  無奈伤痛
    2021-01-27 04:02

    I would use a CASE statement to compare the greatest value against each column until a match is found

    Not perfect as it won't be fast and will also just find the first column when more than one share the same max value:-

    SELECT id, 
        name,
        LEAST(q1, q2, q3, q4) AS minValue,
        CASE LEAST(q1, q2, q3, q4)
            WHEN q1 THEN 'q1'
            WHEN q2 THEN 'q2'
            WHEN q3 THEN 'q3'
            ELSE 'q4'
        END,
        GREATEST(q1, q2, q3, q4) AS maxValue,
        CASE GREATEST(q1, q2, q3, q4)
            WHEN q1 THEN 'q1'
            WHEN q2 THEN 'q2'
            WHEN q3 THEN 'q3'
            ELSE 'q4'
        END
    FROM employee
    WHERE id = 4;
    

    EDIT - using a join, and I suspect this will be far worse:-

    SELECT a0.id, 
        a0.name,
        LEAST(a0.q1, a0.q2, a0.q3, a0.q4) AS minValue,
        CASE 
            WHEN a1.id IS NULL THEN 'q1'
            WHEN a2.id IS NULL THEN 'q2'
            WHEN a3.id IS NULL THEN 'q3'
            ELSE 'q4'
        END,
        GREATEST(a0.q1, a0.q2, a0.q3, a0.q4) AS maxValue,
        CASE GREATEST(q1, q2, q3, q4)
            WHEN a15.id IS NULL THEN 'q1'
            WHEN a16.id IS NULL THEN 'q2'
            WHEN a17.id IS NULL THEN 'q3'
            ELSE 'q4'
        END
    FROM employee a0
    LEFT OUTER JOIN employee a1 ON a0.id = a1.id AND LEAST(a0.q1, a0.q2, a0.q3, a0.q4) = a1.q1 
    LEFT OUTER JOIN employee a2 ON a0.id = a2.id AND LEAST(a0.q1, a0.q2, a0.q3, a0.q4) = a2.q2 
    LEFT OUTER JOIN employee a3 ON a0.id = a3.id AND LEAST(a0.q1, a0.q2, a0.q3, a0.q4) = a3.q3 
    LEFT OUTER JOIN employee a4 ON a0.id = a4.id AND LEAST(a0.q1, a0.q2, a0.q3, a0.q4) = a4.q4 
    LEFT OUTER JOIN employee a11 ON a0.id = a11.id AND GREATEST(a0.q1, a0.q2, a0.q3, a0.q4) = a1.q11 
    LEFT OUTER JOIN employee a12 ON a0.id = a12.id AND GREATEST(a0.q1, a0.q2, a0.q3, a0.q4) = a2.q12 
    LEFT OUTER JOIN employee a13 ON a0.id = a13.id AND GREATEST(a0.q1, a0.q2, a0.q3, a0.q4) = a3.q13 
    LEFT OUTER JOIN employee a14 ON a0.id = a14.id AND GREATEST(a0.q1, a0.q2, a0.q3, a0.q4) = a4.q14 
    WHERE a0.id = 4;
    

提交回复
热议问题