板凳-------Mysql cookbook学习 (十一--------9)

13.2 分组描述统计

mysql> select age, count(score) as n,-> sum(score) as sum,-> min(score) as minimum,-> max(score) as maximum,-> avg(score) as mean,-> stddev_samp(score) as 'std. dev.',-> var_samp(score) as 'variance'-> from testscore-> group by age;
+-----+---+------+---------+---------+--------+--------------------+--------------------+
| age | n | sum  | minimum | maximum | mean   | std. dev.          | variance           |
+-----+---+------+---------+---------+--------+--------------------+--------------------+
|   5 | 4 |   22 |       4 |       7 | 5.5000 | 1.2909944487358056 | 1.6666666666666667 |
|   6 | 4 |   27 |       4 |       9 | 6.7500 |  2.217355782608345 |  4.916666666666667 |
|   7 | 4 |   30 |       6 |       9 | 7.5000 | 1.2909944487358056 | 1.6666666666666667 |
|   8 | 4 |   32 |       6 |      10 | 8.0000 | 1.8257418583505538 | 3.3333333333333335 |
|   9 | 4 |   35 |       7 |      10 | 8.7500 | 1.2583057392117918 |  1.583333333333334 |
+-----+---+------+---------+---------+--------+--------------------+--------------------+
5 rows in set (0.03 sec)mysql> select sex, count(score) as n,-> sum(score) as sum,-> min(score) as minimum,-> max(score) as maximum,-> avg(score) as mean,-> stddev_samp(score) as 'std. dev.',-> var_samp(score) as 'variance'-> from testscore-> group by sex;
+-----+----+------+---------+---------+--------+--------------------+--------------------+
| sex | n  | sum  | minimum | maximum | mean   | std. dev.          | variance           |
+-----+----+------+---------+---------+--------+--------------------+--------------------+
| M   | 10 |   71 |       4 |       9 | 7.1000 | 1.7919573407620817 | 3.2111111111111112 |
| F   | 10 |   75 |       4 |      10 | 7.5000 | 1.9578900207451218 | 3.8333333333333335 |
+-----+----+------+---------+---------+--------+--------------------+--------------------+
2 rows in set (0.00 sec)mysql> select age, sex,  count(score) as n,-> sum(score) as sum,-> min(score) as minimum,-> max(score) as maximum,-> avg(score) as mean,-> stddev_samp(score) as 'std. dev.',-> var_samp(score) as 'variance'-> from testscore-> group by age, sex;
+-----+-----+---+------+---------+---------+--------+--------------------+----------+
| age | sex | n | sum  | minimum | maximum | mean   | std. dev.          | variance |
+-----+-----+---+------+---------+---------+--------+--------------------+----------+
|   5 | M   | 2 |    9 |       4 |       5 | 4.5000 | 0.7071067811865476 |      0.5 |
|   5 | F   | 2 |   13 |       6 |       7 | 6.5000 | 0.7071067811865476 |      0.5 |
|   6 | M   | 2 |   17 |       8 |       9 | 8.5000 | 0.7071067811865476 |      0.5 |
|   6 | F   | 2 |   10 |       4 |       6 | 5.0000 | 1.4142135623730951 |        2 |
|   7 | M   | 2 |   14 |       6 |       8 | 7.0000 | 1.4142135623730951 |        2 |
|   7 | F   | 2 |   16 |       7 |       9 | 8.0000 | 1.4142135623730951 |        2 |
|   8 | M   | 2 |   15 |       6 |       9 | 7.5000 | 2.1213203435596424 |      4.5 |
|   8 | F   | 2 |   17 |       7 |      10 | 8.5000 | 2.1213203435596424 |      4.5 |
|   9 | M   | 2 |   16 |       7 |       9 | 8.0000 | 1.4142135623730951 |        2 |
|   9 | F   | 2 |   19 |       9 |      10 | 9.5000 | 0.7071067811865476 |      0.5 |
+-----+-----+---+------+---------+---------+--------+--------------------+----------+
10 rows in set (0.00 sec)

13.3 产生频率分布

