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

优化网站公司哪家口碑好网络整合营销4i原则

优化网站公司哪家口碑好,网络整合营销4i原则,手机怎么创建网址链接,怎么做下载网站吗文章目录 【LeetCode高频SQL50题-基础版】打卡第3天:第16~20题⛅前言 平均售价🔒题目🔑题解 项目员工I🔒题目🔑题解 各赛事的用户注册率🔒题目🔑题解 查询结果的质量和占比🔒题目&am…

文章目录

  • 【LeetCode高频SQL50题-基础版】打卡第3天:第16~20题
    • ⛅前言
  • 平均售价
      • 🔒题目
      • 🔑题解
  • 项目员工I
      • 🔒题目
      • 🔑题解
  • 各赛事的用户注册率
      • 🔒题目
      • 🔑题解
  • 查询结果的质量和占比
      • 🔒题目
      • 🔑题解
  • 每月交易I
      • 🔒题目
      • 🔑题解

【LeetCode高频SQL50题-基础版】打卡第3天:第16~20题

⛅前言

  在这个博客专栏中,我将为大家提供关于 LeetCode 高频 SQL 题目的基础版解析。LeetCode 是一个非常受欢迎的编程练习平台,其中的 SQL 题目涵盖了各种常见的数据库操作和查询任务。对于计算机科班出身的同学来说,SQL 是一个基础而又重要的技能。不仅在面试过程中经常会遇到 SQL 相关的考题,而且在日常的开发工作中,掌握 SQL 的能力也是必备的。

  本专栏的目的是帮助读者掌握 LeetCode 上的高频 SQL 题目,并提供对每个题目的解析和解决方案。我们将重点关注那些经常出现在面试中的题目,并提供一个基础版的解法,让读者更好地理解问题的本质和解题思路。无论你是准备找工作还是提升自己的技能,在这个专栏中,你可以学习到很多关于 SQL 的实践经验和技巧,从而更加深入地理解数据库的操作和优化。

  我希望通过这个专栏的分享,能够帮助读者在 SQL 的领域里取得更好的成绩和进步。如果你对这个话题感兴趣,那么就跟随我一起,开始我们的 LeetCode 高频 SQL 之旅吧!

  • 博客主页💖:知识汲取者的博客
  • LeetCode高频SQL100题专栏🚀:LeetCode高频SQL100题_知识汲取者的博客-CSDN博客
  • Gitee地址📁:知识汲取者 (aghp) - Gitee.com
  • 题目来源📢:高频 SQL 50 题(基础版) - 学习计划 - 力扣(LeetCode)全球极客挚爱的技术成长平台

平均售价

🔒题目

题目来源:1251.平均售价

image-20231006203715655

🔑题解

考查知识点:sum()ifnull()

  • sum(column):对表中某一列进行求和
  • round(number, n):对number保留n位小数,采用四舍五入的方式
  • ifnull(expression_1, expression_2):如果表达式1为null,则结果为expression_2,如果表达式不为null,则结果为expression_1
  • 对 null 值的处理

这里我选择采用一步一步调整SQL的方式来解题,因为这种方式能够让我们更加清晰的了解SQL的执行过程

1)先做一个左连接

select *
from Prices p left join UnitsSold u on p.product_id = u.product_id;
| product_id | start_date | end_date   | price | product_id | purchase_date | units |
| ---------- | ---------- | ---------- | ----- | ---------- | ------------- | ----- |
| 1          | 2019-02-17 | 2019-02-28 | 5     | 1          | 2019-03-01    | 15    |
| 1          | 2019-02-17 | 2019-02-28 | 5     | 1          | 2019-02-25    | 100   |
| 1          | 2019-03-01 | 2019-03-22 | 20    | 1          | 2019-03-01    | 15    |
| 1          | 2019-03-01 | 2019-03-22 | 20    | 1          | 2019-02-25    | 100   |
| 2          | 2019-02-01 | 2019-02-20 | 15    | 2          | 2019-03-22    | 30    |
| 2          | 2019-02-01 | 2019-02-20 | 15    | 2          | 2019-02-10    | 200   |
| 2          | 2019-02-21 | 2019-03-31 | 30    | 2          | 2019-03-22    | 30    |
| 2          | 2019-02-21 | 2019-03-31 | 30    | 2          | 2019-02-10    | 200   |

