简体   繁体   中英

Find max column name and value for each row

I need to find the question with the lowest score. The data has columns for Yes and No points which are used to calculate a total score. I not only need to know the lowest score but also which question number had the lowest score. The scores are all stored in a single record.

What's the best way to go about this? I tried a PIVOT table but that got messy.

Here's some sample data:

SELECT 1 AS Score_ID, 28.0 AS YesPtsGivenI, 2.0 AS NoPtsGivenI, 30.0 AS YesPtsGivenII, 0.0 AS NoPtsGivenII, 29.0 AS YesPtsGivenIII, 0.0 AS NoPtsGivenIII, 25.0 AS YesPtsGivenIV, 3.0 AS NoPtsGivenIV, 30.0 AS YesPtsGivenV, 0.0 AS NoPtsGivenV, 29.0 AS YesPtsGivenVI, 0.0 AS NoPtsGivenVI 
INTO #FS 
UNION 
SELECT 2 AS Score_ID, 27.0 AS YesPtsGivenI, 3.0 AS NoPtsGivenI, 29.0 AS YesPtsGivenII, 1.0 AS NoPtsGivenII, 28.0 AS YesPtsGivenIII, 0.0 AS NoPtsGivenIII, 28.0 AS YesPtsGivenIV, 0.0 AS NoPtsGivenIV, 30.0 AS YesPtsGivenV, 0.0 AS NoPtsGivenV, 29.0 AS YesPtsGivenVI, 0.0 AS NoPtsGivenVI  
UNION 
SELECT 3 AS Score_ID, 28.0 AS YesPtsGivenI, 2.0 AS NoPtsGivenI, 30.0 AS YesPtsGivenII, 0.0 AS NoPtsGivenII, 27.0 AS YesPtsGivenIII, 2.0 AS NoPtsGivenIII, 28.0 AS YesPtsGivenIV, 0.0 AS NoPtsGivenIV, 28.0 AS YesPtsGivenV, 2.0 AS NoPtsGivenV, 28.0 AS YesPtsGivenVI, 1.0 AS NoPtsGivenVI 
UNION 
SELECT 4 AS Score_ID, 30.0 AS YesPtsGivenI, 0.0 AS NoPtsGivenI, 29.0 AS YesPtsGivenII, 1.0 AS NoPtsGivenII, 29.0 AS YesPtsGivenIII, 0.0 AS NoPtsGivenIII, 28.0 AS YesPtsGivenIV, 0.0 AS NoPtsGivenIV, 30.0 AS YesPtsGivenV, 0.0 AS NoPtsGivenV, 28.0 AS YesPtsGivenVI, 1.0 AS NoPtsGivenVI 
UNION 
SELECT 5 AS Score_ID, 29.0 AS YesPtsGivenI, 1.0 AS NoPtsGivenI, 30.0 AS YesPtsGivenII, 0.0 AS NoPtsGivenII, 28.0 AS YesPtsGivenIII, 1.0 AS NoPtsGivenIII, 28.0 AS YesPtsGivenIV, 0.0 AS NoPtsGivenIV, 29.0 AS YesPtsGivenV, 1.0 AS NoPtsGivenV, 29.0 AS YesPtsGivenVI, 0.0 AS NoPtsGivenVI 
UNION 
SELECT 6 AS Score_ID, 30.0 AS YesPtsGivenI, 0.0 AS NoPtsGivenI, 28.0 AS YesPtsGivenII, 2.0 AS NoPtsGivenII, 29.0 AS YesPtsGivenIII, 0.0 AS NoPtsGivenIII, 27.0 AS YesPtsGivenIV, 1.0 AS NoPtsGivenIV, 30.0 AS YesPtsGivenV, 0.0 AS NoPtsGivenV, 29.0 AS YesPtsGivenVI, 0.0 AS NoPtsGivenVI 
UNION 
SELECT 7 AS Score_ID, 39.0 AS YesPtsGivenI, 0.0 AS NoPtsGivenI, 30.0 AS YesPtsGivenII, 0.0 AS NoPtsGivenII, 29.0 AS YesPtsGivenIII, 0.0 AS NoPtsGivenIII, 26.0 AS YesPtsGivenIV, 2.0 AS NoPtsGivenIV, 30.0 AS YesPtsGivenV, 0.0 AS NoPtsGivenV, 29.0 AS YesPtsGivenVI, 0.0 AS NoPtsGivenVI 

