Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于查找表中最长名称和最短名称的SQL查询_Sql - Fatal编程技术网

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

用于查找表中最长名称和最短名称的SQL查询,sql,Sql,我有一个表,其中一列的类型是varcharcity。并希望找到存储在该列中的最长值和最短值 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 =

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

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;

您的查询只需要一些调整。基本问题是,在执行以下操作时,不能在子查询中使用:

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);

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

DECLARE @tbl TABLE(CityName VARCHAR(100));
INSERT INTO @tbl VALUES ('xy'),('Long name'),('very long name'),('middle'),('extremely long name');

WITH MyCTE AS 
(
    SELECT MAX(LEN(CityName)) AS Longest
          ,MIN(LEN(CityName)) AS Shortest
    FROM @tbl
)
SELECT * 
FROM MyCTE
--You must think about the chance of more than one city matching the given length
CROSS APPLY(SELECT TOP 1 CityName FROM @tbl WHERE LEN(CityName)=Longest) AS LongestCity(LongName)
CROSS APPLY(SELECT TOP 1 CityName FROM @tbl WHERE LEN(CityName)=Shortest) AS ShortestCity(ShortName)
结果

Longest Shortest    LongName               ShortName
19       2          extremely long name    xy

我在SQLServer中使用CTE和dense_rank函数实现了这一点。 排名是如何运作的

第一个分区在长度上形成组,即相同的长度构成一个组分区。然后在每个分区内按字母顺序排列所有名称。然后在每个分区内分配列。因此,每个组中的排名1将被分配给在各自分区中按字母顺序排在第一位的名称。所有这些都发生在公共表表达式cte块中

"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)"

最初找到城市的最短长度,并与城市的最长长度合并。这将最小化查询的复杂性

(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
在SQL Server 2016中进行了测试

我认为我们不需要使用最小值和最大值函数,也不需要使用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或UNIONALL。下面是针对同一问题的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语句嵌套在子查询中,因为当我们使用ORDERBY子句时,我们不能直接使用Union或Union ALL,这就是为什么我将其写入子查询中的原因。

在Oracle中:

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;
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;
也许是一个更简单的选择,因为我想你正在寻找一个解决黑客排名问题的帮助?限制的增加使我更容易调试返回错误的地方

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

我认为这应该奏效:

    SELECT MAX(CITY) , LENGTH(MAX(CITY)) FROM STATION;
    SELECT MIN(CITY) , LENGTH(MIN(CITY)) FROM STATION;
最短的:

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;
最短的:

select city, char_length(city) city_length from station order by city_length, city limit 1;
按长度城市ASC、城市ASC从车站顺序中选择排名前1的城市、长度城市; 最长:

select city, char_length(city) city_length from station order by city_length desc, city limit 1;
从车站顺序中,按“长度城市描述”、“城市ASC”选择排名前1的城市“长度城市”; 这适用于黑客攻击问题MS SQL Server。

升序:

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;
在Oracle和任何其他支持分析函数的语言中,使用分析函数可以根据城市的上升或下降长度为行分配唯一的编号。由于可能有多行具有相同的长度,因此可以应用二级顺序以按字母顺序获得该长度的第一个城市。然后,您只需要一个外部查询,将结果过滤为最短或最长的名称:

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;
如果要返回名称最短或最长的所有城市,请使用稠密排名而不是行编号:


在Oracle 12c中,这可以使用FETCH..FIRST来完成

最短的

最长的

对于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);
在MySQL中


以下查询似乎很简单:

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;
内部的第一个查询返回长度最小的城市,而第二个查询返回长度最大的城市


希望这有帮助。

有关城市的最短名称:

SELECT ST.CITY,LENGTH(ST.CITY) AS LENGTH FROM STATION ST
ORDER BY LENGTH ASC, ST.CITY ASC
LIMIT 1;
SELECT ST.CITY,LENGTH(ST.CITY) AS LENGTH FROM STATION ST
ORDER BY LENGTH DESC, ST.CITY DESC
LIMIT 1;
城市最长名称:

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

在甲骨文中是这样的

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));    

此查询将在您的条件下正常工作。这两个查询完全相同。在第一个查询中,我们只是按降序排列记录,以获取长度最大的城市名称;在第二部分中,我们只是按升序排列记录,以获取长度最小的城市。所有其他查询都是相同的。请看一看

详情:

选择要取出记录的语句 使用GROUPBY子句,因为我使用的是Len聚合函数。 按降序和升序对所有检索到的记录进行排序,以获得前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

对于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;

从select city,lengthcity中选择city,lengthcity,按lengthcity,city asc按长度城市顺序将超额部分排序为stationa中的rnk,其中a.rnk=1

在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;

其思想是获取最长和最短的城市,然后将它们合并为一个结果。

lengthCITY将返回字符串的长度

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

我使用for Oracle的WITH子句将最短/最长的名称保存在临时变量中,而不是在main from子句中查询。


这是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;
对于Oracle数据库:


从select*中选择城市、长度城市,从按长度城市排列的车站顺序中选择城市,其中rownum=1

联合


按lengthcity desc从选择*中选择城市、lengthcity,其中rownum=1的城市

世界银行
ome到堆栈溢出!虽然这个代码片段可能会解决这个问题,包括一个解释,以提高您的文章的质量。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在您的回答中添加解释,并说明适用的限制和假设。您正在使用哪些RDBMS?我们需要知道这一点,因为我们将能够使用更高效的rdbms特定功能,并为您提供一些有关高级技术的提示。请注意,可能没有“最长”的名称,这意味着您可能有多个名称。请对此提供一些解释。如果我们选择Oracle,则此查询在黑客级别中不起作用。它抛出错误,因为“error is error位于第1行:ORA-00904:TOP:无效标识符”`此查询用于MS SQL Server,无法与oracle一起使用。针对oracle尝试以下操作:最短:从选择城市中选择*,从站点中选择LENGTHCITY LengthOfCity按LengthOfCity ASC排序,城市ASC,其中rownum=1;最长:选择*从选择城市,长度城市长度从车站顺序按长度城市描述,城市ASC,其中rownum=1;对于MySql:从LEN ASC的车站订单中选择CITY、LENGTHCITY作为LEN,CITY ASC LIMIT 1;通过LEN DESC、CITY ASC LIMIT 1从车站订单中选择城市、Length CITY作为LEN;什么是长度。。。是不是更像别名。。。hacker上没有列名为LengthOfCityrank@ShubhamJain您说得对,它是别名,定义为以后按顺序使用。with oracle error作为错误弹出在第1行:ORA-00904:TOP:invalid identifierProduct specific对未指定dbms的问题的回答。至少告诉我们这是针对哪个dbms的。@jarlh这是针对SQL Server的。varchar的最大值和最小值,根据字母顺序而不是纵向限制1比较值?你能解释一下吗?@Taylor,在本例中,限制1将查询的输出减少为一个条目。通过查询,您可以获得具有最短和最长城市名称的城市。
SELECT MAX(LENGTH(transaction_id)) AS Longest ,MIN(LENGTH(transaction_id)) AS Shortest FROM cards
(select CITY, length(CITY) from STATION order by length(CITY),CITY limit 1)
UNION
(select CITY, length(CITY) from STATION order by length(CITY) DESC limit 1);
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;
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;