如何获得具有最大术语代码的行

时间:2014-12-02 21:36:48

标签: sql oracle

我需要能够获得具有最大/最大/最大TERM_CODE_EFF的行 我需要更改类似于以下查询的内容:

SELECT MAJR_CODE_MINR_1 AS Minor1,
       MAJR_CODE_MINR_1_2 AS Minor2,
       MAJR_CODE_MINR_2 AS Minor3,
       MAJR_CODE_MINR_2_2 AS Minor4
FROM SGBSTDN
WHERE SGBSTDN_PIDM = '367021'

这样的事情:

SELECT MAX (TERM_CODE_EFF) AS TermCode,
       MAJR_CODE_MINR_1 AS Minor1,
       MAJR_CODE_MINR_1_2 AS Minor2,
       MAJR_CODE_MINR_2 AS Minor3,
       MAJR_CODE_MINR_2_2 AS Minor4
FROM SGBSTDN
WHERE SGBSTDN_PIDM = '367021'

这给了我一个ORA-00937:不是单组组功能。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

SELECT MAX (TERM_CODE_EFF) AS TermCode,
       MAJR_CODE_MINR_1 AS Minor1,
       MAJR_CODE_MINR_1_2 AS Minor2,
       MAJR_CODE_MINR_2 AS Minor3,
       MAJR_CODE_MINR_2_2 AS Minor4
FROM SGBSTDN
WHERE SGBSTDN_PIDM = '367021'
group by MAJR_CODE_MINR_1 ,MAJR_CODE_MINR_1_2 ,MAJR_CODE_MINR_2 ,MAJR_CODE_MINR_2_2 

请在发布问题之前做一些研究。对于这些类型的错误,网上有丰富的解决方案。

答案 1 :(得分:0)

使用order byrownum

SELECT *
FROM (SELECT TERM_CODE_EFF,
             MAJR_CODE_MINR_1 AS Minor1,
             MAJR_CODE_MINR_1_2 AS Minor2,
             MAJR_CODE_MINR_2 AS Minor3,
             MAJR_CODE_MINR_2_2 AS Minor4
      FROM SGBSTDN
      WHERE SGBSTDN_PIDM = '367021'
      ORDER BY TERM_CODE_EFF DESC
     ) t
WHERE rownum = 1;