最短和最长的城市名称

Posted

技术标签:

【中文标题】最短和最长的城市名称【英文标题】:shortest and longest city name 【发布时间】:2019-12-25 06:49:03 【问题描述】:

查询 STATION 中两个城市的 CITY 名称最短和最长,以及它们各自的长度(即:名称中的字符数)。如果有不止一个最小或最大的城市,请选择按字母顺序排列的第一个城市。

请问这个问题的答案是什么?

  SELECT CITY,
         LENGTH(CITY)
    FROM STATION
ORDER BY CITY ASC
   FETCH FIRST 1 ROWS ONLY ;

为什么此查询不适用于获取最短长度城市?

【问题讨论】:

这不起作用,因为您是通过city 订购的,而您可能打算通过length(city) 订购。 【参考方案1】:

我会在这里使用ROW_NUMBER

WITH cte AS (
    SELECT s.*, ROW_NUMBER() OVER (ORDER BY LENGTH(CITY), CITY) rn_first,
        ROW_NUMBER() OVER (ORDER BY LENGTH(CITY) DESC, CITY) rn_last
    FROM STATION s
)

SELECT CITY, LENGTH(CITY) AS CITY_LENGTH
FROM cte
WHERE 1 IN (rn_first, rn_last)
ORDER BY LENGTH(CITY);

这将生成一个包含两个记录的结果集,最短的城市名称出现在最前面,最长的出现在最后。

【讨论】:

是的,我在黑客等级上使用 row_number 做到了这一点。Oracle 也只接受获取前 1 行,但这在黑客等级上不起作用。【参考方案2】:

我最喜欢在 Oracle 中执行此操作的方法是使用 keep 关键字:

select min(city) keep (dense_rank first order by length(city) asc) as shortest_city,
       min(city) keep (dense_rank first order by length(city) desc) as longest_city,
       min(length(city)) as min_len,
       max(length(city)) as max_len
from station;

根据我的经验,keep 的性能非常好,因此如果它在大数据上具有最佳性能,我不会感到惊讶。

【讨论】:

我不明白。为什么会缺少城市名称? @BarbarosÖzhan 。 . .谢谢你。我确定了答案。【参考方案3】:

感谢您的回复。我会检查“保留”一次。 我是这样解决的。有没有其他的可能来优化它或者让它更简单?

**

select city,length(city) as "SL"

from (select city from station  order by city asc) 

where length(city) in (select min(length(city)) from station) and rownum=1; 

select city,length(city) as "LL"

from (select city from station  order by city asc) 

where length(city) in (select max(length(city)) from station) 

order by city desc;`

**

【讨论】:

【参考方案4】:

对于 MS SQL Server,解决方案如下。

Declare @Small int
Declare @Large int
select @Small = Min(Len(CITY )) from STATION 
select @Large = Max(Len(CITY )) from STATION
;with cte1 as(
select Top 1 CITY  as SmallestCity,Len(CITY ) as Minimumlength from STATION  where Len(CITY ) = @Small Order by CITY  Asc
),
cte2 as(
select Top 1 CITY  as LargestCity,Len(CITY ) as MaximumLength from STATION  where Len(CITY ) = @Large Order by CITY  Asc 
)
select * from cte1
union
select * from cte2

它的输出看起来像这样, Out put of query

【讨论】:

【参考方案5】:

我编写的代码适用于 Microsoft SQL Server

select city, len(city) from station where len(city) = (select max(len(city)) from station); 
select city, len(city) from station where len(city) = (select min(len(city)) from station) order by city desc offset 2 rows

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。【参考方案6】:

你需要修改你的ORDER BY:

SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY
FETCH FIRST 1 ROWS ONLY ;

这将给出最长名字的答案。对于最短的名称,只需将ORDER BY 更改为使用ASCLENGTH(CITY)

【讨论】:

要求要求两个城市名称,而不仅仅是一个。 我确实阅读了该部分,但用户没有指定它必须在一个查询中。不过,您的回答会解决这个问题。【参考方案7】:
SELECT CITY,
       LENGTH(CITY) as length
  FROM CITY
 ORDER BY length ASC limit 1;

【讨论】:

limit 选项 ORDER 子句在 Oracle 中不存在。 虽然这个答案可能是正确的。仅代码答案很少有帮助。请评论您的代码并解释此代码如何解决问题。【参考方案8】:

选择城市,长度(城市)

从车站

WHERE LENGTH(CITY) IN (SELECT min(LENGTH(CITY))

                                   FROM  STATION
                            )

AND ROWNUM

联合

选择城市,长度(城市)

从车站 WHERE LENGTH(CITY) IN (SELECT max(LENGTH(CITY))

                               FROM  STATION )

AND ROWNUM

按城市 ASC 排序;

【讨论】:

【参考方案9】:
SELECT top 1 City,LEN(City) as Citylength into #tmp
FROM STATION
WHERE LEN(City)=(SELECT MIN(LEN(City)) FROM STATION )
order by City asc

SELECT top 1 City,LEN(City) as Citylength into #tmp1
FROM STATION
WHERE LEN(City)=(SELECT MAX(LEN(City)) FROM STATION )
order by City

select * from #tmp
union
select * from #tmp1

【讨论】:

以上是关于最短和最长的城市名称的主要内容,如果未能解决你的问题,请参考以下文章

HackerRank天气观测站5

[不使用TOP的SELECT SSMS的最后一行Hackerrank挑战者

1195.最长&最短文本

C语言 统计歌词中的单词个数,并找出最短和最长的单词(必须用指针)求帮忙,感谢!!!

华科考研机试题最长&最短文本

如何在c程序中找到最长和最短的字符串?