Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:24:46+00:00 2026-05-22T23:24:46+00:00

I have the testmax table as following : I J ———————- ———————- 1 2

  • 0

I have the testmax table as following :

I                      J                      
---------------------- ---------------------- 
1                      2                      
2                      4                      
3                      3       

Now, the problem is how can I find the I which has max J, by following I can find only what is the max J

SELECT MAX(j) 
  FROM testmax

but by following I get this error: ORA-00937: not a single-group group function:

SELECT i, MAX(j) 
  FROM testmax
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:24:47+00:00Added an answer on May 22, 2026 at 11:24 pm

    Note that your question is still somewhat ambiguous; what should be returned when there is more than one record with a maximum value for J. Will you return one record or more than one? My answer is only applicable if you want one record returned.

    And in that case, the query below, using FIRST/LAST aggregate function for i, is the most efficient query.

    A small test with your table:

    SQL> create table testmax (i,j)
      2  as
      3  select 1, 2 from dual union all
      4  select 2, 4 from dual union all
      5  select 3, 3 from dual
      6  /
    
    Table created.
    

    And the query using the LAST aggregate function (http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions076.htm#sthref1540):

    SQL> set autotrace on explain
    SQL> select max(i) keep (dense_rank last order by j) i
      2       , max(j)
      3    from testmax
      4  /
    
             I     MAX(J)
    ---------- ----------
             2          4
    
    1 row selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 44308443
    
    ------------------------------------------------------------------------------
    | Id  | Operation          | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |         |     1 |    26 |     3   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE    |         |     1 |    26 |            |          |
    |   2 |   TABLE ACCESS FULL| TESTMAX |     3 |    78 |     3   (0)| 00:00:01 |
    ------------------------------------------------------------------------------
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    

    Also with one table scan, but using an analytic function on all the rows, where a single aggregate will do just fine:

    SQL> select i,j
      2    from (
      3      select i, j, max(j) over () max_j
      4        from testmax
      5    )
      6    where j=max_j
      7  /
    
             I          J
    ---------- ----------
             2          4
    
    1 row selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1897951616
    
    -------------------------------------------------------------------------------
    | Id  | Operation           | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT    |         |     3 |   117 |     3   (0)| 00:00:01 |
    |*  1 |  VIEW               |         |     3 |   117 |     3   (0)| 00:00:01 |
    |   2 |   WINDOW BUFFER     |         |     3 |    78 |     3   (0)| 00:00:01 |
    |   3 |    TABLE ACCESS FULL| TESTMAX |     3 |    78 |     3   (0)| 00:00:01 |
    -------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("J"="MAX_J")
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    

    This one uses two table scans instead of one:

    SQL> SELECT i, j
      2    FROM testmax
      3   WHERE j = ( SELECT MAX(j) from testmax )
      4  /
    
             I          J
    ---------- ----------
             2          4
    
    1 row selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 3795151209
    
    -------------------------------------------------------------------------------
    | Id  | Operation           | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT    |         |     1 |    26 |     6   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL  | TESTMAX |     1 |    26 |     3   (0)| 00:00:01 |
    |   2 |   SORT AGGREGATE    |         |     1 |    13 |            |          |
    |   3 |    TABLE ACCESS FULL| TESTMAX |     3 |    39 |     3   (0)| 00:00:01 |
    -------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("J"= (SELECT MAX("J") FROM "TESTMAX" "TESTMAX"))
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    

    Regards,
    Rob.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have such a problem, hope you'll help me.. Can't find anywhere. Here is the
Have a bunch of classes which I need to do serialize and deserialize from/to
Have the following scenario. I have a few form, which essentially have a few
have been able to output images from BLOB, however I am now wanting to
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the
I have hashmap and its keys are like folder/1.txt,folder/2.txt,folder/3.txt and value has these text
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
have such zend query: $select = $this->_table ->select() ->where('title LIKE ?', '%'.$searchWord.'%') ->where('description LIKE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.