SQL server 计算分月,分年,分季度的同环比

2023-12-20 19:36

本文主要是介绍SQL server 计算分月,分年,分季度的同环比,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SQL server 计算分月,分年,分季度的环比

--- dbo.ads_product_category_sale_money_yoy_mom_month-- Insert statements for procedure here-- 开始 TRY 块  BEGIN TRY  -- 执行可能引发异常的 SQL 语句  DELETE FROM dbo.ads_product_category_sale_money_yoy_mom_month WHERE dt = @DateThreshold;INSERT INTO dbo.ads_product_category_sale_money_yoy_mom_month ([dt],[ym],[ord],[title],[month_money],[previous_month_money],[mom_growth_rate])select dt,ym,product_sort1,product_menuname,month_money,previous_month_money,CASE WHEN previous_month_money = 0 THEN null ELSE ((month_money - previous_month_money) / previous_month_money) * 100 END AS mom_growth_ratefrom(select t2.dt,product_sort1,product_menuname,ym,t2.year,t2.month,t2.month_money,case when monthdiff = 1 then previous_month_money else 0 end  previous_month_moneyfrom(select dt,product_sort1,product_menuname,ym,SUBSTRING(ym, 1, 4) AS year,CAST(SUBSTRING(ym, 6, 2) AS INT) AS month,previous_month,month_product_order_sale_money as month_money,previous_month_money,DATEDIFF(MONTH, CAST(CONCAT(previous_month, '-01') AS DATE) , CAST(CONCAT(ym, '-01') AS DATE)) monthdifffrom(selectdt,product_sort1,product_menuname,ym,lag(ym, 1, '1970-01') over (partition by product_sort1 order by ym)  previous_month,lag(month_product_order_sale_money, 1, 0) over (partition by product_sort1 order by ym) previous_month_money,month_product_order_sale_moneyfromdws_product_category_sale_analyse_month where dt = @DateThreshold ) t1) t2) t3END TRY  -- 开始 CATCH 块  BEGIN CATCH  -- 捕捉异常并执行特定的操作  DECLARE @ErrorMessage51 NVARCHAR(4000);  SET @ErrorMessage51 = ERROR_MESSAGE();  RAISERROR (@ErrorMessage51, 16, 1);  -- 继续执行其他操作  -- ...  END CATCH--- dbo.ads_product_category_sale_money_yoy_pop_quarter-- Insert statements for procedure here-- 开始 TRY 块  BEGIN TRY  -- 执行可能引发异常的 SQL 语句  DELETE FROM dbo.ads_product_category_sale_money_yoy_pop_quarter WHERE dt = @DateThreshold;INSERT INTO dbo.ads_product_category_sale_money_yoy_pop_quarter ([dt],[year],[quarter],[ord],[title],[quarter_money],[previous_quarter_money],[qoq_growth_rate])SELECT @DateThreshold as dt,year,quarter,product_sort1,product_menuname,quarter_money,previous_quarter_money,CASE WHEN previous_quarter_money = 0 THEN null ELSE ((quarter_money - previous_quarter_money) / previous_quarter_money) * 100 END AS qoq_growth_ratefrom (SELECT year,quarter,product_sort1,product_menuname,quarter_money,case when (year = previous_year and quarter - previous_quarter = 1) or (year - previous_year = 1 and previous_quarter = 4 and quarter -previous_quarter = -3 ) then previous_quarter_money else 0 end  previous_quarter_money	from (SELECT year,quarter,product_sort1,product_menuname,quarter_money,CAST(SUBSTRING(previous_year_quarter, 1, 4) as INT) as previous_year,CAST(SUBSTRING(previous_year_quarter, 7, 1) as INT) as  previous_quarter,previous_quarter_moneyfrom(SELECT product_sort1,product_menuname,year,quarter,year_quarter,LAG(year_quarter, 1, '1970 Q1') OVER (PARTITION BY product_sort1,product_menuname ORDER BY year_quarter) AS previous_year_quarter,quarter_money,LAG(quarter_money, 1, 0) OVER (PARTITION BY product_sort1,product_menuname ORDER BY year_quarter) AS previous_quarter_moneyFROM (SELECT product_sort1,product_menuname,year,quarter,CONCAT([year], ' Q', [quarter]) AS year_quarter,quarter_moneyFROM (select product_sort1,product_menuname,year,quarter,sum(month_product_order_sale_money) as quarter_money from dws_product_category_sale_analyse_month WHERE dt = @DateThresholdgroup by product_sort1,product_menuname,year,quarter) t1) t3) t4) t5) t6;END TRY  -- 开始 CATCH 块  BEGIN CATCH  -- 捕捉异常并执行特定的操作  DECLARE @ErrorMessage52 NVARCHAR(4000);  SET @ErrorMessage52 = ERROR_MESSAGE();  RAISERROR (@ErrorMessage52, 16, 1);  -- 继续执行其他操作  -- ...  END CATCH--- dbo.ads_product_category_sale_money_yoy_year-- Insert statements for procedure here-- 开始 TRY 块  BEGIN TRY  -- 执行可能引发异常的 SQL 语句  DELETE FROM dbo.ads_product_category_sale_money_yoy_year WHERE dt = @DateThreshold;INSERT INTO dbo.ads_product_category_sale_money_yoy_year ([dt],[year],[ord],[title],[year_money],[previous_year_money],[yoy_growth_rate])select@DateThreshold as dt,year,product_sort1,product_menuname, year_money,previous_year_money,CASE WHEN previous_year_money = 0 THEN null ELSE ((year_money - previous_year_money) / previous_year_money) * 100 END AS yoy_growth_ratefrom (selectproduct_sort1,product_menuname,year,previous_year,year_money,case when yeardiff = 1 then previous_year_money else 0 end previous_year_moneyfrom (selectproduct_sort1,product_menuname, year,previous_year,year_money,previous_year_money,year - previous_year as yeardifffrom (selectproduct_sort1,product_menuname, year,lag(cast(year as int ),1,'1970') over (partition by product_sort1 order by year) previous_year,year_money,lag(year_money, 1, 0) over (partition by product_sort1 order by year) previous_year_moneyfrom (select product_sort1,product_menuname,year,sum(month_product_order_sale_money) as year_money from dws_product_category_sale_analyse_month WHERE dt = @DateThresholdgroup by product_sort1,product_menuname,year) t1) t3) t4) t2;END TRY  -- 开始 CATCH 块  BEGIN CATCH  -- 捕捉异常并执行特定的操作  DECLARE @ErrorMessage53 NVARCHAR(4000);  SET @ErrorMessage53 = ERROR_MESSAGE();  RAISERROR (@ErrorMessage53, 16, 1);  -- 继续执行其他操作  -- ...  END CATCH

