mysql 查询实战3-解答

news2024/12/23 16:09:54

        对mysql 查询实战3-题目,进行一个解答

11、查询每⽉产品交易与退款情况

        目标:查询每⽉产品交易(交易总额,交易数)与退款情况(退款总额,退款数)

1,先把日期格式化

使用 EXTRACT
SELECT  EXTRACT(YEAR_MONTH FROM t.trans_date) AS months FROM transactions t;
        这个不符合我们常见的年月格式,还是用DATE_FORMAT
使用DATE_FORMAT
SELECT id, product_id, amount, DATE_FORMAT(t.trans_date, '%Y-%m') AS months 
FROM transactions t;

2,统计成功的 ,加个flag标记, 0 成功, 1 失败 

SELECT  product_id, amount AS sum_amount, 
DATE_FORMAT(t.trans_date, '%Y-%m') AS months, 0 AS flag
FROM transactions t WHERE state = 'success' ;

3,统计失败的,加个flag标记, 0 成功, 1 失败

SELECT product_id, SUM(amount) AS sum_amount, 
DATE_FORMAT(r.trans_date, '%Y-%m') AS months, 1 AS flag
 FROM transactions t, refund r
WHERE t.id = r.trans_id
GROUP BY product_id, months;

4, 拼接到一起 

SELECT  product_id, amount AS sum_amount, 
DATE_FORMAT(t.trans_date, '%Y-%m') AS months, 0 AS flag
FROM transactions t WHERE state = 'success' 
UNION 
SELECT product_id, SUM(amount) AS sum_amount, 
DATE_FORMAT(r.trans_date, '%Y-%m') AS months, 1 AS flag
 FROM transactions t, refund r
WHERE t.id = r.trans_id
GROUP BY product_id, months;

5,再进行判断统计

 SELECT months, product_id,
SUM(CASE WHEN flag=0 THEN sum_amount ELSE 0 END) AS success_total,
COUNT(CASE WHEN flag=0 THEN 1 ELSE NULL END) AS success_count,
SUM(CASE WHEN flag=1 THEN sum_amount ELSE 0 END) AS fail_total,
COUNT(CASE WHEN flag=1 THEN 1 ELSE NULL END) AS fail_count
FROM(
SELECT  product_id, amount AS sum_amount, 
DATE_FORMAT(t.trans_date, '%Y-%m') AS months , 0 AS flag
FROM transactions t WHERE state = 'success' 
UNION ALL
SELECT product_id, SUM(amount) AS sum_amount, 
DATE_FORMAT(r.trans_date, '%Y-%m') AS months , 1 AS flag
 FROM transactions t, refund r
WHERE t.id = r.trans_id
GROUP BY product_id, months
) AS tmp GROUP BY product_id, months 
ORDER BY months,product_id;

12、查询活动产品的平均价格

1,关联查询

SELECT * FROM sold AS s,  activity AS a
WHERE s.product_id=a.product_id 
AND s.purchase_date BETWEEN a.start_date AND a.end_date;

2,进行统计: 计算平均值,总价格/总数量

SELECT s.product_id, SUM(s.num*a.price)/SUM(num) AS average_price 
FROM sold AS s,  activity AS a
WHERE s.product_id=a.product_id 
AND s.purchase_date BETWEEN a.start_date AND a.end_date 
GROUP BY a.product_id;

3,保留两位小数  

SELECT s.product_id, ROUND(SUM(s.num*a.price)/SUM(num),2) AS average_price 
FROM sold AS s,  activity AS a
WHERE s.product_id=a.product_id 
AND s.purchase_date BETWEEN a.start_date AND a.end_date 
GROUP BY a.product_id;

13、统计部⻔每⽉收⼊概况

1,先统计一月的情况 

SELECT department_id, 
SUM(CASE MONTH WHEN 'Jan' THEN income ELSE 0 END) Jan_income
FROM department_income
GROUP BY department_id;

2,再统计全年的

