sql - 查询长度最长和最短的城市名称
问题描述
我编写了一个查询来返回 MS SQL SERVER 数据库中长度最短和最长的城市。
Select city, len(city) as l
From Station Where len(city) in
((select max(len(city)) from station)
Union
(select min(len(city)) from station))
Order by l,city;
我的困难是我得到了重复,因为我有几个城市的长度最长和最短。此外,当我尝试ORDER BY CITY
在两个子查询中失败时。
有什么建议吗?
解决方案
我可能会这样做:
select top (1) with ties city
from station
order by len(city) asc
union
select top (1) with ties city
from station
order by len(city) desc;
或者:
select distinct city
from station
where len(city) = (select max(len(city)) from station) or
len(city) = (select min(len(city)) from station);
推荐阅读
- express - 如何在应用程序启动时在 MongoDB 中插入初始数据?
- postgresql - 从文件制作者快照文件映射外部数据源记录 ID
- mysql - 查询 7 个表,其中一个表中的值作为返回结果中的列标题
- wordpress - WordPress API:加载资源失败:服务器响应状态为 401(未授权)
- sql - 从分组依据中排除列
- android - Flutter:在 Whatsapp 和 Facebook 共享图像时无法在预览中显示图像
- mysql - 查询似乎间歇性地在 MySQL/Sequelize 上超时
- c# - 需要帮助从拇指驱动器上执行 Windows 窗体 C# 应用程序
- spring - 如何在 SimpleJDBCTemplate 中获取连接对象
- lua - 如何获得 Corona Simulator 正在运行的操作系统?