SQL server 计算分月,分年,分季度的同环比

--- dbo.ads_sale_money_yoy_mom_month-- Insert statements for procedure here-- 开始 TRY 块  BEGIN TRY  -- 执行可能引发异常的 SQL 语句  DELETE FROM dbo.ads_sale_money_yoy_mom_month WHERE dt = @DateThreshold;WITH sales AS (  SELECT  [year],  month,  [month_sale_money] as month_money,  LAG([month_sale_money], 12) OVER (PARTITION BY [year],[month] ORDER BY [year],[month]) AS same_month_last_year_money,  ROW_NUMBER() OVER (PARTITION BY [year], [month] ORDER BY [month_sale_money]) AS rn  FROM (select year,month,month_sale_moneyfrom(select year,CAST(SUBSTRING(ym, 6, 2) AS INT) AS month,month_sale_moneyfrom dws_sale_analyse_month where dt = @DateThreshold) t1 			) t2),  months AS (  SELECT DISTINCT [year], month FROM sales  )  INSERT INTO dbo.ads_sale_money_yoy_mom_month ([dt],[ym],[month_money],[previous_month_money],[same_month_last_year_money],[yoy_growth_rate],[mom_growth_rate])select @DateThreshold as dt,CONCAT(CAST(year AS VARCHAR), '-', FORMAT(month, '00')) AS ym,month_money,previous_month_money,same_month_last_year_money,CASE WHEN same_month_last_year_money = 0 THEN null ELSE ((month_money - same_month_last_year_money) / same_month_last_year_money) * 100 END AS yoy_growth_rate,CASE WHEN previous_month_money = 0 THEN null ELSE ((month_money - previous_month_money) / previous_month_money) * 100 END AS mom_growth_ratefrom (SELECT DISTINCT q.[year],q.[month],case when s.[month_money] is NULL then 0 else s.[month_money] end AS month_money,    case when sqs.[month_money] is NULL then 0 else sqs.[month_money] end AS same_month_last_year_money,t4.previous_month_moneyFROM months q  JOIN sales s ON q.[year] = s.[year] AND q.[month] = s.[month] AND s.rn = 1  LEFT JOIN sales sqs ON q.[year] - 1 = sqs.[year] AND q.[month] = sqs.[month] AND sqs.rn = 1left join (select t2.dt,ym,t2.year,t2.month,t2.month_money,case when monthdiff = 1 then previous_month_money else 0 end  previous_month_moneyfrom(select dt,ym,SUBSTRING(ym, 1, 4) AS year,CAST(SUBSTRING(ym, 6, 2) AS INT) AS month,previous_month,month_sale_money as month_money,previous_month_money,DATEDIFF(MONTH, CAST(CONCAT(previous_month, '-01') AS DATE) , CAST(CONCAT(ym, '-01') AS DATE)) monthdifffrom(selectdt,ym,lag(ym, 1, '1970-01') over (partition by dt order by ym)  previous_month,lag(month_sale_money, 1, 0) over (partition by dt order by ym) previous_month_money,month_sale_moneyfromdws_sale_analyse_month where dt = @DateThreshold) t1) t2) t4 on q.year = t4.year and q.month = t4.month) t1;END TRY  -- 开始 CATCH 块  BEGIN CATCH  -- 捕捉异常并执行特定的操作  DECLARE @ErrorMessage23 NVARCHAR(4000);  SET @ErrorMessage23 = ERROR_MESSAGE();  RAISERROR (@ErrorMessage23, 16, 1);  -- 继续执行其他操作  -- ...  END CATCH--- dbo.ads_sale_money_yoy_pop_quarter-- Insert statements for procedure here-- 开始 TRY 块  BEGIN TRY  -- 执行可能引发异常的 SQL 语句  DELETE FROM dbo.ads_sale_money_yoy_pop_quarter WHERE dt = @DateThreshold;WITH sales AS (SELECT[year],[quarter],[quarter_money],LAG([quarter_money], 2) OVER (PARTITION BY [year], [quarter] ORDER BY [year], [quarter]) AS same_quarter_last_year_money,ROW_NUMBER() OVER (PARTITION BY [year], [quarter] ORDER BY [quarter_money]) AS rnFROM (SELECT [year],[quarter],sum(month_sale_money) as quarter_money FROM [big_data].[dbo].[dws_sale_analyse_month] where dt = @DateThreshold  group by year, quarter) t1), quarters AS (SELECT DISTINCT [year], [quarter] FROM sales)INSERT INTO dbo.ads_sale_money_yoy_pop_quarter ([dt],[year],[quarter],[quarter_money],[previous_quarter_money],[same_quarter_last_year_money],[yoy_growth_rate],[qoq_growth_rate])select @DateThreshold as dt,year,quarter,quarter_money,previous_quarter_money,same_quarter_last_year_money,CASE WHEN same_quarter_last_year_money = 0 THEN null ELSE ((quarter_money - same_quarter_last_year_money) / same_quarter_last_year_money) * 100 END AS yoy_growth_rate,CASE WHEN previous_quarter_money = 0 THEN null ELSE ((quarter_money - previous_quarter_money) / previous_quarter_money) * 100 END AS qoq_growth_ratefrom (SELECT DISTINCT q.[year],q.[quarter],case when s.[quarter_money] is NULL then 0 else s.[quarter_money] end AS quarter_money,   case when pqs.[previous_quarter_money] is NULL then 0 else pqs.[previous_quarter_money] end AS previous_quarter_money,   case when sqs.[quarter_money] is NULL then 0 else sqs.[quarter_money] end AS same_quarter_last_year_money  FROM quarters q  JOIN sales s ON q.[year] = s.[year] AND q.[quarter] = s.[quarter] AND s.rn = 1   LEFT JOIN sales sqs ON q.[year] - 1 = sqs.[year] AND q.[quarter] = sqs.[quarter] AND sqs.rn = 1LEFT JOIN (SELECT dt,SUBSTRING(year_quarter, 1, 4) AS year,SUBSTRING(year_quarter, 7, 1) AS quarter,LAG(year_quarter, 1, '1970 Q1') OVER (PARTITION BY dt ORDER BY year_quarter) AS previous_year,quarter_money,LAG(quarter_money, 1, 0) OVER (PARTITION BY dt ORDER BY year_quarter) AS previous_quarter_moneyFROM (SELECT @DateThreshold AS dt, CONCAT([year], ' Q', [quarter]) AS year_quarter,SUM(month_sale_money) AS quarter_moneyFROM [big_data].[dbo].[dws_sale_analyse_month] WHERE dt = @DateThreshold  GROUP BY CONCAT([year], ' Q', [quarter])) t) pqs ON q.[year] = pqs.[year] AND q.[quarter] = pqs.[quarter]) t1order by year, quarter asc;END TRY  -- 开始 CATCH 块  BEGIN CATCH  -- 捕捉异常并执行特定的操作  DECLARE @ErrorMessage22 NVARCHAR(4000);  SET @ErrorMessage22 = ERROR_MESSAGE();  RAISERROR (@ErrorMessage22, 16, 1);  -- 继续执行其他操作  -- ...  END CATCH--- dbo.ads_sale_money_yoy_year-- Insert statements for procedure here-- 开始 TRY 块  BEGIN TRY  -- 执行可能引发异常的 SQL 语句  DELETE FROM dbo.ads_sale_money_yoy_year WHERE dt = @DateThreshold;INSERT INTO dbo.ads_sale_money_yoy_year ([dt],[year],[year_money],[previous_year_money],[yoy_growth_rate])selectdt,year,--- lag(cast(year as int ),1,'1970') over (partition by dt order by year) previous_year,year_money,previous_year_money,CASE WHEN previous_year_money = 0 THEN null ELSE ((year_money - previous_year_money) / previous_year_money) * 100 END AS yoy_growth_ratefrom (selectdt,year,lag(cast(year as int ),1,'1970') over (partition by dt order by year) previous_year,year_money,lag(year_money, 1, 0) over (partition by dt order by year) previous_year_moneyfrom (select@DateThreshold dt,year,sum(month_sale_money) as year_moneyfromdws_sale_analyse_month where dt = @DateThreshold group by year) t1) t2;END TRY  -- 开始 CATCH 块  BEGIN CATCH  -- 捕捉异常并执行特定的操作  DECLARE @ErrorMessage24 NVARCHAR(4000);  SET @ErrorMessage24 = ERROR_MESSAGE();  RAISERROR (@ErrorMessage24, 16, 1);  -- 继续执行其他操作  -- ...  END CATCH

