LeetCode 高频 SQL 50 题(基础版)之 【聚合函数】部分

题目:620. 有趣的电影



题解:

sql 复制代码
select * from cinema
where description !='boring' and id%2=1
order by rating desc

题目:1251. 平均售价




题解:

sql 复制代码
select p.product_id product_id,round(ifnull(sum(p.price*u.units)/sum(u.units),0),2) average_price 
from Prices p left join UnitsSold u
on p.product_id=u.product_id
where (p.start_date<=u.purchase_date and u.purchase_date<=p.end_date) or u.units is null
group by p.product_id

题目:1075. 项目员工 I




题解:

sql 复制代码
select p.project_id project_id, round(avg(e.experience_years),2) average_years from Project p,Employee e
where p.employee_id=e.employee_id
group by p.project_id

题目:1633. 各赛事的用户注册率




题解:

sql 复制代码
select contest_id,round(count(user_id)*100/(select count(*) from Users),2) as percentage
from Register r 
group by contest_id
order by percentage desc,contest_id asc

题目:1211. 查询结果的质量和占比



题解:

sql 复制代码
select query_name,round(avg(rating/position),2) quality,
round(100*avg(rating<3),2) poor_query_percentage 
from Queries
group by query_name

题目:1193. 每月交易 I



题解:

sql 复制代码
select  left(trans_date,7) month,country,
count(*) trans_count,
sum(amount) approved_count,
sum(if(state='approved',1,0)) trans_total_amount,
sum(if(state='approved',amount,0)) approved_total_amount
from Transactions
group by month,country

题目:1174. 即时食物配送 II



题目:

sql 复制代码
select round(100*avg(order_date=customer_pref_delivery_date),2) immediate_percentage 
from Delivery
where (customer_id,order_date) in(
    select customer_id,min(order_date) 
    from Delivery
    group by customer_id
)

题目:550. 游戏玩法分析 IV



题解:

sql 复制代码
select round(avg(a2.event_date is not null),2) fraction from (
    select player_id,min(event_date) event_date from Activity
    group by player_id) a1 left join Activity a2
on a1.player_id=a2.player_id and datediff(a2.event_date,a1.event_date)=1
相关推荐
YuTaoShao10 分钟前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
古月居GYH2 小时前
【数据分析】如何在PyCharm中高效配置和使用SQL
ide·sql·pycharm
JoJo_Way5 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
计算机毕设定制辅导-无忧学长7 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
凌肖战7 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
程序员柳7 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
梦在深巷、8 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机8 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10248 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
GEEK零零七9 小时前
Leetcode 1103. 分糖果 II
数学·算法·leetcode·等差数列