查找每一行的最大列名和值

时间:2018-10-04 17:44:19

标签: sql sql-server sql-server-2012

我需要找到分数最低的问题。数据具有用于确定总分的是和否点的列。我不仅需要知道最低的分数,还要知道哪个问题编号的分数最低。分数全部存储在单个记录中。

解决此问题的最佳方法是什么?我尝试了一个PIVOT桌子,但是很乱。

以下是一些示例数据:

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 

这是我的分数查询:

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 

我需要从以上结果中识别出表中每一行哪个Question得分最低。

2 个答案:

答案 0 :(得分:1)

我不能真正跟踪您的查询或示例数据-因为我不太清楚“分数”是什么。但最简单的答案是apply。我可以推测像这样的东西:

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;

答案 1 :(得分:1)

这可能有点聪明,但可以完成工作。

我使用您的基本查询,使结果集更清晰,使排序更容易,然后应用SELECT TOP 1 WITH TIES技巧使每个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)

结果:

+----------+-------+----------+
| 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 |
+----------+-------+----------+