SQL/Oracle: Select min and max column values for each unique value in another column

StackOverflow https://stackoverflow.com/questions/22515707

  •  17-06-2023
  •  | 
  •  

سؤال

Lets say I have the following data

CODE   TYPE
1      1
2      1
6      1
8      1
10     1
2      2
3      2
5      2
9      2
11     2

How can I go about getting the min and max CODE for each unique value of TYPE? Basically I want the query to produce the following:

MIN    MAX   TYPE
1      10    1
2      11    2

Thanks.

هل كانت مفيدة؟

المحلول

Group by the type and use the aggregate functions min() and max()

select min(code) as min,
       max(code) as max,
       type
from your_table
group by type
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top