HackerRank天气观测站5

时间:2018-06-07 09:08:20

标签: sql

我想知道为什么我的代码不起作用。这个问题在此之前已被提出: Query the two cities in STATION with the shortest and longest CITY names,

和解决方案: https://github.com/chhayac/SQL-hackerrank-problems/blob/master/basic-select.md

但这两个答案都不起作用。我已粘贴下面的问题,然后是我的解决方案。谢谢你的帮助!

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

输入格式

STATION表描述如下:

Station.jpg

其中LAT_N是北纬,LONG_W是西经。

示例输入

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

示例输出

ABC 3
PQRS 4

解释

按字母顺序排序时,CITY名称列为ABC,DEF,PQRS和WXY,各自的长度和。名字最长的城市显然是PQRS,但有最短命名的城市可供选择;我们选择ABC,因为它首先按字母顺序排列。

请注意 您可以编写两个单独的查询来获得所需的输出。它不一定是一个查询。

我的回答:

/ 按字母顺序排列的最短字符长度 /

SELECT city, LENGTH(city) as length_char
FROM station
ORDER BY LENGTH(city) ASC, city ASC
LIMIT 1;

/ 按字母顺序排列的最长字符长度 /

SELECT city, LENGTH(city) as length_char
FROM station
ORDER BY LENGTH(city) DESC
LIMIT 1;

15 个答案:

答案 0 :(得分:4)

您在github上的解决方案如下:

select city, length(city) from station order by length(city) DESC,city ASC fetch first row only;
select city, length(city) from station order by length(city) asc ,city asc fetch first row only;  

您在这里遇到问题-没有fetch first row only这样的命令。根据数据库系统的不同,可以是toplimitrownum-请在此处阅读更多内容-https://www.w3schools.com/sql/sql_top.asp

因此,根据系统的不同,答案也将有所不同。

Oracle

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

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

SQL Server

select top 1 city c, len(city) l
from   station
order by l desc, c asc;

select top 1 city c, len(city) l
from   station
order by l asc, c asc;

MySQL

select city c, length(city) l
from   station
order by l desc, c asc
limit 1;

select city c, length(city) l
from   station
order by l asc, c asc
limit 1;

或使用union

(select city, length(city) 
from station 
order by length(city) asc , city asc limit 1) 
union
(select city,length(city) 
from station 
order by length(city) desc, city asc limit 1)

答案 1 :(得分:1)

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

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

/* FOR MYSQL */

答案 2 :(得分:0)

使用以下查询:

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

答案 3 :(得分:0)

我在这个问题上的成功是:-

ais-refinement-list

答案 4 :(得分:0)

实际上,您的代码似乎正确。我认为唯一的问题可能是将工作地点设置为“ MYSQL”。如果您在“ MS SQL Server”上运行代码,它将给您一些“内置函数”的问题(例如,在mysql上,它写为lengt(),在ms sql服务器上,则是写为len())(或“限制1”等)

我尝试过的另一种解决方案是(在MS SQL Server上);

用于查找最长的字符城市(按字母顺序排在首位);

Select TOP 1 city, LEN(CITY)
From station 
Where  
len(city) = (select max(len(city)) from station ) 
Order By city asc ;

用于查找最短的字符城市(按字母顺序排在首位);

Select TOP 1 city, LEN(CITY)
From station
Where
len(city) = (select min(len(city)) from station) 
Order By city asc ;

答案 5 :(得分: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 desc
LIMIT 1;

答案 6 :(得分:0)

select t2.city , t2.t  
from  
(  
    select t1.city , t1.t , row_number() over (partition by t1.t order by t1.city) as ro 
    from
        ( select city , length(city)as t 
          from station 
        ) t1
    group by t1.city,t1.t
    having 
        t1.t = (select min(length(city)) from station )
                           or 
        t1.t = (select max(length(city)) from station)
) t2
where t2.ro = 1    ;    

表t2将给出具有最小和最大字符串长度的所有记录以及行编号,现在基于行num过滤记录将获取您所需的输出

答案 7 :(得分:0)

您也可以将此查询用作稍有不同的答案。我在MySQL的WHERE子句中使用了子查询

select CITY,LENGTH(CITY) from STATION where LENGTH(CITY)= (select min(LENGTH(CITY))from STATION) order by CITY LIMIT 1;
select CITY,LENGTH(CITY) from STATION where LENGTH(CITY)= (select max(LENGTH(CITY))from STATION) order by CITY LIMIT 1;

答案 8 :(得分:0)

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

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

答案 9 :(得分:0)

使用MySQL解决方案:

SELECT CITY, length(CITY) FROM STATION ORDER BY length( CITY), CITY ASC limit 1;

SELECT CITY, length(CITY) FROM STATION ORDER BY length(CITY) DESC limit 1;

答案 10 :(得分:0)

关注MYSQL-

(SELECT  CITY , LENGTH(CITY) AS CITY_LENGTH
    FROM STATION
    ORDER BY CITY_LENGTH DESC, CITY ASC
    LIMIT 1)
UNION ALL
(SELECT  CITY , LENGTH(CITY) AS CITY_LENGTH
    FROM STATION
    ORDER BY CITY_LENGTH ASC, CITY ASC
    LIMIT 1)

关注评论以获取更好的解释- AS关键字-别名 ASC / DESC-升序/降序 LIMIT函数-限制查询输出 联合函数-汇总结果

答案 11 :(得分:0)

import Credenitals from 'your/path';

这对我有用,希望对您有所帮助。

答案 12 :(得分:0)

对于甲骨文 ==> 我使用子查询解决了它并在子查询中使用了“MIN”和“MAX”函数,所以我跳过使用“ORDER BY”作为“MIN”,“MAX”函数在这里做同样的事情,只是使用“ORDER BY”外部查询按长度对最终输出进行排序。(希望有帮助!!)

SELECT CITY, LENGTH(CITY)
FROM STATION
WHERE CITY=(SELECT MIN(CITY)
            FROM STATION
            WHERE LENGTH(CITY)=(SELECT MIN(LENGTH(CITY))
                                FROM STATION)) OR
      CITY=(SELECT MIN(CITY)
            FROM STATION
            WHERE LENGTH(CITY)=(SELECT MAX(LENGTH(CITY))
                                FROM STATION))
ORDER BY LENGTH(CITY);

答案 13 :(得分:0)

对于 Oracle - 使用密集排名:

SELECT CITY, LENGTH(CITY) FROM (
    SELECT CITY, LENGTH(CITY), 
        DENSE_RANK() OVER (ORDER BY LENGTH(CITY) ASC, CITY ASC) ROW1
    FROM STATION
)WHERE ROW1 = 1 
UNION
SELECT CITY, LENGTH(CITY) FROM (
    SELECT CITY, LENGTH(CITY), 
        DENSE_RANK() OVER (ORDER BY LENGTH(CITY) DESC, CITY ASC) ROW2
    FROM STATION
)WHERE ROW2 = 1;

答案 14 :(得分:0)

(select city,length(city) from station 
order by  length(city) desc,city asc limit 1)
union
(select city,length(city) from station 
order by length(city) asc,city asc limit 1);

这在 MySQL 中有效。