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

天塌了!!!SQL竟也可以做预测分析?| 商品零售额的预测

 目录

0 问题背景

1  数据准备

2 问题解决

2.1 模型构建

(1)符号规定

(2)基本假设

(3)模型的分析与建立

2.2 模型求解

3 小结


0 问题背景

1960年—1985年全国社会商品零售额如图1 所示

表1全国社会商品零售额数据

年份

1960

1961

1962

1963

1964

1965

1966

1967

零售总额

696.6

607.7

604

604.5

638.2

670.3

732.8

770.5

年份

1968

1969

1970

1971

1972

1973

1974

1975

零售总额

737.3

801.5

858

929.2

1023.3

1106.7

1163.6

1271.1

年份

1976

1977

1978

1979

1980

1981

1982

 

零售总额

1339.4

1432.8

1558.6

1800

2140

2350

2570

 

问题:试用三次指数平滑法预测1983年和1985年全国社会商品零售额?

1  数据准备

create table sale_amount as			
select '1960' years, '696.6' sale_amount from dual union all
select '1961' years, '607.7' sale_amount from dual union all
select '1962' years, '604'   sale_amount from dual union all
select '1963' years, '604.5' sale_amount from dual union all
select '1964' years, '638.2' sale_amount from dual union all
select '1965' years, '670.3' sale_amount from dual union all
select '1966' years, '732.8' sale_amount from dual union all
select '1967' years, '770.5' sale_amount from dual union all
select '1968' years, '737.3' sale_amount from dual union all
select '1969' years, '801.5' sale_amount from dual union all
select '1970' years, '858'   sale_amount from dual union all
select '1971' years, '929.2'  sale_amount from dual union all
select '1972' years, '1023.3' sale_amount from dual union all
select '1973' years, '1106.7' sale_amount from dual union all
select '1974' years, '1163.6' sale_amount from dual union all
select '1975' years, '1271.1' sale_amount from dual union all
select '1976' years, '1339.4' sale_amount from dual union all
select '1977' years, '1432.8' sale_amount from dual union all
select '1978' years, '1558.6' sale_amount from dual union all
select '1979' years, '1800' sale_amount from dual union all
select '1980' years, '2140' sale_amount from dual union all
select '1981' years, '2350' sale_amount from dual union all
select '1982' years, '2570' sale_amount from dual 

2 问题解决

2.1 模型构建

(1)符号规定

8b51702d48b540998f42a442f25039e3.png

(2)基本假设

  1. 假设本问题考虑全社会商品零售额数据;
  2. 假设本问题只考虑销售,不考虑其余因素
  3. 假设本问题只考虑销售额总额,不考虑其余分支

 (3)模型的分析与建立

令加权系数eq?%5Calpha%20%3D0.3,则计算公式为

88710b97a2fc4ec38bed4fbf36f660ee.png

其中,eq?%7BS_%7Bt%7D%7D%5E%7B%281%29%7D 表示一次指数的平滑值;eq?%7BS_%7Bt%7D%7D%5E%7B%282%29%7D表示二次次指数的平滑值;eq?%7BS_%7Bt%7D%7D%5E%7B%283%29%7D表示三次指数的平滑值。初始值为

549168110e5e46c2847c1be480b61939.png

三次指数平滑法的预测模型为:

968015e4b7274349b558e998979f52d3.png

其中,

6aa277235f4e478ca5ee9ae99efebf3f.png

2.2 模型求解

步骤1:计算初始值

select years, sale_amount, last_value(init_sale_amount ignore nulls) over (order by YEARS) init_sale_amount, rn
from (select years, sale_amount, casewhen rn = 1 then cast(avg(sale_amount)over (order by years rows between current row and 2 following ) as decimal(18, 1)) end init_sale_amount, rnfrom (select years, sale_amount, row_number() over (order by years) rnfrom sale_amount) t) t

 6f14ba3d2c664ec79c9ce5e44bdef38a.png

 步骤2 :计算一次平滑值