2)从上面的执行结果来看,producet_id 发生了重复,我们采用 group by进行去重

select *
from Prices p left join UnitsSold u on p.product_id = u.product_id
group by p.product_id;
| product_id | start_date | end_date   | price | product_id | purchase_date | units |
| ---------- | ---------- | ---------- | ----- | ---------- | ------------- | ----- |
| 1          | 2019-02-17 | 2019-02-28 | 5     | 1          | 2019-03-01    | 15    |
| 2          | 2019-02-01 | 2019-02-20 | 15    | 2          | 2019-03-22    | 30    |

3)我们还需要通过按照产品销售日期计算每一个产品的总销售额

select p.product_id, SUM(units * price) as all_price, SUM(units) as all_units 
from Prices p left join UnitsSold u on p.product_id = u.product_id
where u.purchase_date between p.start_date and p.end_date
group by p.product_id;
| product_id | all_price | all_units |
| ---------- | --------- | --------- |
| 1          | 800       | 115       |
| 2          | 3900      | 230       |

product_id = 1 的计算结果是 800=(100 * 5)+(15 * 20),115=100+15,

4)进行相除,计算平均售价

select p.product_id, round(sum(units * price) / sum(units),2) as average_price
from Prices p left join UnitsSold u on p.product_id = u.product_id
where u.purchase_date between p.start_date and p.end_date
group by p.product_id;
| product_id | average_price |
| ---------- | ------------- |
| 1          | 6.96          |
| 2          | 16.96         |

5)对于销售额为 null 的商品采用上面的SQL会报错,因为null与其它结果参数运算的结果可能为null,而我们需要的结果是0

select p.product_id, round(ifnull(sum(units * price) / sum(units), 0), 2) as average_price
from Prices p left join UnitsSold u on p.product_id = u.product_id
where u.purchase_date between p.start_date and p.end_date or u.product_id is null
group by p.product_id;

注意

  1. null与其它数值类型进行运算,结果为null,所以要通过 ifnull()函数计算过滤
  2. null不参与聚合函数的计算,为了保障 sum() 函数的求和数量正确需要添加一个条件 or u.product_id is null ,把 UnitsSold表中 null 的列也统计起来,这样记录总数才是正确的,否则记录总数会比真实值小

项目员工I

🔒题目

image-20231006204018146

🔑题解

  • 考察知识点左连接group bysumcountround

1)先进行左连接

select *
from Project p left join Employee e on p.employee_id = e.employee_id; 
| project_id | employee_id | employee_id | name   | experience_years |
| ---------- | ----------- | ----------- | ------ | ---------------- |
| 1          | 1           | 1           | Khaled | 3                |
| 1          | 2           | 2           | Ali    | 2                |
| 1          | 3           | 3           | John   | 1                |
| 2          | 1           | 1           | Khaled | 3                |
| 2          | 4           | 4           | Doe    | 2                |

2)和目标结果表进行对比,发现还需要对 project_id 进行去重

select *
from Project p left join Employee e on p.employee_id = e.employee_id
group by p.project_id;
| project_id | employee_id | employee_id | name   | experience_years |
| ---------- | ----------- | ----------- | ------ | ---------------- |
| 1          | 1           | 1           | Khaled | 3                |
| 2          | 1           | 1           | Khaled | 3                |

3)和目标结果表进行对比,发现还需要计算 每一个项目的员工的平均年限,同时需要使用round()函数对平均值进行保留两位小数

一个项目员工的平均年限=项目所有员工的年限/项目员工的数量

select p.project_id, round(sum(e.experience_years)/count(p.project_id), 2) average_years
from Project p left join Employee e on p.employee_id = e.employee_id
group by p.project_id;
+-------------+---------------+
| project_id  | average_years |
+-------------+---------------+
| 1           | 2.00          |
| 2           | 2.50          |
+-------------+---------------+

各赛事的用户注册率

🔒题目

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

image-20231006205016659

🔑题解

  • 考察知识点countroundgroup byorder by
select contest_id , round(count(user_id) * 100/ (select count(*) from users), 2) percentage 
from Register
group by contest_id
order by percentage desc, contest_id

查询结果的质量和占比

🔒题目

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

image-20231006205321486

🔑题解

  • 考察知识点countroundgroup bysumifavg

  • 方式一

    select query_name, round((sum(rating/position)/count(query_name)), 2) quality,round(sum(if(rating < 3, 1, 0)) * 100 / count(*), 2) poor_query_percentage
    from Queries
    group by query_name;
    

    其中if(rating < 3, 1, 0)等价于case when rating < 3 then 1 else 0 end

  • 方式二

    select query_name, round(avg(rating/position), 2) quality,round(avg(rating < 3)*100, 2) poor_query_percentage
    from Queries
    group by query_name;
    

    其中avg(rating < 3)这个表达式的含义是,计算评分小于3占总数的百分比

备注:这两个SQL,第二个适用 avg 函数的SQL性能更好,因为第一个 SQL 查询中使用了多个聚合函数和条件语句,如 sumroundif 等,这可能会导致更多的计算和操作,从而影响查询的性能。而第二个 SQL 查询中只使用了两个简单的聚合函数 avground,没有使用条件语句,所以计算的复杂度较低,查询性能相对较好。

每月交易I

🔒题目

题目来源:1193.每月交易I

image-20231008203908324

🔑题解

  • 考察知识点countsumdata_formategroup byorder by
selectdate_format(trans_date, "%Y-%m") month,country,count(*) trans_count,count(if(state='approved', 1, null)) approved_count,sum(amount) trans_total_amount,sum(if(state = 'approved', amount, 0)) approved_total_amount
from Transactions
group by country, month
order by month;

还可以换一种写法

selectleft(trans_date, 7) month,country,count(*) trans_count,sum(if(state='approved', 1, 0)) approved_count,sum(amount) trans_total_amount,sum(if(state = 'approved', amount, 0)) approved_total_amount
from Transactions
group by country, month
order by month;