mysql> select score, count(score) as occurence-> from testscore group by score;
+-------+-----------+
| score | occurence |
+-------+-----------+
|     5 |         1 |
|     4 |         2 |
|     6 |         4 |
|     7 |         4 |
|     8 |         2 |
|     9 |         5 |
|    10 |         2 |
+-------+-----------+
7 rows in set (0.00 sec)mysql> select @n := count(score) from  testscore;
+--------------------+
| @n := count(score) |
+--------------------+
|                 20 |
+--------------------+
1 row in set, 1 warning (0.01 sec)mysql> select score, (count(score) * 100)/@n as percent-> from testscore group by score;
+-------+---------+
| score | percent |
+-------+---------+
|     5 |  5.0000 |
|     4 | 10.0000 |
|     6 | 20.0000 |
|     7 | 20.0000 |
|     8 | 10.0000 |
|     9 | 25.0000 |
|    10 | 10.0000 |
+-------+---------+
7 rows in set (0.00 sec)mysql> select score, repeat('*', count(score)) as occurrences-> from testscore group by score;
+-------+-------------+
| score | occurrences |
+-------+-------------+
|     5 | *           |
|     4 | **          |
|     6 | ****        |
|     7 | ****        |
|     8 | **          |
|     9 | *****       |
|    10 | **          |
+-------+-------------+
7 rows in set (0.00 sec)mysql> select @n := count(score) from  testscore;
+--------------------+
| @n := count(score) |
+--------------------+
|                 20 |
+--------------------+
1 row in set, 1 warning (0.00 sec)mysql> select score, repeat('*', (count(score)*100)/@n) as percent-> from testscore group by score;
+-------+---------------------------+
| score | percent                   |
+-------+---------------------------+
|     5 | *****                     |
|     4 | **********                |
|     6 | ********************      |
|     7 | ********************      |
|     8 | **********                |
|     9 | ************************* |
|    10 | **********                |
+-------+---------------------------+
7 rows in set (0.00 sec)mysql> drop table if exists ref;
Query OK, 0 rows affected (0.03 sec)mysql> create table ref(score int);
Query OK, 0 rows affected (0.04 sec)mysql> insert into ref(score)-> values(0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
Query OK, 11 rows affected (0.02 sec)
Records: 11  Duplicates: 0  Warnings: 0mysql> select ref.score, count(testscore.score) as occurences-> from ref left join testscore on ref.score = testscore.score-> group by ref.score;
+-------+------------+
| score | occurences |
+-------+------------+
|     0 |          0 |
|     1 |          0 |
|     2 |          0 |
|     3 |          0 |
|     4 |          2 |
|     5 |          1 |
|     6 |          4 |
|     7 |          4 |
|     8 |          2 |
|     9 |          5 |
|    10 |          2 |
+-------+------------+
11 rows in set (0.00 sec)mysql> select ref.score, (count(testscore.score)*100)/@n as percent-> from ref left join testscore on ref.score = testscore.score-> group by ref.score;
+-------+---------+
| score | percent |
+-------+---------+
|     0 |  0.0000 |
|     1 |  0.0000 |
|     2 |  0.0000 |
|     3 |  0.0000 |
|     4 | 10.0000 |
|     5 |  5.0000 |
|     6 | 20.0000 |
|     7 | 20.0000 |
|     8 | 10.0000 |
|     9 | 25.0000 |
|    10 | 10.0000 |
+-------+---------+
11 rows in set (0.00 sec)

13.4 计数缺失值

mysql> -- 创建表
mysql> CREATE TABLE subject_scores (->   subject INT,->   score INT NULL-> );
Query OK, 0 rows affected (0.06 sec)mysql>
mysql> -- 插入示例数据
mysql> INSERT INTO subject_scores VALUES->   (1, 38), (2, NULL), (3, 47),->   (4, 82), (5, NULL), (6, 65),->   (7, 90), (8, 73), (9, NULL),->   (10, 55), (11, 68), (12, 79);
Query OK, 12 rows affected (0.01 sec)
Records: 12  Duplicates: 0  Warnings: 0mysql>
mysql> -- 查询
mysql> SELECT * FROM subject_scores ORDER BY subject;
+---------+-------+
| subject | score |
+---------+-------+
|       1 |    38 |
|       2 |  NULL |
|       3 |    47 |
|       4 |    82 |
|       5 |  NULL |
|       6 |    65 |
|       7 |    90 |
|       8 |    73 |
|       9 |  NULL |
|      10 |    55 |
|      11 |    68 |
|      12 |    79 |
+---------+-------+
12 rows in set (0.00 sec)mysql> SELECT->   COUNT(*) AS 'n (total)',->   COUNT(score) AS 'n (nonmissing)',->   COUNT(*) - COUNT(score) AS 'n (missing)',->   ((COUNT(*) - COUNT(score)) * 100) / COUNT(*) AS '% missing'-> FROM subject_scores;
+-----------+----------------+-------------+-----------+
| n (total) | n (nonmissing) | n (missing) | % missing |
+-----------+----------------+-------------+-----------+
|        12 |              9 |           3 |   25.0000 |
+-----------+----------------+-------------+-----------+
1 row in set (0.00 sec)mysql> select count(*) as 'n (total)',-> count(score) as  'n (nonmissing)',-> sum(isnull(score)) as 'n (missing)',-> (sum(isnull(score))* 100) / count(*) as '% missing'-> from subject_scores;
+-----------+----------------+-------------+-----------+
| n (total) | n (nonmissing) | n (missing) | % missing |
+-----------+----------------+-------------+-----------+
|        12 |              9 |           3 |   25.0000 |
+-----------+----------------+-------------+-----------+
1 row in set (0.00 sec)如果您确实需要按某些条件分组:
需要先确定分组依据。例如,如果:
•	科目1-6是A组
•	科目7-12是B组
可以这样写:
sql
mysql> SELECT->   CASE WHEN subject BETWEEN 1 AND 6 THEN 'A' ELSE 'B' END AS group_name,->   COUNT(*) AS 'n (total)',->   COUNT(score) AS 'n (nonmissing)',->   COUNT(*) - COUNT(score) AS 'n (missing)',->   ((COUNT(*) - COUNT(score)) * 100) / COUNT(*) AS '% missing'-> FROM subject_scores-> GROUP BY group_name;
+------------+-----------+----------------+-------------+-----------+
| group_name | n (total) | n (nonmissing) | n (missing) | % missing |
+------------+-----------+----------------+-------------+-----------+
| A          |         6 |              4 |           2 |   33.3333 |
| B          |         6 |              5 |           1 |   16.6667 |
+------------+-----------+----------------+-------------+-----------+
2 rows in set (0.00 sec)

13.5 计算线性回归和相关系数

mysql> select age, score from testscore;
+-----+-------+
| age | score |
+-----+-------+
|   5 |     5 |
|   5 |     4 |
|   5 |     6 |
|   5 |     7 |
|   6 |     8 |
|   6 |     9 |
|   6 |     4 |
|   6 |     6 |
|   7 |     8 |
|   7 |     6 |
|   7 |     9 |
|   7 |     7 |
|   8 |     9 |
|   8 |     6 |
|   8 |     7 |
|   8 |    10 |
|   9 |     9 |
|   9 |     7 |
|   9 |    10 |
|   9 |     9 |
+-----+-------+
20 rows in set (0.00 sec)mysql> SELECT->   @n := COUNT(score) AS n,->   @meanx := AVG(age) AS 'x mean',->   @sumx := SUM(age) AS 'x sum',->   @sumxx := SUM(age * age) AS 'x sum of squares',->   @meany := AVG(score) AS 'y mean',->   @sumy := SUM(score) AS 'y sum',->   @sumyy := SUM(score * score) AS 'y sum of squares',->   @sumxy := SUM(age * score) AS 'x*y sum'-> FROM testscore\G
*************************** 1. row ***************************n: 20x mean: 7.0000x sum: 140
x sum of squares: 1020y mean: 7.3000y sum: 146
y sum of squares: 1130x*y sum: 1053
1 row in set, 8 warnings (0.00 sec)mysql> select-> @b := (@n * @sumxy - @sumx * @sumy)/ (@n * @sumxx - @sumx * @sumx)-> as slope;
+-------------+
| slope       |
+-------------+
| 0.775000000 |
+-------------+
1 row in set, 1 warning (0.00 sec)mysql> select @a := (@meany - @b * @meanx) as intercept;
+----------------------+
| intercept            |
+----------------------+
| 1.875000000000000000 |
+----------------------+
1 row in set, 1 warning (0.00 sec)mysql> select concat('y =', @b, 'x + ', @a) as 'least-squares regression';
+----------------------------------------+
| least-squares regression               |
+----------------------------------------+
| y =0.775000000x + 1.875000000000000000 |
+----------------------------------------+
1 row in set (0.00 sec)mysql> select-> (@n * @sumxy - @sumx * @sumy)-> /sqrt((@n * @sumxx - @sumx * @sumx) * (@n * @sumyy - @sumy * @sumy ))-> as correlation;
+--------------------+
| correlation        |
+--------------------+
| 0.6117362044219903 |
+--------------------+
1 row in set (0.00 sec)

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

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

相关文章

编写产品需求文档:黄历日历小程序

整理产品需求文档:黄历日历小程序版本:1.0 更新时间:2025-7-9一、文档概述1.1 产品背景开发一款融合传统黄历文化的日历工具,提供每日吉凶查询、神煞展示和个人运势分析功能。1.2 目标用户关注传统历法的中老年群体婚嫁/搬家等需要…

Spring Boot + MyBatis 实现用户登录功能详解(基础)

一、项目概述做了几个项目发现有人问到怎么使用springbootHTMLjsCSS开发一个项目呢所以本文将介绍如何使用Spring Boot和MyBatis实现一个完整的用户登录功能。系统包含前端登录页面、后端控制器、服务层、数据访问层以及数据库交互。二、技术栈Spring Boot 2.xMyBatis 持久层框…

adb 简介与常用命令

1. adb 简介adb 的全称为 Android Debug Bridge,就是起到调试桥的作用。借助 adb 工具,我们可以管理设备或手机模拟器的状态。还可以进行很多手机操作,如安装软件、系统升级、运行 shell 命令等等。其实简而言说,adb 就是连接 And…

阿里云-跨账号同步OSS Bucket

说明 阿里云A账号的OSS BUCKET同步到B账号的指定OSS BUCKET。 账号Bucket NamesRAM角色A{源buctket}OSS-SYNCERB{目标buctket} 步骤 在阿里云A,B账号分别建上表buckets, 最好是相同地域的在A号-RAM控制台建立角色OSS-SYNCER,并赋权AliyunOSSFullAccess&#xff…

uniapp小程序无感刷新token

request.js // request.js import {getApptoken,getStoredApptoken } from ./tokenRequest // 从合并模块导入// 全局配置 const MAX_RETRIES 1 // 最大重试次数 const baseURL https://your-api.com// 请求队列和刷新状态 let requestsQueue [] let isRefreshing false// …

MySQL优化高手笔记

语雀完整版:https://www.yuque.com/g/mingrun/embiys/dv3btw/collaborator/join?tokenzMBwPzSMfSGINLuv&sourcedoc_collaborator# 《MySQL优化高手笔记》MySQL优化高手一、MySQL架构01 天天写CRUD,你知道你的系统是如何跟MySQL打交道的吗通过驱动连接数据库&am…

Git 详解:从概念,常用命令,版本回退到工作流

本文将从 Git 的核心概念讲起,详细介绍常用命令、各阶段版本回退、分支控制以及企业内常见的 Git 工作流。 Git 与 GitHub 简介 Git 简介 Git 是一个开源的分布式版本控制系统,由 Linus Torvalds 于 2005 年开发。它与集中式版本控制系统(…

CMSIS(Cortex Microcontroller Software Interface Standard)ARM公司为 Cortex-M 系列处理器

CMSIS(Cortex Microcontroller Software Interface Standard)是ARM公司为 Cortex-M 系列处理器(如 M0/M3/M4/M7/M23/M33 等)定义的一套硬件抽象层标准,旨在简化嵌入式开发,提高代码的可移植性和复用性。 核…

[特殊字符] 扫描式处理:Python 自动提取 PDF 中关键词相关表格并导出为 Excel

本文演示如何利用 pdfplumber 批量处理指定文件夹下 PDF 文档:定位关键词(如“主要会计数据”)出现的页码及下一页,提取其中的表格并保存为独立 Excel 文件。适用于财务报告、审计表格、统计报表等场景。 1️⃣ 第一步&#xff1a…

python3的返回值能返回多个吗?

在Python中,函数可以通过返回一个元组(tuple) 来间接实现返回多个值的效果。以下是具体说明: 实现方式:直接返回逗号分隔的值 Python会自动将这些值打包成一个元组: def multiple_return():a 1b "he…

UE5 Secondary Materials

首先放入材质A材质B放入Secondary Materials两个效果就能融合到一起了动态设置secondary material

AUTOSAR进阶图解==>AUTOSAR_SWS_FlashTest

AUTOSAR Flash Test模块详解与分析 基于AUTOSAR标准的Flash Test模块架构、功能与应用分析目录 1. Flash Test模块概述 1.1 模块作用与功能1.2 适用范围 2. Flash Test模块架构 2.1 模块位置2.2 组件关系 3. 状态管理 3.1 状态定义3.2 状态转换 4. 后台测试执行流程 4.1 测试间…

msf复现永恒之蓝

永恒之蓝(EternalBlue)是利用 Windows 系统的 SMB 协议漏洞(MS17-010)来获取系统最高权限的漏洞,利用 Metasploit 框架(MSF)复现该漏洞是一个复杂且具有一定风险的操作,必须在合法合…

格密码--LWE,DLWE和ss-LWE

格密码–LWE,DLWE和ss-LWE 0.数学符号数学符号含义备注Zq\mathbb{Z}_qZq​模qqq的整数集合,即{0,1,2,...,q−1}\{0,1,2,...,q-1\}{0,1,2,...,q−1}用于定义LWE、DLWE、ss-LWE等问题中矩阵和向量的元素取值范围,是基础整数环x∈RSx \in_R Sx∈…

【闭包】前端的“保护神”——闭包详解+底层原理

目录 一、闭包是什么?概念 二、闭包为什么存在?作用 1. 创建私有变量 2. 实现数据封装与信息隐藏 3. 模拟私有方法 4. 保存函数执行时的状态 5. 回调函数和事件处理 6. 模块化编程 7. 懒加载与延迟执行 三、闭包怎么用?实践业务场景 …

算法学习笔记:19.牛顿迭代法——从原理到实战,涵盖 LeetCode 与考研 408 例题

牛顿迭代法(Newtons Method)是一种强大的数值计算方法,由英国数学家艾萨克・牛顿提出。它通过不断迭代逼近方程的根,具有收敛速度快、适用范围广的特点,在科学计算、工程模拟、计算机图形学等领域有着广泛应用。牛顿迭…

小白学Python,操作文件和文件夹

目录 前言 一、操作文件路径 1.获取当前路径 2.创建文件夹 (1)mkdir()函数 (2)makedirs() 函数 3.拼接路径 4.跳转路径 5.判断相对路径和绝对路径 6.获取文件路径和文件名 二、操作文件和文件夹 1.查询文件大小 2.删除…

015_引用功能与信息溯源

引用功能与信息溯源 目录 引用功能概述支持的模型引用类型API使用方法引用格式应用场景最佳实践 引用功能概述 什么是引用功能 Claude的引用功能允许在回答基于文档的问题时提供详细的信息来源引用,帮助用户追踪和验证信息的准确性。这个功能特别适用于需要高可…

ROS2中的QoS(Quality of Service)详解

ROS2中的QoS(Quality of Service)详解1. 主要QoS参数2. 为什么需要设置QoS3. QoS兼容性规则4. 选择QoS策略的建议5. 调试QoS问题的方法6. 踩坑:订阅话题没有输出可能的原因:调试方法QoS是ROS2中用于控制通信质量和行为的机制。它定…

Cursor三大核心AI功能

一:Tab键:智能小助手 1.1 单行/多行代码补全 在代码中写出要实现的功能,第一次按Tab生成代码,第二次按Tab接受代码。1.2 智能代码重写 对已有代码重新编写。 写个注释告诉AI重构方法,然后鼠标点到方法内部,…