SELECT department_id,
SUM(CASE MONTH WHEN 'Jan' THEN income ELSE 0 END) Jan_income,
SUM(CASE MONTH WHEN 'Feb' THEN income ELSE 0 END) Feb_income,
SUM(CASE MONTH WHEN 'Mar' THEN income ELSE 0 END) Mar_income,
SUM(CASE MONTH WHEN 'Apr' THEN income ELSE 0 END) Apr_income,
SUM(CASE MONTH WHEN 'May' THEN income ELSE 0 END) May_income,
SUM(CASE MONTH WHEN 'Jun' THEN income ELSE 0 END) Jun_income,
SUM(CASE MONTH WHEN 'Jul' THEN income ELSE 0 END) Jul_income,
SUM(CASE MONTH WHEN 'Aug' THEN income ELSE 0 END) Aug_income,
SUM(CASE MONTH WHEN 'Sep' THEN income ELSE 0 END) Sep_income,
SUM(CASE MONTH WHEN 'Oct' THEN income ELSE 0 END) Oct_income,
SUM(CASE MONTH WHEN 'Nov' THEN income ELSE 0 END) Nov_income,
SUM(CASE MONTH WHEN 'Dec' THEN income ELSE 0 END) Dec_income
FROM department_income
GROUP BY department_id

3,改成用if的方式:

        IF(expr, vl, v2),如果表达式expr是TRUE(expr > 0 and expr  NULL),则IF()的返回值为v1;否则返回值为v2

SELECT department_id,
SUM(IF(MONTH = 'Jan' , income, 0)) Jan_income,
SUM(IF(MONTH = 'Feb' , income, 0)) Feb_income,
SUM(IF(MONTH = 'Mar' , income, 0)) Mar_income,
SUM(IF(MONTH = 'Apr' , income, 0)) Apr_income,
SUM(IF(MONTH = 'May' , income, 0)) May_income,
SUM(IF(MONTH = 'Jun' , income, 0)) Jun_income,
SUM(IF(MONTH = 'Jul' , income, 0)) Jul_income,
SUM(IF(MONTH = 'Aug' , income, 0)) Aug_income,
SUM(IF(MONTH = 'Sep' , income, 0)) Sep_income,
SUM(IF(MONTH = 'Oct' , income, 0)) Oct_income,
SUM(IF(MONTH = 'Nov' , income, 0)) Nov_income,
SUM(IF(MONTH = 'Dec' , income, 0)) Dec_income
FROM department_income
GROUP BY department_id;

14、统计课程新学员数量

1,找出人员在课程里面一开始的学习时间,即筛选出新学员

SELECT lr.user_id, lr.course_id, MIN(lr.created_at) start_study_date  
FROM learning_records lr
GROUP BY lr.user_id, lr.course_id;

2,加个时间条件,比如180天内容

SELECT * FROM (
SELECT lr.user_id, lr.course_id, MIN(lr.created_at) start_study_date  
FROM learning_records lr
GROUP BY lr.user_id, lr.course_id
) AS temp WHERE DATEDIFF('2020-06-01', start_study_date) <= 180;

3,再进行统计人数

 SELECT course_id, start_study_date, COUNT(user_id) FROM (
SELECT lr.user_id, lr.course_id, MIN(lr.created_at) start_study_date  
FROM learning_records lr
GROUP BY lr.user_id, lr.course_id
) AS temp WHERE DATEDIFF('2020-06-01', start_study_date) <= 180
GROUP BY start_study_date, course_id 
ORDER BY course_id, start_study_date;

15、平均销售额:部⻔与公司对⽐

        目标: 计算每月部门和公司的收入比较

1,日期格式化

SELECT id, sale_id, income, 
DATE_FORMAT(created_at,'%Y-%m') belong_month 
FROM income;
 

2,先计算公司的收入

SELECT  AVG(income) AS avg_income, 
DATE_FORMAT(created_at,'%Y-%m') AS belong_month 
FROM income
GROUP BY belong_month;

3,先计算部门的收入,关联人员表  

SELECT sp.department_id, AVG(income) AS avg_income, 
DATE_FORMAT(created_at,'%Y-%m') AS belong_month 
FROM income ic, sales_person sp
WHERE ic.sale_id = sp.id
GROUP BY belong_month, sp.department_id;

4,公司表和部门表,根据公司做一个关联查询 

SELECT d.department_id, c.belong_month,
d.avg_income AS department_avg_income, c.avg_income AS company_avg_income 
 FROM (SELECT  AVG(income) AS avg_income, 
DATE_FORMAT(created_at,'%Y-%m') AS belong_month 
FROM income
GROUP BY belong_month) AS c, (SELECT sp.department_id, AVG(income) AS avg_income, 
DATE_FORMAT(created_at,'%Y-%m') AS belong_month 
FROM income ic, sales_person sp
WHERE ic.sale_id = sp.id
GROUP BY belong_month, sp.department_id) AS d
WHERE c.belong_month = d.belong_month;

5,再加个标记,判断是高,还是低了

-- if只能判断二元的,这边得用case when
SELECT d.department_id, c.belong_month, 
(CASE WHEN d.avg_income > c.avg_income THEN  'higher'
WHEN d.avg_income < c.avg_income THEN 'lower' ELSE 'same' END) AS mark,
d.avg_income AS department_avg_income, c.avg_income AS company_avg_income 
 FROM (SELECT  AVG(income) AS avg_income, 
DATE_FORMAT(created_at,'%Y-%m') AS belong_month 
FROM income
GROUP BY belong_month) AS c,
(SELECT sp.department_id, AVG(income) AS avg_income, 
DATE_FORMAT(created_at,'%Y-%m') AS belong_month 
FROM income ic, sales_person sp
WHERE ic.sale_id = sp.id
GROUP BY belong_month, sp.department_id) AS d
WHERE c.belong_month = d.belong_month;

总结:

        在统计后,进行进一步过滤的时候,case when就非常好用了。if只能判断二元的,有限制。case when可以使用多元,可以统计各种情况,再聚合,把多列的,合并成单列数据,数据再进行处理就很方便了

                

        上一篇: 《mysql 查询实战3-题目》

        下一篇: 《》

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1597719.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

npm配置阿里镜像库

1、配置阿里云镜像源 #查看当前使用的镜像地址命令 npm config get registry#设置阿里镜像源 npm config set registry http://registry.npmmirror.com 这里要注意下&#xff0c;之前的镜像源地址 https://registry.npm.taobao.org/ 已经不能用了&#xff0c;这里要更改为新…

科大讯飞星火开源大模型iFlytekSpark-13B GPU版部署方法

星火大模型的主页&#xff1a;iFlytekSpark-13B: 讯飞星火开源-13B&#xff08;iFlytekSpark-13B&#xff09;拥有130亿参数&#xff0c;新一代认知大模型&#xff0c;一经发布&#xff0c;众多科研院所和高校便期待科大讯飞能够开源。 为了让大家使用的更加方便&#xff0c;科…

李沐37_微调——自学笔记

标注数据集很贵 网络架构 1.一般神经网络分为两块&#xff0c;一是特征抽取原始像素变成容易线性分割的特征&#xff0c;二是线性分类器来做分类 微调 1.原数据集不能直接使用&#xff0c;因为标号发生改变&#xff0c;通过微调可以仍然对我数据集做特征提取 2.pre-train源…

SQL12 获取每个部门中当前员工薪水最高的相关信息

题目&#xff1a;获取每个部门中当前员工薪水最高的相关信息 注意了&#xff0c;这道题目&#xff0c;分组函数只能查出来&#xff1a;每个部门的最高薪水&#xff0c;group by dept_no &#xff0c;根据部门分组&#xff0c;绝对不能group by dept_no,emp_no&#xff0c;不能…

el-tree如何修改节点点击颜色

el-tree修改点击节点颜色三大步 使用elementui库时&#xff0c;有时候我们会对里面提供的组件做一些样式修改。如果我们想要修改el-tree组件点击节点时的颜色&#xff0c;可以使用下面这种方式实现&#xff1a;

C++ | Leetcode C++题解之第29题两数相除

题目&#xff1a; 题解&#xff1a; class Solution { public:int divide(int dividend, int divisor) {// 考虑被除数为最小值的情况if (dividend INT_MIN) {if (divisor 1) {return INT_MIN;}if (divisor -1) {return INT_MAX;}}// 考虑除数为最小值的情况if (divisor I…

密码学基础 -- 走进RSA(2)(放弃数学原理版)

目录 1.概述 2. RSA测试 2.1 加解密实验 2.2 签名验签测试 3. RSA原理简介 4.小结 1.概述 从上面密码学基础 -- 走进RSA(1)(放弃数学原理版)-CSDN博客我们知道了非对称算法的密钥对使用时机&#xff0c;那么接下里我们继续讲解RSA&#xff0c;我们分别从RSA加解密、签名验…

【Unity】RPG小游戏创建游戏中的交互

RPG小游戏创建游戏中的交互 创建可交互的物体的公共的父类&#xff08;Interactable&#xff09;InteractableObject 类NPCObject 类PickableObject 类 创建可交互的物体的公共的父类&#xff08;Interactable&#xff09; InteractableObject 类 using System.Collections; u…

数字乡村创新实践推动农业现代化发展:科技赋能农业产业升级、提升农民收入水平与乡村治理效能

