HackerRank 天气观测站 5
Posted
技术标签:
【中文标题】HackerRank 天气观测站 5【英文标题】:HackerRank Weather Observation Station 5 【发布时间】:2018-06-07 09:08:19 【问题描述】:我想知道为什么我的代码不起作用。这个问题之前在这里被问过: 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
但是这两个答案都不起作用。我已经粘贴了下面的问题,然后是我的解决方案。感谢您的帮助!
查询 STATION 中两个城市的 CITY 名称最短和最长,以及它们各自的长度(即:名称中的字符数)。如果有多个最小或最大的城市,请选择按字母顺序排列在前的城市。
输入格式
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;
【问题讨论】:
我想知道为什么我的答案是错误的,其次是解决问题的方法。 【参考方案1】:您在 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
这样的命令。根据数据库系统,它可以是 top
、limit
或 rownum
- 请在此处阅读更多信息 - https://www.w3schools.com/sql/sql_top.asp
因此,根据系统的不同,答案也会有所不同。
甲骨文
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 服务器
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)
【讨论】:
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;
但是错误说联合存在语法问题。我排除了工会,它工作得很好。你能纠正我在这里使用联合吗? @Anatolii
@ProteetiProva 你在哪个 SQL 系统上运行它?
hackerrank 上的 mysql
@ProteetiProva 我用你的固定解决方案编辑了我的答案。【参考方案2】:
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 */
【讨论】:
【参考方案3】: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 ;
table t2 将给出所有具有最小和最大字符串长度的记录以及一个行编号,现在根据行数过滤记录将获取您想要的输出
【讨论】:
【参考方案4】:使用以下查询:
(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);
【讨论】:
欢迎来到 Stack Overflow。虽然此代码可能会回答问题,但提供有关此代码回答问题的原因和/或方式的附加上下文会提高其长期价值。How to Answer【参考方案5】:我对这个问题的看法是:-
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;
【讨论】:
【参考方案6】:实际上,您的代码似乎是正确的。我认为唯一的问题可能是设置工作场所不是“MYSQL”。如果你在“MS SQL Server”上运行你的代码,它会给你一些“内置函数”的问题(比如在 mysql 上它的书面 lengt() 但在 ms sql server 上它的书面 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 ;
【讨论】:
【参考方案7】: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;
【讨论】:
虽然共享代码可能会回答问题,但添加一些上下文或解释通常很有用,并且可以提高答案的质量。【参考方案8】:您也可以将此查询用作稍有不同的答案。我在 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;
【讨论】:
【参考方案9】: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;
【讨论】:
【参考方案10】:使用 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;
【讨论】:
虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。【参考方案11】:关注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)
关注 cmets 以获得更好的解释- AS 关键字- 别名 ASC/DESC - 升序/降序 LIMIT 函数 - 限制查询输出 联合函数 - 聚合结果
【讨论】:
【参考方案12】:select city,lens from (select city,Length(city) 'lens' from station order by lens asc,city asc )as zz limit 1;
select city,lens from (select city,Length(city) 'lens' from station order by lens desc ,city asc )as zz limit 1;
这对我有用,希望能有所帮助。
【讨论】:
虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。 How to Answer【参考方案13】:对于甲骨文 ==> 我使用子查询解决了它并在子查询中使用了“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);
【讨论】:
【参考方案14】:对于 Oracle - 使用 Dense Rank:
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;
【讨论】:
【参考方案15】:(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 中有效。
【讨论】:
【参考方案16】:对于甲骨文
选择城市,长度(城市) 从(选择城市,长度(城市) 从车站 按城市分组 按长度排序(城市),城市) 其中rownum=1;
选择城市,长度(城市) 从(选择城市,长度(城市) 从车站 按城市分组 按长度排序(城市) DESC,城市) 其中rownum=1;
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。【参考方案17】:另一种使用 SQL Server 的单一查询解决方案:
SELECT TOP 1*
FROM (SELECT DISTINCT TOP 1 CITY, LEN(CITY) C1
FROM STATION
ORDER BY LEN(CITY) ASC, CITY ASC) ONE
GROUP BY CITY, LEN(CITY), C1
HAVING LEN(CITY) = MIN(LEN(CITY))
UNION
SELECT TOP 1*
FROM (SELECT DISTINCT TOP 1 CITY, LEN(CITY) C2
FROM STATION
ORDER BY LEN(CITY) DESC, CITY DESC) TWO
GROUP BY CITY, LEN(CITY), C2
HAVING LEN(CITY) = MAX(LEN(CITY))
【讨论】:
【参考方案18】:选择城市,长度(城市) 从车站 按长度排序(城市)ASC ,城市 ASC 限制 1;
选择城市,长度(城市) 从车站 按长度排序(城市)DESC,CITY ASC 限制 1;
【讨论】:
已经发布了相同的答案。 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。以上是关于HackerRank 天气观测站 5的主要内容,如果未能解决你的问题,请参考以下文章
雷达回波基于matlab天气观测极化雷达回波仿真含Matlab源码 2252期