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_rate
		from(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_money
		from(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)) monthdiff
			from(select
				dt,
				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_money
				from
				dws_product_category_sale_analyse_month 
			where dt = @DateThreshold ) t1) t2) t3

    END 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_rate
		from (
		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_money
		from(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_money
		FROM (
			SELECT 
				product_sort1,
			    product_menuname,
				year,
				quarter,
				CONCAT([year], ' Q', [quarter]) AS year_quarter,
				quarter_money
			FROM 
			(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 = @DateThreshold
				group 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_rate
		from (
		select
		    product_sort1,
		    product_menuname,
			year,
			previous_year,
			year_money,
			case when yeardiff = 1 then previous_year_money else 0 end previous_year_money
        from (select
		    product_sort1,
		    product_menuname, 
			year,
			previous_year,
			year_money,
			previous_year_money,
			year - previous_year as yeardiff
		from (select
		    product_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_money
		from (select 
		        product_sort1,
		        product_menuname,
			    year,
			    sum(month_product_order_sale_money) as year_money 
				from dws_product_category_sale_analyse_month 
				WHERE dt = @DateThreshold
				group 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_money
					from(select 
						year,
						CAST(SUBSTRING(ym, 6, 2) AS INT) AS month,
						month_sale_money
						from 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_rate
		from (
			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_money
			FROM 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 = 1
			left 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_money
				from(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)) monthdiff
					from(select
						dt,
						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_money
						from
						dws_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 rn
			FROM (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_rate
		from (
			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 = 1
			LEFT 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_money
					FROM (
						SELECT 
							@DateThreshold AS dt, 
							CONCAT([year], ' Q', [quarter]) AS year_quarter,
							SUM(month_sale_money) AS quarter_money
						FROM [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]
		) t1
		order 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])
		select
			dt,
			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_rate
		from (
		select
			dt,
			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_money
		from (select
				@DateThreshold dt,
				year,
				sum(month_sale_money) as year_money
				from
				dws_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

你可能感兴趣的:(服务器,数据库,运维)