gpt4 book ai didi

sql - 查找每行的最大列名称和值

转载 作者:行者123 更新时间:2023-12-03 03:03:35 25 4
gpt4 key购买 nike

我需要找到得分最低的问题。数据包含"is"和“否”分列,用于计算总分。我不仅需要知道最低分,还需要知道哪个题号得分最低。分数全部存储在一条记录中。

解决这个问题的最佳方法是什么?我尝试了数据透视表,但结果很困惑。

这是一些示例数据:

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

我需要从上面的结果中确定表中每一行的哪个问题得分最低。

最佳答案

我无法真正理解您的查询或示例数据 - 因为我不太清楚“分数”是什么。但这个问题最简单的答案是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;

关于sql - 查找每行的最大列名称和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52652631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com