当前位置: 首页 > news >正文

数据库相关算法题 V3

订单最多的客户

在考虑多个最多订单客户的情况下可以采用dense_rank()函数,最多则由group by customer_number以及order count(*)得到

select customer_number from (select customer_number,dense_rank() over (order by count(*) desc) as rk from Orders group by customer_number) t where rk = 1

https://leetcode.cn/problems/customer-placing-the-largest-number-of-orders/description/

买下所有产品的客户

买下所有产品意味着该客户买的所有不重复产品总数等于总产品数

select customer_id from Customer group by customer_id having count(distinct(product_key))=(select count(*) from Product)

https://leetcode.cn/problems/customers-who-bought-all-products/description/

计算首单为即时订单的用户数

最自然判断首单为即时订单应该就是先找到首单,然后判断首单是不是即时的。但这样就太麻烦了。更好的办法,已知期望配送日期一定不早于下单日期,那么只要用户的首单日期与最小的期望配送日期相等,那么这个首单就是即时订单

select round(avg(a)*100,2) as immediate_percentage from
(select customer_id, 	if(min(order_date)=min(customer_pref_delivery_date),1,0) 
as a from Delivery group by customer_id ) t

重新格式化部门表

select id,avg(case month when 'Jan' then revenue end) as Jan_Revenue,avg(case month when 'Feb' then revenue end) as Feb_Revenue,avg(case month when 'Mar' then revenue end) as Mar_Revenue,avg(case month when 'Apr' then revenue end) as Apr_Revenue,avg(case month when 'May' then revenue end) as May_Revenue,avg(case month when 'Jun' then revenue end) as Jun_Revenue,avg(case month when 'Jul' then revenue end) as Jul_Revenue,avg(case month when 'Aug' then revenue end) as Aug_Revenue,avg(case month when 'Sep' then revenue end) as Sep_Revenue,avg(case month when 'Oct' then revenue end) as Oct_Revenue,avg(case month when 'Nov' then revenue end) as Nov_Revenue,avg(case month when 'Dec' then revenue end) as Dec_Revenue
from Department
group by id

avg函数在本题中并没有特殊含义,只是用于聚合,防止本应该获取到数值,但得到了null

case when相当于编程语言的switch

https://leetcode.cn/problems/reformat-department-table/description/

每月交易1

我最初的方案原本如下,就是根据国家区域和月份分组聚合,但是忽略了在没有匹配数据的情况下sum()、count()会返回null

select 
DATE_FORMAT(trans_date, '%Y-%m') as month,
country, 
count(*) as trans_count, 
sum(case state when 'approved' then 1 end) as approved_count, 
sum(amount) as trans_total_amount, 
sum(case state when 'approved' then amount end) as approved_total_amount
from Transactions
group by concat(country, DATE_FORMAT(trans_date, '%Y-%m'))

那么只需要对count()、sum()做些小小改变,count()会忽略null,那么如果都是null,返回值也就是0了;sum()如果都是null,才会返回null,那么只要在无值的时候返回0就可以了

select 
DATE_FORMAT(trans_date, '%Y-%m') as month,
country, 
count(*) as trans_count, 
count(if(state = 'approved', 1, null)) as approved_count, 
sum(amount) as trans_total_amount, 
sum(if(state = 'approved', amount, 0)) as approved_total_amount
from Transactions
group by concat(country, DATE_FORMAT(trans_date, '%Y-%m'))

此外也可以根据month、group分组

SELECT DATE_FORMAT(trans_date, '%Y-%m') AS month,country,COUNT(*) AS trans_count,COUNT(IF(state = 'approved', 1, NULL)) AS approved_count,SUM(amount) AS trans_total_amount,SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
FROM Transactions
GROUP BY month, country

https://leetcode.cn/problems/monthly-transactions-i/

http://www.lryc.cn/news/252021.html

相关文章:

  • 第二证券:本周3只新股申购,大豆蛋白行业领军企业来了!
  • 【go语言开发】loglus日志框架的使用
  • 【Unity动画】Unity 动画播放的流程
  • 深度学习——第3章 Python程序设计语言(3.2 Python程序流程控制)
  • EasyExcel生成多sheet页的excel
  • 家用小型洗衣机哪款性价比高?内衣洗衣机品牌推荐
  • 为何Go爬虫依然远没有Python爬虫流行
  • 【华为OD题库-057】MELON的难题-java
  • OGG实现Oracle19C到postgreSQL14的实时同步
  • windows 你的电脑不能投影到其他屏幕,请尝试重新安装驱动程序
  • 2023-简单点-树莓派中的硬件通讯
  • 游戏反Frida注入检测方案
  • 观海微电子---AF、AG、AR 的差别和作用
  • 颠覆性语音识别:单词级时间戳和说话人分离
  • 吉利展厅 | 透明OLED拼接2x2:科技与艺术的完美融合
  • qnx修改tcp和udp缓冲区默认大小
  • vscode的eslint检查代码格式不严谨的快速修复
  • OpenAI GPT-4 Turbo发布:开创AI新时代
  • 基于c 实现 FIFO
  • tortoisegit 报错:server refused to start a shell/command
  • 电商平台API接口指南,京东商品详情接口,京东详情页接口,宝贝详情页接口,商品属性接口,商品信息查询,商品详细信息接口,h5实时详情页数据展示
  • 什么是迁移学习
  • 万宾科技水环境综合治理监测系统的融合与应用
  • 【EI会议征稿】第三届图像,信号处理与模式识别国际学术会议(ISPP 2024)
  • 继阿里云、滴滴、语雀后,腾讯视频也出现重大系统故障
  • kotlin中sealed语句的使用
  • 软信天成:数据泄露日趋严重 “资产”保护何去何从
  • GitHub打不开的解决方案(百试不爽法)
  • 一文入门Python面向对象编程(干货满满)
  • qiankun: 关于ElementUI字体图标加载不出来的问题