Here's my score query:

SELECT 
FS.YesPtsGivenI / (FS.YesPtsGivenI + FS.NoPtsGivenI) AS Q1, 
FS.YesPtsGivenII / (FS.YesPtsGivenII + FS.NoPtsGivenII) AS Q2, 
FS.YesPtsGivenIII / (FS.YesPtsGivenIII + FS.NoPtsGivenIII) AS Q3, 
FS.YesPtsGivenIV / (FS.YesPtsGivenIV + FS.NoPtsGivenIV) AS Q4, 
FS.YesPtsGivenV / (FS.YesPtsGivenV + FS.NoPtsGivenV) AS Q5, 
FS.YesPtsGivenVI / (FS.YesPtsGivenVI + FS.NoPtsGivenVI) AS Q6 
FROM #FS FS 

I need to identify from the above result which Question had the lowest score for each row in the table.

I can't really follow your query or sample data -- because I can't quite tell what the "score" is. But simplest answer to the question is apply . I can speculate something like:

select fs.*, v.*
from #fs fs cross apply
     (select top (1) val, which
      from (values (FS.YesPtsGivenI / (FS.YesPtsGivenI + FS.NoPtsGivenI), 'Q1'), 
                   (FS.YesPtsGivenII / (FS.YesPtsGivenII + FS.NoPtsGivenII), 'Q2'), 
                   (FS.YesPtsGivenIII / (FS.YesPtsGivenIII + FS.NoPtsGivenIII), 'Q3'), 
                   (FS.YesPtsGivenIV / (FS.YesPtsGivenIV + FS.NoPtsGivenIV), 'Q4'), 
                   (FS.YesPtsGivenV / (FS.YesPtsGivenV + FS.NoPtsGivenV), 'Q5'), 
                   (FS.YesPtsGivenVI / (FS.YesPtsGivenVI + FS.NoPtsGivenVI), 'Q6')
           ) v(val, which)
       order by val desc
      ) v;

This might be a little too clever by half, but it gets the job done.

Using your base query, I unpivoted the result set to make sorting easier, then applied the SELECT TOP 1 WITH TIES trick to get the lowest score by each Score_ID .

SELECT TOP 1 WITH TIES
    Score_ID,
    QName,
    QScore
FROM
    (
        SELECT Score_ID,
        FS.YesPtsGivenI / (FS.YesPtsGivenI + FS.NoPtsGivenI) AS Q1, 
        FS.YesPtsGivenII / (FS.YesPtsGivenII + FS.NoPtsGivenII) AS Q2, 
        FS.YesPtsGivenIII / (FS.YesPtsGivenIII + FS.NoPtsGivenIII) AS Q3, 
        FS.YesPtsGivenIV / (FS.YesPtsGivenIV + FS.NoPtsGivenIV) AS Q4, 
        FS.YesPtsGivenV / (FS.YesPtsGivenV + FS.NoPtsGivenV) AS Q5, 
        FS.YesPtsGivenVI / (FS.YesPtsGivenVI + FS.NoPtsGivenVI) AS Q6 
        FROM #FS FS 
    ) AS q
UNPIVOT
    (
        QScore
        FOR QName IN (Q1, Q2, Q3, Q4, Q5, Q6)
    ) unp
ORDER BY RANK() OVER (PARTITION BY Score_ID ORDER BY QScore ASC)

Results:

+----------+-------+----------+
| Score_ID | QName |  QScore  |
+----------+-------+----------+
|        1 | Q4    | 0.892857 |
|        2 | Q1    | 0.900000 |
|        3 | Q3    | 0.931034 |
|        4 | Q6    | 0.965517 |
|        5 | Q3    | 0.965517 |
|        6 | Q2    | 0.933333 |
|        7 | Q4    | 0.928571 |
+----------+-------+----------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM