用于查找表中最长名称和最短名称的SQL查询

时间:2016-02-14 20:11:32

标签: sql

我有一个表,其中一列是varchar(city)类型。并希望找到该列中存储的最长和最短的值。

select a.city, a.city_length from (select city, char_length(city) city_length 
from station order by city, city_length) a
where a.city_length = (select min(a.city_length) from a) or
      a.city_length = (select max(a.city_length) from a)
group by a.city_length;

有人可以帮忙吗?感谢

一个解决方案:

select * from (select city, char_length(city) city_length from station order by city, city_length) a group by a.city_length order by a.city_length limit 1;
select * from (select city, char_length(city) city_length from station order by city, city_length) a group by a.city_length order by a.city_length desc limit 1;

30 个答案:

答案 0 :(得分:18)

我认为我们不需要使用Min和Max功能,也不需要Group by。

我们可以使用以下代码实现此目的:

select top 1 City, LEN(City) City_Length from STATION order by City_Length ASC,City ASC

select top 1 CITY, LEN(city) City_Length from station order by City_Length desc, City ASC

但在这种情况下,它会在2表中显示输出,如果我们想在单个表中组合,那么我们可以使用Union或Union ALL。下面是相同

的SQL查询
  select * from (
     select top 1 City, LEN(City) City_Length from STATION order by City_Length ASC,City ASC) TblMin
   UNION
   select * from (
   select top 1 CITY, LEN(city) City_Length from STATION order by City_Length desc, City ASC) TblMax

这里我将select语句嵌套在子查询中,因为当我们使用order by子句时,我们不能直接使用 Union或Union ALL ,这就是为什么我把它写在子查询中。

答案 1 :(得分:8)

最短:

select TOP 1 CITY,LEN(CITY) LengthOfCity FROM STATION ORDER BY LengthOfCity ASC, CITY ASC;

最长:

select TOP 1 CITY,LEN(CITY) LengthOfCity FROM STATION ORDER BY LengthOfCity DESC, CITY ASC;

这适用于HackerRank挑战问题(MS SQL Server)。

答案 2 :(得分:4)

您的查询只需要一些调整。根本问题在于您不能像以前一样在子查询中使用a

select a.city, a.city_length
from (select city, char_length(city) city_length 
      from station 
     ) a
where a.city_length = (select min(char_length(city)) from station) or
      a.city_length = (select max(char_length(city)) from station);

也就是说,编写查询的一种更简单的方法是:

select s.*
from station s cross join
     (select min(char_length(city)) as mincl, max(char_length(city)) as maxcl
      from station
     ) ss
where char_length(s.city) in (mincl, maxcl);

答案 3 :(得分:4)

也许是一个更简单的选项,因为我想你正在寻找黑客等级问题解决方案的帮助?添加限制使我更容易调试返回错误的问题。

SELECT city, length(city) FROM station order by length(city) desc limit 1;

SELECT city, length(city) FROM station order by length(city) asc, city asc limit 1

答案 4 :(得分:2)

在MySQL中

(select city, LENGTH(city) cityLength  from station order by cityLength desc,city asc LIMIT 1)
    union all
    (select city, LENGTH(city) cityLength  from station order by cityLength asc,city asc LIMIT 1)

答案 5 :(得分:1)

这是CTE的一种方法。首先,它找到最长和最短的匹配城市:

!

结果

nil

答案 6 :(得分:1)

对于Oracle,我使用WITH子句将最短/最长的名称保存在一个临时变量中,而不是在主“ from”子句中进行查询。 SQL WITH clause example

WITH shortnames AS
(SELECT city, length(city) 
 FROM station 
 ORDER BY length(city) asc, city),

 longnames AS
 (SELECT city, length(city) 
  FROM station
  ORDER BY length(city) desc, city)

SELECT * FROM shortnames WHERE ROWNUM=1
UNION ALL
SELECT * FROM longnames WHERE ROWNUM=1;

答案 7 :(得分:1)

select top(1) city, max(len(city)) [Length] from station group by city order by [Length]
select top(1) city, max(len(city)) [Length] from station group by city order by [Length] DESC

在SQL Server 2016中测试

答案 8 :(得分:1)

我在SQL Server中使用CTE和dense_rank函数执行此操作。 排名如何运作?

