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

相关文章

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

MySQL错误代码2058和2059的解决办法

《MySQL错误代码2058和2059的解决办法》:本文主要介绍MySQL错误代码2058和2059的解决办法,2058和2059的错误码核心都是你用的客户端工具和mysql版本的密码插件不匹配,... 目录1. 前置理解2.报错现象3.解决办法(敲重点!!!)1. php前置理解2058和2059的错误

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

MySQL Workbench 安装教程(保姆级)

《MySQLWorkbench安装教程(保姆级)》MySQLWorkbench是一款强大的数据库设计和管理工具,本文主要介绍了MySQLWorkbench安装教程,文中通过图文介绍的非常详细,对大... 目录前言:详细步骤:一、检查安装的数据库版本二、在官网下载对应的mysql Workbench版本,要是

mysql数据库重置表主键id的实现

《mysql数据库重置表主键id的实现》在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,本文主要介绍了mysql数据库重置表主键id的实现,具有一定的参考价值,感兴趣的可以了... 目录关键语法演示案例在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,当我们