如何获得多列之间的2个最大值?

时间:2017-04-14 19:03:52

标签: sql oracle oracle12c

我正在试图弄清楚如何从5个字段获得2个最高值。我知道driver.find_elements_by_css_selector("div.result_wrap") 函数,但我不知道如何提取第二个最高值。

基本上,该表有5个greatest类型字段。在此示例中,最后两列是我想要的结果。

NUMBER

3 个答案:

答案 0 :(得分:4)

取消隐藏数据并使用select id ,max(case when rnum=1 then val end) as highest_1 ,max(case when rnum=2 then val end) as highest_2 from (select id,score,val,row_number() over(partition by id order by val desc) as rnum from (select * from t --replace this with your tablename unpivot (val for score in (score1,score2,score3,score4,score5)) p ) tbl ) tbl group by id 获取每个ID的前2个最高得分。

{{1}}

答案 1 :(得分:3)

如果我理解正确,你需要这个:

with open("file.txt") as f:
    a = f.readlines()
    f.seek(0)  # seek to the beginning of the file
    b = f.readline()

答案 2 :(得分:0)

这个解决方案避免了解开,只需要大量的剪切和粘贴以及修改:

SELECT 
   dt.*,
   CASE maxscore -- GREATEST on all but the highest value,
                 -- simplified to a "Valued Case" based on mathguy's comment
      WHEN score1 THEN Greatest(score2, score3, score4, score5)
      WHEN score2 THEN Greatest(score1, score3, score4, score5)
      WHEN score3 THEN Greatest(score1, score2, score4, score5)
      WHEN score4 THEN Greatest(score1, score2, score3, score5)
      ELSE Greatest(score1, score2, score3, score4)
   END
FROM
 (
   SELECT t.*,
      Greatest(Score1,Score2,Score3,Score4,Score5) AS maxscore
   FROM tab t
 ) dt

基于@ vkp& @ mathguy的评论没有派生表/内联视图:

SELECT 
   t.*,
   Greatest(Score1,Score2,Score3,Score4,Score5) as Highest1_value,
   CASE Greatest(Score1,Score2,Score3,Score4,Score5)-- GREATEST on all but the highest value
      WHEN score1 THEN Greatest(       score2,score3,score4,score5)
      WHEN score2 THEN Greatest(score1,       score3,score4,score5)
      WHEN score3 THEN Greatest(score1,score2,       score4,score5)
      WHEN score4 THEN Greatest(score1,score2,score3,       score5)
      ELSE             Greatest(       score1,score2,score3,score4)
   END as Highest2_value
FROM tab t