HackerRank天气观测站5
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HackerRank天气观测站5相关的知识,希望对你有一定的参考价值。
我想知道为什么我的代码不起作用。这个问题在此之前已经被问到:qazxsw poi
和解决方案:Query the two cities in STATION with the shortest and longest CITY names,
但这两个答案都不起作用。我已粘贴下面的问题,然后是我的解决方案。谢谢您的帮助!
使用最短和最长的CITY名称查询STATION中的两个城市,以及它们各自的长度(即:名称中的字符数)。如果有多个最小或最大的城市,请选择按字母顺序排序的城市。
输入格式
STATION表描述如下:
https://github.com/chhayac/SQL-hackerrank-problems/blob/master/basic-select.md
其中LAT_N是北纬,LONG_W是西经。
样本输入
假设CITY只有四个条目:DEF,ABC,PQRS和WXY
样本输出
Station.jpg
说明
按字母顺序排序时,CITY名称列为ABC,DEF,PQRS和WXY,各自的长度和。名字最长的城市显然是PQRS,但有最短命名的城市可供选择;我们选择ABC,因为它首先按字母顺序排列。
注意您可以编写两个单独的查询以获得所需的输出。它不一定是一个查询。
我的答案:
/最短字符长度按字母顺序排序/
ABC 3
PQRS 4
/最长字符长度按字母顺序排序/
SELECT city, LENGTH(city) as length_char
FROM station
ORDER BY LENGTH(city) ASC, city ASC
LIMIT 1;
您在github上的解决方案如下所示:
SELECT city, LENGTH(city) as length_char
FROM station
ORDER BY LENGTH(city) DESC
LIMIT 1;
你有一个问题 - 没有像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
,top
或limit
- 请在这里阅读更多 - rownum
因此,根据系统的不同,答案也会有所不同。
神谕
https://www.w3schools.com/sql/sql_top.asp
SQL Server
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;
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;
或使用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)
我对这个问题的看法是: -
(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);
实际上你的代码似乎是对的。我认为唯一的问题可能是设置工作地点而不是“MYSQL”。如果您在“MS SQL Server”上运行代码,它将为您提供一些“内置函数”问题(比如在mysql上编写lengt(),但在ms sql server上写入len())(或“限制1”)等等。)
我试过的其他解决方案之一是(在MS SQL Server上);
寻找最长的角色城市(按字母顺序排列);
SELECT CITY,LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY),CITY
LIMIT 1 OFFSET 0;
SELECT CITY,LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC,CITY
LIMIT 1 OFFSET 0;
寻找最短的人物城市(按字母顺序排列);
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 ;
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;
表t2将给出具有最小和最大字符串长度的所有记录以及行编号,现在基于行num过滤记录将获取所需的输出
以上是关于HackerRank天气观测站5的主要内容,如果未能解决你的问题,请参考以下文章
雷达回波基于matlab天气观测极化雷达回波仿真含Matlab源码 2252期