Hackerrank SQL挑战

时间:2017-02-06 21:16:16

标签: sql-server tsql

此T-SQL查询

SELECT city, Len(city) 
FROM station 
ORDER BY Len(city)

返回按city排序的表格,而不是Len(city) - 这是正确的行为吗?

Acme 4 
Addison 7 
Agency 6 
Aguanga 7 
Alanson 7 
Alba 4 
...

挑战是:

https://www.hackerrank.com/challenges/weather-observation-station-5

1 个答案:

答案 0 :(得分:1)

既然你想要第一个也是最后一个,我可能只是使用一个工会和顶部1.明确你所追求的并且易于维护。

因为你可以按顺序使用别名......我别名len(city)

SELECT TOP 1 
    city, len(city) LenCity 
FROM 
    station 
ORDER BY
    LenCity ASC 

UNION ALL 

SELECT TOP 1 
    city, Len(City) lenCity 
FROM 
    station 
ORDER BY 
    LenCity DESC