-1

I am trying to return all columns for a row that has the minimum value for one column, example:

COL 1        COl 2         COL3

Steve        XF3           17
Steve        XF4           2
Steve        XF5           1 
Jason        XF6           16
Jason        XF7           2

I would like to return rows 3 and 5 and all of their respective columns, because COL3 has the lowest value FOR STEVE and JASON. Pretend column2 cannot be leveraged as it cannot.

Much appreciated for any help on this.

I can't paste into here but I tried applying min function but ran into issues because there are multiple steve and jason.

MT0
  • 143,790
  • 11
  • 59
  • 117
Alan Seed
  • 1
  • 1

1 Answers1

0

CREATE TABLE mytable( col1, col2, col3) AS 
SELECT 'Steve',  'XF3',17 FROM DUAL UNION ALL 
SELECT 'Steve', 'XF4',  2 FROM DUAL UNION ALL 
SELECT 'Steve', 'XF5',  1 FROM DUAL UNION ALL 
SELECT 'Jason', 'XF6', 16 FROM DUAL UNION ALL 
SELECT 'Jason','XF7', 2 FROM DUAL 

select col1,
          col2,
          col3
from
       (
       SELECT col1, 
              col2,
              col3,
              min(col3) over (partition by col1) min_col3
       FROM   mytable
       )
where col3 = min_col3

COL1    COL2    COL3
Jason   XF7 2
Steve   XF5 1

WITH cte AS (
  SELECT
     col1, col2, col3,
         DENSE_RANK() OVER(PARTITION BY col1 ORDER BY col3) AS rnk
  FROM mytable 
)
SELECT 
     col1, col2, col3
FROM   cte 
WHERE  rnk=1


COL1    COL2 COL3
Jason   XF7  2
Steve   XF5  1


Beefstu
  • 804
  • 6
  • 11