We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Weather Observation Station 5
Weather Observation Station 5
Sort by
recency
|
7201 Discussions
|
Please Login in order to post a comment
WITH CTE AS( SELECT CITY, LENGTH(CITY), RANK() OVER (ORDER BY LENGTH(CITY), CITY ASC) AS RANKING FROM STATION ) SELECT CITY, LENGTH(CITY) FROM CTE WHERE RANKING = 1 OR RANKING = (SELECT MAX(RANKING) FROM CTE);
First I selected the cities and ranked them basing on their length. Then i filtered min or max lengths. Finaly wraped the query in a CTE and filtered the first row of every rank.
WITH cte_cities AS ( SELECT city , LEN(city) as citylength , ROW_NUMBER() OVER(PARTITION BY LEN(city) ORDER BY city) as cityorder FROM station WHERE LEN(city) = (SELECT MIN(LEN(city)) FROM station) OR LEN(city) = (SELECT MAX(LEN(city)) FROM station) ) select city, citylength from cte_twocities where cityorder=1 order by citylength, city asc
The following is full one query to solve the problem
'''sql
select city,name_lenght from (
) x where lnth_asc<2 or lnth_desc<2 order by city;
with cte as ( select city,length(city) as l,row_number() over ( order by length(city) desc,city) as r_desc, row_number() over ( order by length(city) ,city) as r_asc from station ) select city,l from cte where r_desc=1 union all select city,l from cte where r_asc=1;