작성
·
256
0
글자 최소길이, 최대 길이 작성
from 서브쿼리에 전체 데이터 글자 작성
1,2번 조인을 통해 글자 최소길이, 최대 길이의 city 구함
글자수 별 city컬럼 오름차순을 통해 1순위 데이터만 출력을 위한 ROW_NUMBER함수 사용
ROW_NUMBER가 1인 것만 출력
이런식으로 굳이 복잡하게 풀었습니다..
더 쉬운 방법이 있을 거 같은데 알려주시면 감사하겠습니다.
Select
city, lc
From
(
Select city
, lc
, ROW_NUMBER() OVER(Partition By lc Order By city) rn
From(
Select city, length(city) lc
From station a)a
inner join
(Select min(length(city)) min_c, max(length(city)) max_c
From station)b
on a.lc = b.min_c or a.lc = b.max_c
)a
Where rn = 1
답변 1
1
저라면 row_number()를 사용한다면, 두 번 사용하는 방법을 택할 것 같아요.
하나는 길이의 오름차순으로, 하나는 내림차순으로 두 가지의 row number를 만들어 필터링하는 방식입니다.
아래 쿼리 참고해 주세요.
select city, lc
from (
select city, length(city) as lc
, row_number() over (order by length(city), city) as row_no_asc
, row_number() over (order by length(city) desc, city) as row_no_desc
from station
) a
where row_no_asc = 1 or row_no_desc = 1
order by lc