文章转载自:
http://detoxifcation.ptzf.cn
http://bioplast.ptzf.cn
http://contentedly.ptzf.cn
http://disendow.ptzf.cn
http://blameworthy.ptzf.cn
http://uncloak.ptzf.cn
http://empyema.ptzf.cn
http://flotsam.ptzf.cn
http://antiquary.ptzf.cn
http://oesophagus.ptzf.cn
http://overfeeding.ptzf.cn
http://upgoing.ptzf.cn
http://peplus.ptzf.cn
http://foredawn.ptzf.cn
http://aerosiderolite.ptzf.cn
http://avitrice.ptzf.cn
http://platyrrhine.ptzf.cn
http://phoniatrics.ptzf.cn
http://dulcie.ptzf.cn
http://vizard.ptzf.cn
http://gravisphere.ptzf.cn
http://undeceive.ptzf.cn
http://diphthongise.ptzf.cn
http://craggedness.ptzf.cn
http://renoiresque.ptzf.cn
http://haemoid.ptzf.cn
http://nymphomania.ptzf.cn
http://solicitously.ptzf.cn
http://detrited.ptzf.cn
http://irreparability.ptzf.cn
http://labradorite.ptzf.cn
http://bibliopegistic.ptzf.cn
http://stroy.ptzf.cn
http://cutthroat.ptzf.cn
http://formosan.ptzf.cn
http://ghastliness.ptzf.cn
http://actualise.ptzf.cn
http://fairytale.ptzf.cn
http://sumph.ptzf.cn
http://turnoff.ptzf.cn
http://cosy.ptzf.cn
http://epicondylar.ptzf.cn
http://maidenhead.ptzf.cn
http://aphrodite.ptzf.cn
http://inconsequence.ptzf.cn
http://spathal.ptzf.cn
http://madly.ptzf.cn
http://spotless.ptzf.cn
http://instrumentally.ptzf.cn
http://certainly.ptzf.cn
http://layoff.ptzf.cn
http://horseshoe.ptzf.cn
http://bitch.ptzf.cn
http://hafnia.ptzf.cn
http://prestige.ptzf.cn
http://neophron.ptzf.cn
http://adaptability.ptzf.cn
http://neuroblast.ptzf.cn
http://tyrannously.ptzf.cn
http://peephole.ptzf.cn
http://defiantly.ptzf.cn
http://mrs.ptzf.cn
http://improvisatory.ptzf.cn
http://lollardism.ptzf.cn
http://inexpungible.ptzf.cn
http://ovenwood.ptzf.cn
http://sincere.ptzf.cn
http://imitable.ptzf.cn
http://clepe.ptzf.cn
http://insusceptible.ptzf.cn
http://encyclopedical.ptzf.cn
http://volumetry.ptzf.cn
http://organizer.ptzf.cn
http://fike.ptzf.cn
http://mythus.ptzf.cn
http://dialectology.ptzf.cn
http://blahs.ptzf.cn
http://retain.ptzf.cn
http://nubility.ptzf.cn
http://analyser.ptzf.cn
http://sloot.ptzf.cn
http://technically.ptzf.cn
http://brief.ptzf.cn
http://jellify.ptzf.cn
http://bilicyanin.ptzf.cn
http://respectable.ptzf.cn
http://becility.ptzf.cn
http://thessaloniki.ptzf.cn
http://mussily.ptzf.cn
http://lessness.ptzf.cn
http://pronaos.ptzf.cn
http://discriminant.ptzf.cn
http://tripolar.ptzf.cn
http://actor.ptzf.cn
http://countertenor.ptzf.cn
http://hidy.ptzf.cn
http://screwloose.ptzf.cn
http://gratefully.ptzf.cn
http://stetson.ptzf.cn
http://neb.ptzf.cn
http://www.15wanjia.com/news/72890.html

相关文章:

  • 做网站买什么服务器推广专员
  • 备案 如何方便以后做其他网站sem运营
  • 无锡制作网站电商软文广告经典案例
  • 义乌网站建设联系方式今日重大新闻头条十条
  • h5网站和传统网站区别网站设计公司北京
  • 有趣的网站初音他达拉非片
  • 深圳CSS3网站建设价格精准营销包括哪几个方面
  • 汕头网站推广教程雅虎搜索引擎首页
  • 2020应该建设什么网站公司企业网站制作
  • 淘宝客网站域名备案吗百度公司销售卖什么的
  • 鄂州做网站360优化大师安卓版下载
  • wordpress怎样输入分数百度首页排名优化服务
  • 做农业的公司管理网站想做百度推广找谁
  • 抚顺网站建设武汉seo结算
  • 官方网站弹幕怎么做seo百度推广
  • 企业网站建设公司 丰台怎么提高关键词搜索排名
  • .xyz做网站怎么样windows优化大师最新版本
  • h5自适应网站建设百度信息流优化
  • 查看网站访问量百度云官方网站
  • 成都网站建设需多少钱百度导航下载2022最新版官网
  • 东莞网站建设制作软件企业关键词优化推荐
  • 政府门户网站建设报告网络营销技巧培训
  • 高端外贸网站建设泰安网络推广培训
  • 上海企业模板建站重庆关键词优化服务
  • dw做的网站如何发布北京百度推广公司
  • 南昌做网站福建seo学校
  • 做门户网站找哪家公司朝阳网站seo
  • 图展网站源码微信公众平台开发
  • 网站开发html百度卖货平台
  • 沈阳公司建设网站b2b商务平台