长度上的第一个分区(表单组),即相同的长度构成一个组(分区)。然后在每个分区中按字母顺序排序所有名称。然后在每个分区中分配排名(dRank列)。因此,每组中的等级1将被分配给在其各自的分区中按字母顺序首先出现的名称。所有这些都发生在公用表表达式(cte block)

"with cte as
(
select *, LEN(city) as length, DENSE_RANK() over (partition by len(city) order by city) as dRank from Station
)"

select city,length from cte where dRank = 1 and length = (select MIN(length) from cte)
UNION
select city,length from cte where dRank = 1 and length = (select max(length) from cte)"

答案 9 :(得分:0)

length(CITY)将返回字符串的长度,

https://www.w3schools.com/sql/func_mysql_length.asp

import 'dart:convert';

答案 10 :(得分:0)

SELECT MAX(LENGTH(transaction_id)) AS Longest ,MIN(LENGTH(transaction_id)) AS Shortest FROM cards

答案 11 :(得分:0)

Oracle 中,我们可以这样做

select city, length(city)
from (select city from STATION order by length(city) DESC, city ASC)
where rownum = 1 
union
select city, length(city)  
from (select city from STATION order by length(city), city ASC)
where rownum = 1;

想法是得到最长和最短的城市,然后将它们合并为一个结果。

答案 12 :(得分:0)

从(选择城市,长度(城市),等级()超过(按长度(城市)划分,按长度(城市),城市asc)按从车站到rnk的位置)选择城市,长度(城市)a,其中a。 rnk = 1;

答案 13 :(得分:0)

这是在 MySQL 中执行此操作的另一种方法。可能不是最好的,但仍然是逻辑上正确的选择。

select
   city,
   length(city) 
from
   station 
where
   length(city) in 
   (
      select
         max(length(city)) 
      from
         station 
      union
      select
         min(length(city)) 
      from
         station
   )
order by
   length(city) desc,
   city asc limit 2;

答案 14 :(得分:0)

对于一个结果表中的oracle SQL。这将检索最小和最大城市名称,如果长度相同,则将按字母顺序排序的第一个城市。

SELECT * FROM (
    SELECT CITY, LENGTH(CITY) CITY_LENGTH 
    FROM STATION 
    ORDER BY CITY_LENGTH ASC, CITY ASC ) MAX_LEN  
WHERE ROWNUM <= 1
UNION
SELECT * FROM (
    SELECT CITY, LENGTH(CITY) CITY_LENGTH 
    FROM STATION 
    ORDER BY  CITY_LENGTH DESC, CITY ASC ) MIN_LENGTH  
WHERE ROWNUM <= 1;

答案 15 :(得分:0)

此查询将在您的条件下很好地工作,这两个查询完全相同。在第一个查询中,我们只按降序对记录进行排序,以获取长度最大的城市名称,在第二部分中,我们仅以升序对记录进行获取,以获取长度最小的城市,其余所有查询均相同。绑架看看。

详细信息:

  1. 选择语句以取出记录
  2. 使用group by子句,因为我正在使用Len()聚合函数。
  3. 以降序和升序对所有检索到的记录进行排序,以获取前1位并采用最大和最小长度的城市。

    select  Top 1  City,max(len(City)) as Length  
    from STATION 
    group by City 
    ORDER BY Length desc 
    
    select  Top 1  City,max(len(City)) as Length  
    from STATION 
    group by City 
    ORDER BY Length ASC
    

答案 16 :(得分:0)

在Oracle中应该是

SELECT CITY,LENGTH(CITY) FROM   (SELECT MIN(CITY) CITY  FROM STATION WHERE LENGTH(CITY)=(SELECT MIN(LENGTH(CITY)) FROM STATION))
UNION ALL
SELECT CITY,LENGTH(CITY) FROM   (SELECT MAX(CITY) CITY  FROM STATION WHERE LENGTH(CITY)=(SELECT MAX(LENGTH(CITY)) FROM STATION));    

答案 17 :(得分:0)

在Oracle 12c中,可以使用FETCH..FIRST

完成此操作

最短

select * FROM station ORDER BY LENGTH(city) DESC FETCH FIRST 1 ROWS ONLY;

最长

select * FROM station ORDER BY LENGTH(city) ASC FETCH FIRST 1 ROWS ONLY;

答案 18 :(得分:0)

最短的城市名称:

"names"

城市的最长名称:

