'How to get the minimum of one column and get another column without grouping?

I'm trying to get a record from a table that has the minimum value of a specific column, but I want the rest of the data in that record without grouping one of the columns.

This is sample data:

sample date

I want a query that will return:

January, 24, 0, 3
February, 12, 0, 1



Solution 1:[1]

@Littlefoot gave probably the only reasonable answer. However, if your table is very large and performance is an issue, this version might be more efficient:

select    month, 
          day, 
          min(c_initial) keep ( dense_rank first order by c_initial asc ) c_initial,
          min(ending) keep ( dense_rank first order by c_initial asc ) ending
from      your_table
group by  month, day; 

The min() keep tells Oracle you just want the value for which ever row in each group has the lowest value for c_initial. So, it can discard the values it knows you're not interested in, rather than keeping them around, sorting them, and then giving you the first one.

Incidentally, what tells Oracle to return the data for the row having this lowest c_initial value is the dense_rank first order by c_initial asc clause and NOT the min() function. Using a max() function would give the exact same answer. I use min() is just a style thing -- it reminds me that I'm looking for the first row in the group.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthew McPeak