LeetCode-619. 只出现一次的最大数字(简单) having count

本文介绍了一种SQL查询技巧,如何从包含重复数字的表中找出只出现过一次且数值最大的数字。通过分组和计数操作,结合MAX函数,实现了高效的数据筛选。

表 my_numbers 的 num 字段包含很多数字,其中包括很多重复的数字。

你能写一个 SQL 查询语句,找到只出现过一次的数字中,最大的一个数字吗?

+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 | 
对于上面给出的样例数据,你的查询语句应该返回如下结果:

+---+
|num|
+---+
| 6 |

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/biggest-single-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

审题:查找只出过一次的最大的数字。

思考:查找只出现过一次的数字。按照分组,查找每个组只有一个数字的组,然后排序取第一个。

解题:

找出仅出过一次的最大数。

对数字分组,每组计数,选出计数等于1的行。

从多个数字再取最大值。

select max(A.num) as `num`
from 
(
    select M.num as `num`
    from my_numbers as M
    group by M.num
    having count(M.num)=1
) as A

有一种易错的方法。

select A.num
from 
(
    select M.num as `num`
    from my_numbers as M
    group by M.num
    having count(M.num)=1
    order by M.num desc
    limit 0,1
) as A

思路上是对的,但当 没有只出现一次的数字 时,表A为空但不是NULL。不符合题目的要求。

修改为下面即可。但这样的修改还不及上面的直接。

select max(A.num) as `num`
from 
(
    select M.num as `num`
    from my_numbers as M
    group by M.num
    having count(M.num)=1
    order by M.num desc
    limit 0,1
) as A

也可修改为如下。

应用IFNULL。当第一个参数不为NULL时,返回第一个参数,否则返回第二个参数。

SELECT IFNULL(
	(
		select M.num
		from my_numbers as M
		group by M.num
		having count(M.num)=1
		order by M.num desc
		limit 0,1
	)
, NULL
) AS `num`

由于本例中表只有一列。

上述代码还可简化为:

SELECT IFNULL(
	(
		SELECT *
		from my_numbers as M
		group by M.num
		having COUNT(*)=1
		order by M.num desc
		limit 0,1
	)
, NULL
) AS `num`

 

 

方法二:

方法:使用子查询 和 MAX() 函数【通过】

算法

使用子查询找出仅出现一次的数字。

SELECT
    num
FROM
    `number`
GROUP BY num
HAVING COUNT(num) = 1;

然后使用 MAX() 函数找出其中最大的一个。 

SELECT
    MAX(num) AS num
FROM
    (SELECT
        num
    FROM
        number
    GROUP BY num
    HAVING COUNT(num) = 1) AS t

知识点:

havaing count()  查询分组后数量

### LeetCode MySQL Problems and Solutions #### Problem 1: Sales Analysis III Given three tables `Product`, `Sales` with the following structure: | Column Name | Type | |-------------|----------| | product_id | int | | product_name| varchar | | Column Name | Type | |-------------|----------| | seller_id | int | | product_id | int | | buyer_id | int | | sale_date | date | | quantity | int | | price | int | The task is to find products that were only sold in a specific year. ```sql SELECT DISTINCT p.product_id, p.product_name FROM Product AS p JOIN Sales AS s ON p.product_id = s.product_id WHERE YEAR(s.sale_date) = '2018' AND p.product_id NOT IN ( SELECT product_id FROM Sales WHERE YEAR(sale_date) != '2018') ``` This query selects distinct product IDs and names from the `Product` table where sales occurred exclusively in 2018 by filtering out any products sold outside this period[^1]. #### Problem 2: Big Countries A world table contains columns like name, continent, area, population, gdp. The goal is to list all countries larger than 3 million square kilometers or having more than 25 million people. ```sql SELECT name, population, area FROM world WHERE area > 3000000 OR population > 25000000; ``` This SQL statement retrieves country information based on specified size criteria using logical operators[^2]. #### Problem 3: Duplicate Emails With a Person table containing id and email fields, identify duplicate emails within it. ```sql SELECT Email , COUNT(*) as num FROM Person GROUP BY Email HAVING COUNT(*) > 1; ``` By grouping entries according to their email addresses and applying HAVING clause, one can easily spot duplicates[^3]. --related questions-- 1. How does JOIN operation work between two tables? 2. What are common aggregate functions used alongside GROUP BY statements? 3. Can you explain how subqueries function inside main queries? 4. In what scenarios should window functions be preferred over traditional aggregation methods?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值