SELECT ST.CITY,LENGTH(ST.CITY) AS LENGTH FROM STATION ST
ORDER BY LENGTH ASC, ST.CITY ASC
LIMIT 1;

答案 19 :(得分:0)

以下查询似乎很简单:

select 
    city, leng 
from
    (select top 1 
         city, len(city) leng 
     from 
         station 
     where 
         len(city) = (select min(len(city)) from station) 
     order by 
         city

     Union all

     select top 1 
         city, len(city) leng 
     from 
         station 
     where 
         len(city) = (select max(len(city)) from station)  
     order by 
         city) result;

第一个查询内部返回最小长度的城市,而第二个查询返回最大长度的城市。

希望这有帮助。

答案 20 :(得分:0)

对于oracle:

select min(city),length(city) from station where length(city) <= all(select 
length(city) from station) group by length(city);

select max(city),length(city) from station where length(city) >= all(select 
length(city) from station) group by length(city);

答案 21 :(得分:0)

在Oracle(以及支持分析函数的任何其他语言)中,使用ROW_NUMBER分析函数,您可以根据ASC结尾(或DESC结尾)为行分配唯一编号城市的长度。由于可能存在多个具有相同长度的行,因此可以应用二级顺序以按字母顺序获取该长度的第一个城市。然后,您只需要一个外部查询,将结果过滤为最短(或最长)的名称:

SELECT city
FROM   (
  SELECT CITY,
         ROW_NUMBER() OVER ( ORDER BY LENGTH( CITY ) ASC,  CITY ) shortest_rn,
         ROW_NUMBER() OVER ( ORDER BY LENGTH( CITY ) DESC, CITY ) longest_rn
  FROM   station
)
WHERE shortest_rn = 1
OR    longest_rn  = 1;

如果您想要返回名称最短(或最长)的所有城市,请使用DENSE_RANK代替ROW_NUMBER

SELECT city
FROM   (
  SELECT CITY,
         DENSE_RANK() OVER ( ORDER BY LENGTH( CITY ) ASC  ) shortest_rn,
         DENSE_RANK() OVER ( ORDER BY LENGTH( CITY ) DESC ) longest_rn
  FROM   station
)
WHERE shortest_rn = 1
OR    longest_rn  = 1
ORDER BY shortest_rn, city; -- Shortest first and order tied lengths alphabetically

答案 22 :(得分:0)

升序:

SELECT city, CHAR_LENGTH(city) FROM station ORDER BY CHAR_LENGTH(city), city LIMIT 1;

降序:

SELECT city, CHAR_LENGTH(city) FROM station ORDER BY CHAR_LENGTH(city) DESC, city LIMIT 1;

答案 23 :(得分:0)

在Oracle中:

select * from (select city, min(length(city)) minl from station group by city order by minl, city) where rownum = 1;
select * from (select city, max(length(city)) maxl from station group by city order by maxl desc, city) where rownum = 1;

答案 24 :(得分:0)

最初找到city的最短长度并使用city的最长长度的联合。这最大限度地降低了查询的复杂性。

(select city, char_length(city) as len_city
from station
order by len_city limit 1)
union ( select city, char_length(city) as len_city
    from station
    order by len_city desc limit 1) 
order by len_city

答案 25 :(得分:-1)

最短:

select city, char_length(city) city_length from station order by city_length, city limit 1;

最长:

select city, char_length(city) city_length from station order by city_length desc, city limit 1;

答案 26 :(得分:-1)

select * from (select city,length(city)from station group by city having length(city) in ((select min(length(city))from station))order by city) where rownum<2
UNION
select * from (select city,length(city)from station group by city having length(city) in ((select max(length(city))from station))order by city) where rownum<2;

答案 27 :(得分:-1)

with cte (rank, city , CityLength) 
As 
(select  dense_rank() over (partition by len(city) order by city asc) as Rank, city, len(city) 
    from station 
where len(city) in 
    ((select max(len(city)) from station) 
    union (select min(len(city)) from station)))
select city,citylength from cte where rank = 1;

答案 28 :(得分:-1)

对于 Oracle 数据库:

select city,length(city) from (select * from station order by length(city),city) where rownum=1

联合

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

答案 29 :(得分:-2)

我认为这应该有效:

    SELECT MAX(CITY) , LENGTH(MAX(CITY)) FROM STATION;
    SELECT MIN(CITY) , LENGTH(MIN(CITY)) FROM STATION;