with init as (select years, sale_amount, last_value(init_sale_amount ignore nulls) over (order by YEARS) init_sale_amount, rnfrom (select years, sale_amount, casewhen rn = 1 then cast(avg(sale_amount)over (order by years rows between current row and 2 following ) as decimal(18, 1)) end init_sale_amount, rnfrom (select years, sale_amount, row_number() over (order by years) rnfrom sale_amount) t) t
)
--计算一次平滑值, s1 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, cast(sum(case when t2.rn <= t1.rn then t2.sale_amount * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s1_p3from init t1,init t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn
)
select * from s1 order by  years;

5f718c7007ab42e489d5fb59fc880e2c.png

步骤3:计算二次平滑值

with init as (select years, sale_amount, last_value(init_sale_amount ignore nulls) over (order by YEARS) init_sale_amount, rnfrom (select years, sale_amount, casewhen rn = 1 then cast(avg(sale_amount)over (order by years rows between current row and 2 following ) as decimal(18, 1)) end init_sale_amount, rnfrom (select years, sale_amount, row_number() over (order by years) rnfrom sale_amount) t) t
)
--计算一次平滑值, s1 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, cast(sum(case when t2.rn <= t1.rn then t2.sale_amount * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s1_p3from init t1,init t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn
)
--计算二次平滑值
, s2 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3, cast(sum(case when t2.rn <= t1.rn then t2.s1_p3 * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s2_p3from s1 t1,s1 t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3
)
select * from s2 order by  years;

1efc2aae81da426b8ed4f0d095a28c36.png

步骤4:计算三次平滑值


with init as (select years, sale_amount, last_value(init_sale_amount ignore nulls) over (order by YEARS) init_sale_amount, rnfrom (select years, sale_amount, casewhen rn = 1 then cast(avg(sale_amount)over (order by years rows between current row and 2 following ) as decimal(18, 1)) end init_sale_amount, rnfrom (select years, sale_amount, row_number() over (order by years) rnfrom sale_amount) t) t
)
--计算一次平滑值, s1 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, cast(sum(case when t2.rn <= t1.rn then t2.sale_amount * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s1_p3from init t1,init t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn
)
--计算二次平滑值
, s2 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3, cast(sum(case when t2.rn <= t1.rn then t2.s1_p3 * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s2_p3from s1 t1,s1 t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3
)--计算三次平滑值
,s3 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s2_p3, cast(sum(case when t2.rn <= t1.rn then t2.s2_p3 * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s3_p3from s2 t1,s2 t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s2_p3
)
select * from s3 order by  years;

50e519fdf9a44699832e7874478042a4.png

步骤4:计算二次函数模型系数


with init as (select years, sale_amount, last_value(init_sale_amount ignore nulls) over (order by YEARS) init_sale_amount, rnfrom (select years, sale_amount, casewhen rn = 1 then cast(avg(sale_amount)over (order by years rows between current row and 2 following ) as decimal(18, 1)) end init_sale_amount, rnfrom (select years, sale_amount, row_number() over (order by years) rnfrom sale_amount) t) t
)
--计算一次平滑值, s1 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, cast(sum(case when t2.rn <= t1.rn then t2.sale_amount * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s1_p3from init t1,init t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn
)
--计算二次平滑值
, s2 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3, cast(sum(case when t2.rn <= t1.rn then t2.s1_p3 * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s2_p3from s1 t1,s1 t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3
)--计算三次平滑值
,s3 as (select t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3, t1.s2_p3, cast(sum(case when t2.rn <= t1.rn then t2.s2_p3 * power(0.7, t1.rn - t2.rn) else 0 end) * 0.3 +power(0.7, t1.rn) * t1.init_sale_amount as decimal(18, 4)) s3_p3from s2 t1,s2 t2group by t1.years, t1.sale_amount, t1.init_sale_amount, t1.rn, t1.s1_p3, t1.s2_p3
)--计算二次趋势模型系数
select years, sale_amount, init_sale_amount, rn, s1_p3, s2_p3, s3_p3, cast(case when rk=1 then 3*s1_p3 - 3*s2_p3 + s3_p3 else 0 end as decimal(18,4)) a_p3, cast(case when rk=1 then ((6-5*0.3)*s1_p3 - 2*(5-4*0.3)*s2_p3 + (4-3*0.3)*s3_p3 ) * 0.3/(2*power(0.7,2))  else 0 end as decimal(18,2))  b_p3, cast(case when rk=1 then (s1_p3 - 2*s2_p3 + s3_p3 ) * power(0.3,2)/(2*power(0.7,2))  else 0 end as decimal(18,4))  c_p3
from (select years, sale_amount, init_sale_amount, rn, s1_p3, s2_p3, s3_p3, row_number() over (order by rn desc) rkfrom s3) t
order by years

9c8479c455b84a14b59e0f8e47d7585c.png

步骤5:构建二次预测模型,并预测结果值

由步骤4得知: 

a=2572.2607,b=259.3367,c=8.9818

则预测模型为:

eq?%5Cwidehat%7By%7D%20%3D%208.9818m%5E2%20&plus;%20259.3367m%20&plus;%202572.2607

最后求得1983,1985年销售额的预测值分别是2840.5792亿元,3431.107亿元

3 小结

本文针对商品零售额采用三次指数平滑法构建预测模型,文中选取加权系数eq?%5Calpha%20%3D0.3 求解模型,并利用SQL语言进行实现,若实际中有相关需求,可针对加权系数再进行优化,利用RMSE均方根误差来使模型达到最优。

257aaa3a4e954ae18f302e8e5bf34df2.png

如果您觉得本文还不错,对你有帮助,那么不妨可以关注一下我的数字化建设实践之路专栏,这里的内容会更精彩。

专栏 原价99,现在活动价59.9,按照阶梯式增长,还差5个人上升到69.9,最终恢复到原价

专栏优势:
(1)一次收费持续更新。

(2)实战中总结的SQL技巧,帮助SQLBOY 在SQL语言上有质的飞越,无论你应对业务难题及面试都会游刃有余【全网唯一讲SQL实战技巧,方法独特

SQL很简单,可你却写不好?每天一点点,收获不止一点点-CSDN博客 

(3)实战中数仓建模技巧总结,让你认识不一样的数仓。【数据建模+业务建模,不一样的认知体系】(如果只懂数据建模而不懂业务建模,数仓体系认知是不全面的)

(4)数字化建设当中遇到难题解决思路及问题思考。

我的专栏具体链接如下:

数字化建设通关指南_莫叫石榴姐的博客-CSDN博客 

相关文章:

天塌了!!!SQL竟也可以做预测分析?| 商品零售额的预测

目录 0 问题背景 1 数据准备 2 问题解决 2.1 模型构建 &#xff08;1&#xff09;符号规定 &#xff08;2&#xff09;基本假设 &#xff08;3&#xff09;模型的分析与建立 2.2 模型求解 3 小结 0 问题背景 1960年—1985年全国社会商品零售额如图1 所示 表1全国社…...

VSCode本地C/C++环境配置

基本环境下载 1.我的系统是windows&#xff0c;自己先下载安装VSCode&#xff0c;网上视频实在太多&#xff0c;我建议跟着B站视频操作。 2.下载安装好后你需要明白&#xff1a;VSCode只是一个编辑工具&#xff0c;我们要写C/C代码得编译运行&#xff0c;所以我们要配置它在w…...

【智能算法应用】淘金优化算法求解二维路径规划问题

摘要 本文基于智能算法的淘金优化算法&#xff08;Gold Panning Optimization, GPO&#xff09;求解二维路径规划问题。该算法模拟淘金过程中个体寻找最优金矿路径的行为&#xff0c;利用适应度函数优化路径规划&#xff0c;能够在复杂环境下实现从起点到目标点的最优路径搜索…...

Linux挖矿病毒(kswapd0进程使cpu爆满)

一、摘要 事情起因:有台测试服务器很久没用了&#xff0c;突然监控到CPU飙到了95以上&#xff0c;并且阿里云服务器厂商还发送了通知消息&#xff0c;【阿里云】尊敬的xxh: 经检测您的阿里云服务&#xff08;ECS实例&#xff09;i-xxx存在挖矿活动。因此很明确服务器中挖矿病毒…...

【java】ArrayList与LinkedList的区别

目录 1. 说明2. 内部实现2.1 ArrayList2.2 LinkedList 3. 性能特点3.1 插入和删除操作3.2 访问操作3.1 遍历操作 4. 使用场景5. 扩容机制6. 空间开销 1. 说明 1.Java中的ArrayList和LinkedList是两种常用的集合实现类&#xff0c;都属于Java集合框架的一部分&#xff0c;但它们…...

【LangChain系列6】【Agent模块详解】

目录 前言一、LangChain1-1、介绍1-2、LangChain抽象出来的核心模块1-3、特点1-4、langchain解决的一些行业痛点1-5、安装 二、Agent模块详解2-0、Agent核心思想——React介绍2-0-1、React的介绍以及由来2-0-2、伪代码介绍React的执行顺序 2-1、Agent介绍2-1、Self ask with se…...

JavaScript Cookie 与 服务器生成的 Cookie 的区别与应用

JavaScript Cookie 与 服务器生成的 Cookie 的区别与应用 Cookie是一种甜点&#xff0c;同时也是web前端开发中一种非常常见且重要的技术&#xff0c;它用于在客户端和服务器之间存储和传递信息。用户身份验证、会话管理&#xff0c;还是用户个性化设置&#xff0c;都离不开Coo…...

深入了解Git、GitHub、GitLab及其应用技巧

在现代软件开发中&#xff0c;掌握版本控制系统&#xff08;VCS&#xff09;是至关重要的&#xff0c;其中Git是最流行的分布式版本控制工具之一。本文将详细介绍Git的用途及其基本操作&#xff0c;并深入探讨GitLab、GitHub、和Git Desktop的使用方法&#xff0c;同时总结Git的…...

ctfshow(316,317,318)--XSS漏洞--反射性XSS

反射型XSS相关知识 Web316 进入界面&#xff1a; 审计 显示是关于反射性XSS的题目。 思路 首先想到利用XSS平台解题&#xff0c;看其他师傅的wp提示flag是在cookie中。 当前页面的cookie是flagyou%20are%20not%20admin%20no%20flag。 但是这里我使用XSS平台&#xff0c;…...

Visual Studio2022版本的下载与安装

1-首先打开微软的官网&#xff0c;下面就是链接 下载 Visual Studio Tools - 免费安装 Windows、Mac、Linux免费下载 Visual Studio IDE 或 VS Code。 在 Windows、Mac 上试用 Visual Studio Professional 或企业版。https://visualstudio.microsoft.com/zh-hans/downloads/?…...

nodeJS程序如何引入依赖包

在 Node.js 运行时中引入依赖包通常通过以下步骤完成&#xff1a; 初始化项目&#xff1a; 首先&#xff0c;你需要初始化一个 Node.js 项目。如果你还没有 package.json 文件&#xff0c;可以使用 npm init 命令来创建它。运行以下命令并按提示输入相关信息&#xff1a; npm i…...

建网站怎么建?只需几个步骤

在这个网络飞速发展的时代&#xff0c;越来越多的人都渴望拥有自己的网站。然而&#xff0c;对于大多数新手来说&#xff0c;如何建立自己的网站可能充满了挑战。本文将为您详细介绍建网站的关键步骤&#xff0c;让您能够轻松搭建自己的网站。 选择适合的建站工具 虽然市面上有…...

机器学习课程总结(个人向)

前言 通过看课件PPT整理的笔记&#xff0c;没有截图 由于大部分内容已经耳熟能详了&#xff0c;故记录比较简略&#xff0c;只记录了一些概念和需要记忆的地方。 里面有较多的个人观点&#xff0c;未必正确。如有错误&#xff0c;还请各位大佬指正 正文 绪论 机器学习的定…...

数据分析-43-时间序列预测之深度学习方法GRU

文章目录 1 时间序列1.1 时间序列特点1.1.1 原始信号1.1.2 趋势1.1.3 季节性和周期性1.1.4 噪声1.2 时间序列预测方法1.2.1 统计方法1.2.2 机器学习方法1.2.3 深度学习方法2 GRU2.1 模拟数据2.2 数据归一化2.3 生成滞后特征2.4 切分训练集和测试集2.5 模型训练2.6 模型预测3 参…...

Pandas | 数据分析时将特定列转换为数字类型 float64 或 int64的方法

类型转换 传统方法astype使用value_counts统计通过apply替换并使用astype转换 pd.to_numericx对连续变量进行转化⭐参数&#xff1a;返回值&#xff1a;示例代码&#xff1a; isnull不会检查空字符串 数据准备 有一组数据信息如下&#xff0c;其中主要将TotalCharges、MonthlyC…...

Elasticsearch的自定义查询方法到底是啥?

Elasticsearch主要的目的就是查询&#xff0c;默认提供的查询方法是查询全部&#xff0c;不满足我们的需求&#xff0c;可以定义查询方法 自定义查询方法 单条件查询 我们查询的需求&#xff1a;从title中查询所有包含"鼠标"这个分词的商品数据 SELECT * FROM it…...

Jenkins找不到maven构建项目

有的可能没有出现maven这个选项 解决办法&#xff1a;需要安装Maven项目插件 输入​Maven Integration plugin​...

怎么更换IP地址 改变IP归属地的三种方法

要更换自己的IP地址&#xff0c;您可以按照以下步骤进行操作&#xff1a; 1. 了解IP地址类型&#xff1a;首先&#xff0c;您需要了解您当前使用的IP地址类型。IP地址分为静态IP和动态IP两种。静态IP地址是固定的&#xff0c;使用第三方软件比如S深度IP转换器&#xff1b;而使用…...

C#-异步查询示例

文章速览 CancellationTokenSource 概述代码示例 坚持记录实属不易&#xff0c;希望友善多金的码友能够随手点一个赞。 共同创建氛围更加良好的开发者社区&#xff01; 谢谢~ CancellationTokenSource 概述 使用System.Threading下的CancellationTokenSource类&#xff0c;进…...

设计模式之适配器模式(从多个MQ消息体中,抽取指定字段值场景)

前言 工作到3年左右很大一部分程序员都想提升自己的技术栈&#xff0c;开始尝试去阅读一些源码&#xff0c;例如Spring、Mybaits、Dubbo等&#xff0c;但读着读着发现越来越难懂&#xff0c;一会从这过来一会跑到那去。甚至怀疑自己技术太差&#xff0c;慢慢也就不愿意再触碰这…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址&#xff1a;pdf 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&#xff0c;若有发现欢迎评论指正&#xff01;文章偏向于笔记&#xff0c;谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序

一、开发环境准备 ​​工具安装​​&#xff1a; 下载安装DevEco Studio 4.0&#xff08;支持HarmonyOS 5&#xff09;配置HarmonyOS SDK 5.0确保Node.js版本≥14 ​​项目初始化​​&#xff1a; ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

MySQL中【正则表达式】用法

MySQL 中正则表达式通过 REGEXP 或 RLIKE 操作符实现&#xff08;两者等价&#xff09;&#xff0c;用于在 WHERE 子句中进行复杂的字符串模式匹配。以下是核心用法和示例&#xff1a; 一、基础语法 SELECT column_name FROM table_name WHERE column_name REGEXP pattern; …...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

CSS设置元素的宽度根据其内容自动调整

width: fit-content 是 CSS 中的一个属性值&#xff0c;用于设置元素的宽度根据其内容自动调整&#xff0c;确保宽度刚好容纳内容而不会超出。 效果对比 默认情况&#xff08;width: auto&#xff09;&#xff1a; 块级元素&#xff08;如 <div>&#xff09;会占满父容器…...

push [特殊字符] present

push &#x1f19a; present 前言present和dismiss特点代码演示 push和pop特点代码演示 前言 在 iOS 开发中&#xff0c;push 和 present 是两种不同的视图控制器切换方式&#xff0c;它们有着显著的区别。 present和dismiss 特点 在当前控制器上方新建视图层级需要手动调用…...

wpf在image控件上快速显示内存图像

wpf在image控件上快速显示内存图像https://www.cnblogs.com/haodafeng/p/10431387.html 如果你在寻找能够快速在image控件刷新大图像&#xff08;比如分辨率3000*3000的图像&#xff09;的办法&#xff0c;尤其是想把内存中的裸数据&#xff08;只有图像的数据&#xff0c;不包…...

Kafka主题运维全指南:从基础配置到故障处理

#作者&#xff1a;张桐瑞 文章目录 主题日常管理1. 修改主题分区。2. 修改主题级别参数。3. 变更副本数。4. 修改主题限速。5.主题分区迁移。6. 常见主题错误处理常见错误1&#xff1a;主题删除失败。常见错误2&#xff1a;__consumer_offsets占用太多的磁盘。 主题日常管理 …...