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虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

Window Server创建2台服务器的故障转移群集的图文教程

《WindowServer创建2台服务器的故障转移群集的图文教程》本文主要介绍了在WindowsServer系统上创建一个包含两台成员服务器的故障转移群集,文中通过图文示例介绍的非常详细,对大家的... 目录一、 准备条件二、在ServerB安装故障转移群集三、在ServerC安装故障转移群集,操作与Ser

mysql数据库分区的使用

《mysql数据库分区的使用》MySQL分区技术通过将大表分割成多个较小片段,提高查询性能、管理效率和数据存储效率,本文就来介绍一下mysql数据库分区的使用,感兴趣的可以了解一下... 目录【一】分区的基本概念【1】物理存储与逻辑分割【2】查询性能提升【3】数据管理与维护【4】扩展性与并行处理【二】分区的

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

使用SQL语言查询多个Excel表格的操作方法

《使用SQL语言查询多个Excel表格的操作方法》本文介绍了如何使用SQL语言查询多个Excel表格,通过将所有Excel表格放入一个.xlsx文件中,并使用pandas和pandasql库进行读取和... 目录如何用SQL语言查询多个Excel表格如何使用sql查询excel内容1. 简介2. 实现思路3

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

Mysql DATETIME 毫秒坑的解决

《MysqlDATETIME毫秒坑的解决》本文主要介绍了MysqlDATETIME毫秒坑的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 今天写代码突发一个诡异的 bug,代码逻辑大概如下。1. 新增退款单记录boolean save = s

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d

MySQL中的锁和MVCC机制解读

《MySQL中的锁和MVCC机制解读》MySQL事务、锁和MVCC机制是确保数据库操作原子性、一致性和隔离性的关键,事务必须遵循ACID原则,锁的类型包括表级锁、行级锁和意向锁,MVCC通过非锁定读和... 目录mysql的锁和MVCC机制事务的概念与ACID特性锁的类型及其工作机制锁的粒度与性能影响多版本