这篇关于SQL server 计算分月,分年,分季度的同环比的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/517237

相关文章

SQL中的外键约束

外键约束用于表示两张表中的指标连接关系。外键约束的作用主要有以下三点: 1.确保子表中的某个字段(外键)只能引用父表中的有效记录2.主表中的列被删除时,子表中的关联列也会被删除3.主表中的列更新时,子表中的关联元素也会被更新 子表中的元素指向主表 以下是一个外键约束的实例展示

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

如何去写一手好SQL

MySQL性能 最大数据量 抛开数据量和并发数,谈性能都是耍流氓。MySQL没有限制单表最大记录数,它取决于操作系统对文件大小的限制。 《阿里巴巴Java开发手册》提出单表行数超过500万行或者单表容量超过2GB,才推荐分库分表。性能由综合因素决定,抛开业务复杂度,影响程度依次是硬件配置、MySQL配置、数据表设计、索引优化。500万这个值仅供参考,并非铁律。 博主曾经操作过超过4亿行数据

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

MySQL数据库宕机,启动不起来,教你一招搞定!

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)公众号:老苏畅谈运维欢迎关注本人公众号,更多精彩与您分享。 MySQL数据库宕机,数据页损坏问题,启动不起来,该如何排查和解决,本文将为你说明具体的排查过程。 查看MySQL error日志 查看 MySQL error日志,排查哪个表(表空间

MySQL高性能优化规范

前言:      笔者最近上班途中突然想丰富下自己的数据库优化技能。于是在查阅了多篇文章后,总结出了这篇! 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意,并且最后不要超过32个字符 临时库表必须以tmp_为前缀并以日期为后缀,备份

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

[MySQL表的增删改查-进阶]

🌈个人主页:努力学编程’ ⛅个人推荐: c语言从初阶到进阶 JavaEE详解 数据结构 ⚡学好数据结构,刷题刻不容缓:点击一起刷题 🌙心灵鸡汤:总有人要赢,为什么不能是我呢 💻💻💻数据库约束 🔭🔭🔭约束类型 not null: 指示某列不能存储 NULL 值unique: 保证某列的每行必须有唯一的值default: 规定没有给列赋值时的默认值.primary key: