Weather Observation Station 5

Sort by

recency

|

7201 Discussions

|

  • + 0 comments

    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);

  • + 0 comments

    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

  • + 0 comments

    The following is full one query to solve the problem

    '''sql

    select city,name_lenght from (

    select city,name_lenght,
        rank() over(order by name_lenght desc,city asc) as lnth_desc,
        rank() over(order by name_lenght asc,city asc) as lnth_asc
        from(
            select city,char_length(city) as name_lenght
            from station                
        ) x order by name_lenght desc
    

    ) x where lnth_asc<2 or lnth_desc<2 order by city;

  • + 0 comments

    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;

  • + 0 comments
    select top 1 city,len(city) from station where len(city)=(select min(len(city)) from station) order by city;
    select top 1 city,len(city) from station where len(city)=(select max(len(city)) from station) order by city;