简体   繁体   English

如何选择一列具有最大值的行

[英]How to select rows that have a maximum value for a column

I'm doing this challenge on Hackerrank: 我正在Hackerrank上进行以下挑战:

https://www.hackerrank.com/challenges/weather-observation-station-5 https://www.hackerrank.com/challenges/weather-observation-station-5

I'm a beginner on SQL and I'm trying to query all the rows that have a maximum value for a column, maximum that I can only obtain via MAX() . 我是SQL的初学者,我正在尝试查询所有具有列最大值的行,这些最大值只能通过MAX()获得。 So I'm trying this: 所以我正在尝试:

SELECT CITY, LENGTH(CITY) AS citylength
FROM STATION
WHERE LENGTH(CITY) = (SELECT MIN(CITY) FROM STATION)

and I get errors. 我得到错误。

I've looked up on google about sub-queries but I'm not accustomed enough to know exactly how it works, so I need your help guys.Thanks. 我已经在Google上查询了有关子查询的信息,但是我不习惯于确切地了解它的工作方式,因此我需要您的帮助。谢谢。

So to sum up, I need a query that can get the rows on a table that has a maximum value obtained via MAX() clause. 因此,总而言之,我需要一个查询,该查询可以获取具有通过MAX()子句获得的最大值的表上的行。

You are requested to find two different results: 要求您找到两个不同的结果:

  • The city with maximum length (and the first in the alphabet in case of a tie) 最大长度的城市(如果打成平局,则是字母表中的第一个)
  • The city with minimum length (and the first in the alphabet in case of a tie) 最小长度的城市(如果出现平局,则为字母表中的首位)

This means two different queries, which you glue together with UNION ALL. 这意味着您需要将两个查询与UNION ALL粘合在一起。

(
  select concat(city, ' ', length(city))
  from station 
  order by length(city), city limit 1
)
union all
(
  select concat(city, ' ', length(city))
  from station 
  order by length(city) desc, city limit 1
);

As Strawberry pointed out: You need the parentheses in order to place two ORDER BY clauses, one per query part. 正如Strawberry指出的那样:您需要使用括号才能放置两个ORDER BY子句,每个查询部分一个。 (Otherwise you can only place one ORDER BY clause at the end for the whole query.) (否则,您只能在整个查询的末尾放置一个ORDER BY子句。)

In your query you are comparing LENGTH(CITY) , ie an integer holding the name's length and MIN(CITY) , ie the city name itself, which cannot work of course. 在您的查询中,您正在比较LENGTH(CITY) (即保存名称长度的整数LENGTH(CITY)MIN(CITY) (即城市名称本身),这当然不起作用。 You would have to compare with MIN(LENGTH(CITY)) . 您将不得不与MIN(LENGTH(CITY))进行比较。 Then do the same for the maximum and then use UNION ALL. 然后最大程度地执行相同的操作,然后使用UNION ALL。 This doesn't solve the problem with ties, however, which the LIMIT query does. 但是,这不能解决领带问题,而LIMIT查询可以解决。

This works without sub-queries 无需子查询即可工作

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

This works : 这有效:

SELECT CITY,LENGTH(CITY) 
FROM STATION  
WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) M FROM STATION);

This works correctly: 这可以正常工作:

select city, length(city) 
from station 
where length(city) = (select min(length(city)) as m from station)
order by city ASC
limit 1;
(SELECT CONCAT(city,' ',LENGTH(city)) city, 'shortest' category FROM stations ORDER BY LENGTH(city),city LIMIT 1)
UNION ALL
(SELECT CONCAT(city,' ',LENGTH(city)), 'longest' FROM stations ORDER BY LENGTH(city) DESC,city LIMIT 1);
select TOP 1 concat(city, ' ' , len(city)) from station where len(city) = (select min(len(city)) from station) order by city asc;

select TOP 1 concat(city, ' ' , len(city)) from station where len(city) = (select max(len(city)) from station) order by city desc;
SELECT * FROM
    (SELECT City, LENGTH(City) FROM STATION 
    ORDER BY LENGTH(City),city ASC) 
WHERE ROWNUM <= 1
UNION ALL
SELECT * FROM 
    (SELECT City, LENGTH(City) FROM STATION 
    ORDER BY LENGTH(City) DESC) 
WHERE ROWNUM <= 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM