programing

STATION의 두 도시에 가장 짧고 긴 CITY 이름을 쿼리합니다.

itmemos 2023. 6. 16. 21:28
반응형

STATION의 두 도시에 가장 짧고 긴 CITY 이름을 쿼리합니다.

쿼리: STATION 테이블에 포함된 두 도시를 각각의 길이(예: 이름의 문자 수)와 함께 가장 짧고 긴 CITY 이름으로 쿼리합니다.두 개 이상의 가장 작거나 큰 도시가 있는 경우, 알파벳 순으로 정렬할 때 가장 먼저 오는 도시를 선택합니다.

enter image description here

여기서 LAT_N은 북위도이고 LONG_W는 서경도입니다.

샘플 입력:

CITY에는 4개의 항목만 있다고 가정해 보겠습니다.DEF, ABC, PQRS and WXY

샘플 출력:

ABC 3
PQRS 4

사용해 보세요 :)

mysql 코드... 간단한 것.

select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1; 
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1; 

편집:

위의 해결책은 알파벳 순으로 정렬되지 않아 저에게 효과가 없습니다.논평한 바와 같이@omotto다음은 그것을 작동시키는 적절한 방법입니다.저는 SQL 서버에서 시도해 보았는데 작동합니다.

select top 1 city, len(city) from station order by len(city) ASC, city ASC; 
select top 1 city, len(city) from station order by len(city) DESC, city ASC;

MS SQL Server의 경우:

Declare @Small int
Declare @Large int
select @Small = Min(Len(City)) from Station 
select @Large = Max(Len(City)) from Station
select Top 1 City as SmallestCityName,Len(City) as Minimumlength from Station where Len(City) = @Small Order by City Asc
select Top 1 City as LargestCityName,Len(City) as MaximumLength from Station where Len(City) = @Large Order by City Asc

Oracle 서버의 경우:

select * from(select distinct city,length(city) from station order by length(city) asc,city asc) where rownum=1 union
select * from(select distinct city,length(city) from station order by length(city) desc,city desc) where rownum=1;
( select CITY, 
       char_length(CITY) as len_city 
  from STATION 
  where char_length(CITY)=(select char_length(CITY) 
                          from STATION 
                          order by char_length(CITY) LIMIT 1) 
  Order by CITY LIMIT 1) 
 UNION ALL 
 (select CITY,
        char_length(CITY) as len_city 
  from STATION 
  where char_length(CITY)=(select char_length(CITY) 
                           from STATION 
                           order by char_length(CITY) DESC LIMIT 1)  
  Order by CITY DESC LIMIT 1) 
  ORDER BY char_length(CITY);
select min(city), len
  from (
        select city, length(city) len,
               max(length(city)) over() maxlen,
               min(length(city)) over() minlen
          from station
       )
 where len in(minlen,maxlen)
 group by len

하위 쿼리는 도시 목록과 길이를 가져옵니다.동시에 "창 기능" min/max over()집합(표)의 모든 행에 대해 최소 및 최대 길이를 가져옵니다.기본 쿼리 필터 길이의 도시만 min/max입니다. min(city)의 그룹과 함께len알파벳 순으로 결과 이름을 지정합니다.

항상 편리한 기능을 사용하여 수행할 수 있는 또 다른 방법은 다음과 같습니다.row_number분석 기능:

with cte as (
  select city,
         length(city) as len,
         row_number() over (order by length(city), city) as smallest_rn,
         row_number() over (order by length(city) desc, city) as largest_rn
    from station
)
select city, len
  from cte
 where smallest_rn = 1
union all
select city, len
  from cte
 where largest_rn = 1
  SELECT * FROM (SELECT city, length(city) 
  FROM station
  WHERE LENGTH(city)=(SELECT MIN(LENGTH(city)) FROM station) ORDER BY city ) 
  WHERE ROWNUM =1;
  SELECT city, LENGTH(city) 
  FROM station  
  WHERE LENGTH(city)=(SELECT MIN(LENGTH(city)) FROM STATION) 
  AND ROWNUM=1
  ORDER BY CITY;

언급URL : https://stackoverflow.com/questions/39129585/query-the-two-cities-in-station-with-the-shortest-and-longest-city-names

반응형