如何获取找到MAX()值的行号或ID

时间:2019-06-09 12:41:20

标签: sql sql-server database tsql rdbms

美好的一天社区。 我很难找到一种方法来达到我想要的结果。由于对SQL查询不是很熟练,所以我开始失去理智。我想做的是在特定测试中找到最高和最低成绩,但是我也希望获得MAX()和MIN()所在的行的ID或行号(它们匹配)找到了。

“结果”表如下所示:

 ResultID|Test_UK|Test_US|TestUK_Scr|TestUS_Scr|TestTakenOn
   1         1       3      85          14     2018-11-22 00:00:00.000
   2         3       1      41          94     2018-11-23 00:00:00.000
   3         2       4      71          54     2018-11-24 00:00:00.000
   4         4       2      51          52     2018-12-25 00:00:00.000
   5         6       3      74          69     2018-12-01 00:00:00.000
   6         3       6      83          57     2018-12-02 00:00:00.000
   7         7       4      91          98     2018-12-03 00:00:00.000
   8         4       7      88          22     2018-12-04 00:00:00.000
   9         5       8      41          76     2018-12-08 00:00:00.000
   10        8       5      37          64     2018-12-09 00:00:00.000

运行查询时得到的结果...

TestID|TopScore|LowScore|LastDateTestTaken
   1      94       85    2018-11-23 00:00:00.000
   2      71       52    2018-11-25 00:00:00.000
   3      83       14    2018-12-02 00:00:00.000
   4      98       51    2018-12-04 00:00:00.000
   5      64       41    2018-12-09 00:00:00.000
   6      74       57    2018-12-02 00:00:00.000
   7      91       22    2018-12-04 00:00:00.000
   8      76       37    2018-12-09 00:00:00.000

这是我正在处理的查询。

此查询返回上述结果

WITH
-- Combine the results of UK and US tests
    Combined_Results_Both_Tests AS(
                select ResultID as resultID, Test_UK as TestID, Test_UK_Scr as TestScore, TestTakenOn as TestDate from Results
                union all 
                select ResultID as resultID, Test_US as TestID, Test_US_Scr as TestScore, TestTakenOn as TestDate from Results),
--Gets TOP and WORST results of the tests, LastDateTaken (Needs to add ResultID!)
    Get_Best_and_Worst_Results_And_LastTestDate AS(
                SELECT TestID ,max(TestScore) AS TopScore ,min(TestScore) AS LowScore ,max(TestDate) AS LastDateTestTaken
                FROM Combined_Results_Both_Tests
                GROUP BY TestID)
--Final query execution
SELECT * FROM Get_Best_and_Worst_Results_And_LastTestDate

我试图用类似的方法来达到我想要的结果,但是这种方法不起作用,而且效率也很低。我的意思是,每当在美国和英国的测试中找到匹配项时,它都充满了重复符号。

--Gets ReslutID of Min and Max values
    Get_ResultID_Of_Results AS(
                SELECT * FROM Get_Best_and_Worst_Results_And_LastTestDate A
                CROSS APPLY
                (SELECT ResultID FROM Results res
                WHERE   (A.TestID = res.Test_UK AND A.TopScore = res.Test_UK_Scr) OR
                        (A.TestID = res.Test_US AND A.TopScore = res.Test_UK_Scr) OR
                        (A.TestID = res.Test_UK AND A.LowScore = res.Test_UK_Scr) OR
                        (A.TestID = res.Test_US AND A.LowScore = res.Test_UK_Scr) OR
                        (A.TestID = res.Test_UK AND A.TopScore = res.Test_US_Scr) OR
                        (A.TestID = res.Test_US AND A.TopScore = res.Test_US_Scr) OR
                        (A.TestID = res.Test_UK AND A.LowScore = res.Test_US_Scr) OR
                        (A.TestID = res.Test_US AND A.LowScore = res.Test_US_Scr)) D)
SELECT * FROM Get_ResultID_Of_Results

这是我要达到的结果(表示在哪里找到最大值和最小值的额外列),该结果将说明Results表中的ResultID。另外,行号与表中的ResultID相匹配。

TestID|TopScore|LowScore|LastDateTestTaken    |MaxValueLocID|MinValueLocID|
   1      94       85    2018-11-23 00:00:00.000       2             1
   2      71       52    2018-11-25 00:00:00.000       3             4
   3      83       14    2018-12-02 00:00:00.000       6             1
   4      98       51    2018-12-04 00:00:00.000       7             4
   5      64       41    2018-12-09 00:00:00.000       10            9
   6      74       57    2018-12-02 00:00:00.000       5             6
   7      91       22    2018-12-04 00:00:00.000       7             8
   8      76       37    2018-12-09 00:00:00.000       9             10

寻求解决方案的任何帮助,无论是理论上的还是实际的。谢谢!

2 个答案:

答案 0 :(得分:1)

如果我正确遵循的话,您想取消数据透视并进行汇总:

app.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

然后您可以使用窗口功能对此进行修改:

select v.testid, max(v.score), min(v.score) max(v.TestTakenOn)
from results r cross apply
     (values (Test_UK, TestUK_Scr, TestTakenOn),
             (Test_US, TestUS_Scr, TestTakenOn)
     ) v(testid, score, TestTakenOn)
group by v.testid;

答案 1 :(得分:1)

with allScores (TestId, Score, TestTakenOn, valueLoc) as
(
  select [Test_UK], [TestUK_Scr],[TestTakenOn], ResultId from scores
  union all
  select [Test_US], [TestUS_Scr],[TestTakenOn], ResultId from scores
),
maxMin (TestId, MaxScore, MinScore, LastTestDate) as (
  select TestId, Max(score), Min(score), Max(TestTakenOn)  
  from allScores
  group by TestId
)
select mm.*, a1.valueLoc as MaxValueLoc, a2.ValueLoc as MinValueLoc
from maxMin mm
inner join allScores a1 
on mm.TestId = a1.TestId and mm.MaxScore = a1.score
inner join allScores a2 
on mm.TestId = a2.TestId and mm.MinScore = a2.score;

DBFiddle demo