使用最短和最长的CITY名称查询STATION中的两个城市,

时间:2016-08-24 17:28:30

标签: sql oracle

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

示例输入:

让我们说CITY只有四个条目:DEF, ABC, PQRS and WXY

示例输出:

ABC 3
PQRS 4

7 个答案:

答案 0 :(得分:11)

尝试这个:)

mysql代码....简单的一个

select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1; 
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1; 

答案 1 :(得分:5)

select min(city), len
  from (
        select city, length(city) len,
               max(length(city)) over() maxlen,
               min(length(city)) over() minlen
          from station
       )
 where len in(minlen,maxlen)
 group by len

子查询获取城市列表及其长度。同时"window functions" min/max over()获取set(table)中所有行的最小和最大长度。主查询过滤器只有长度为最小/最大的城市。 min(city)len按字母顺序给出结果的第一个名称。

答案 2 :(得分:5)

( select CITY, 
       char_length(CITY) as len_city 
  from STATION 
  where char_length(CITY)=(select char_length(CITY) 
                          from STATION 
                          order by char_length(CITY) LIMIT 1) 
  Order by CITY LIMIT 1) 
 UNION ALL 
 (select CITY,
        char_length(CITY) as len_city 
  from STATION 
  where char_length(CITY)=(select char_length(CITY) 
                           from STATION 
                           order by char_length(CITY) DESC LIMIT 1)  
  Order by CITY DESC LIMIT 1) 
  ORDER BY char_length(CITY);

答案 3 :(得分:4)

对于MS SQL Server:

Declare @Small int
Declare @Large int
select @Small = Min(Len(City)) from Station 
select @Large = Max(Len(City)) from Station
select Top 1 City as SmallestCityName,Len(City) as Minimumlength from Station where Len(City) = @Small Order by City Asc
select Top 1 City as LargestCityName,Len(City) as MaximumLength from Station where Len(City) = @Large Order by City Asc

对于Oracle服务器:

select * from(select distinct city,length(city) from station order by length(city) asc,city asc) where rownum=1 union
select * from(select distinct city,length(city) from station order by length(city) desc,city desc) where rownum=1;

答案 4 :(得分:2)

以下是另一种使用常用row_number分析函数的方法:

with cte as (
  select city,
         length(city) as len,
         row_number() over (order by length(city), city) as smallest_rn,
         row_number() over (order by length(city) desc, city) as largest_rn
    from station
)
select city, len
  from cte
 where smallest_rn = 1
union all
select city, len
  from cte
 where largest_rn = 1

答案 5 :(得分:1)

SELECT city, LENGTH(city) FROM station  
WHERE LENGTH(city)=(SELECT MAX(LENGTH(city)) FROM station)
AND ROWNUM=1;

SELECT city, LENGTH(city) 
FROM station  
WHERE LENGTH(city)=(SELECT MIN(LENGTH(city)) FROM STATION) 
AND ROWNUM=1
ORDER BY CITY;

答案 6 :(得分:0)

使用UNION

试试这个
SELECT MIN(city), LENGTH(city)
FROM Station
WHERE LENGTH(city) =
(SELECT MIN(LENGTH(city))
FROM Station)
UNION
SELECT MIN(city), LENGTH(city)
FROM Station
WHERE LENGTH(city) =
(SELECT MAX(LENGTH(city))
FROM Station)

当然,我的假设是您的表名是Station,列名是City。请参阅下面的相关文章,仅按字母顺序选择第一条记录:

Return only the first alphabetical result of SELECT