随着信息技术的迅猛发展和数字化转型的深入推进&#xff0c;数字乡村创新实践已成为推动农业现代化发展的重要引擎。数字技术的广泛应用不仅提升了农业生产的智能化水平&#xff0c;也带动了农民收入的增加和乡村治理的现代化。本文旨在探讨数字乡村创新实践如何科技赋能农业产…

61、ARM/串口通信相关学习20240415

一、串口通信&#xff1a;实现PC端串口助手与开发板的字符串通信。 代码&#xff1a; main&#xff1a; #include "uart4.h"int main(){uart4_config();//char a;char s[64];while (1){//a getchar();//putchar(a1);gets(s);puts(s);}return 0;}usrt4.c&#xff…

供应链投毒预警 | 开源供应链投毒202403月报发布啦!(含投毒案例分析)

悬镜供应链安全情报中心通过持续监测全网主流开源软件仓库&#xff0c;结合程序动静态分析方式对潜在风险的开源组件包进行动态跟踪和捕获&#xff0c;能够第一时间捕获开源组件仓库中的恶意投毒攻击。在2024年3月份&#xff0c;悬镜供应链安全情报中心在NPM官方仓库&#xff0…

golang 迷宫回溯算法(递归)

// Author sunwenbo // 2024/4/14 20:13 package mainimport "fmt"// 编程一个函数&#xff0c;完成老鼠找出路 // myMap *[8][7]int 地图&#xff0c;保证是同一个地图&#xff0c;因此是引用类型 // i,j表示对地图的哪个点进行测试 func SetWay(myMap *[8][7]int, …

学习一门语言的方法和套路(B站转述)

视频链接 up虽然长相英(ping)俊(ping)&#xff0c;但是讲的干活&#xff0c;没恰饭。 学习流程&#xff1a; 1.快速阅读&#xff0c;掌握概况 2.深入细节内容 例如&#xff1a;java (JDBC)、html 、netty 不管三七二十一&#xff0c;先了解套路&#xff0c;再深入研究。 高…

【华为】Telnet实验配置

【华为】Telnet 实验配置 应用场景三种认证方式配置注意事项拓扑无认证&#xff08;None&#xff09;交换机配置顺序Telnet ServerTelnet Client测试 密码认证&#xff08;Password&#xff09;配置顺序Telnet ServerTelnet Client测试 AAA认证&#xff08;scheme&#xff09;配…

密码学 | 椭圆曲线 ECC 密码学入门(四)

目录 正文 1 曲线方程 2 点的运算 3 求解过程 4 补充&#xff1a;有限域 ⚠️ 知乎&#xff1a;【密码专栏】动手计算双线性对&#xff08;中&#xff09; - 知乎 ⚠️ 写在前面&#xff1a;本文属搬运博客&#xff0c;自己留着学习。注意&#xff0c;这篇博客与前三…

验证ElasticSearch 分词的BUG

验证ElasticSearch 分词的BUG 环境介绍 ElasticSearch 版本号: 6.7.0 BUG 重现 创建测试案例索引 PUT test_2022 {"settings": {"analysis": {"filter": {"pinyin_filter": {"type": "pinyin"}},"analy…

万界星空科技商业开源MES+项目合作+低代码平台

今天我想和大家分享的是一套商业开源的 MES制造执行管理系统。对于制造业而言&#xff0c;MES 是一个至关重要的系统&#xff0c;它可以帮助企业提高生产效率、优化资源利用、提高产品质量&#xff0c;从而增强市场竞争力。什么是 MES&#xff1f; MES 是指通过计算机技术、自动…

uniapp开发 如何获取IP地址?

一、需求 使用uniapp开发小程序时&#xff0c;需要调取【记录日活动统计】的接口&#xff0c;而这个接口需要传递一个ip给后台&#xff0c; 那么前端如何获取ip呢&#xff1f;下面代码里可以实现 二、代码实现 1.在项目的manifest.json中配置一下网络权限&#xff1a; &quo…

企业网络日益突出的难题与SD-WAN解决方案

随着企业规模的迅速扩张和数字化转型的深入推进&#xff0c;企业在全球范围内需要实现总部、分支机构、门店、数据中心、云等地点的网络互联、数据传输和应用加速。SD-WAN作为当今主流解决方案&#xff0c;在网络效率、传输质量、灵活性和成本等方面远远超越传统的互联网、专线…

C语言之探秘:访问结构体空指针与结构体空指针的地址的区别(九十三)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…