SELECT a.[Sub-Region], a.[SBU], a.[CompanyCtry], a.[Inv_CM], b.[Inv_LM], ROW_NUMBER() OVER ( PARTITION BY a.[Sub-Region], a.[SBU] ORDER BY ABS(a.[Inv_CM] - b.[Inv_LM]) DESC ) as RowNo INTO [AP_Inventory].[dbo].[Temp_vs_CY_LM] FROM ( SELECT [Sub-Region], [SBU_2023] as [SBU], [CompanyCtry], SUM([TotalInv_EUR]) as [Inv_CM] FROM [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] WHERE DATEDIFF(MONTH, DATEFROMPARTS([ReportYear], [ReportMonth], 1), GETDATE()) = 1 and [StockCategory] <> 'Vendor Consignment' and [CompanyCtry] is not null GROUP BY [Sub-Region], [SBU_2023], [CompanyCtry] ) as a, ( SELECT [Sub-Region], [SBU_2023] as [SBU], [CompanyCtry], SUM([TotalInv_EUR]) as [Inv_LM] FROM [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] WHERE DATEDIFF(MONTH, DATEFROMPARTS([ReportYear], [ReportMonth], 1), GETDATE()) = 2 and [StockCategory] <> 'Vendor Consignment' and [CompanyCtry] is not null GROUP BY [Sub-Region], [SBU_2023], [CompanyCtry] ) as b WHERE a.[Sub-Region] = b.[Sub-Region] and a.[SBU] = b.[SBU] and a.[CompanyCtry] = b.[CompanyCtry]; GO UPDATE a SET a.[vs_CY_LM_Content] = b.[CompanyCtry], a.[vs_CY_LM_Content_Qty] = b.[Inv_CM], a.[vs_CY_LM_Delta_Qty] = b.[Inv_CM] - b.[Inv_LM] FROM [AP_Inventory].[dbo].[Calculate_Top_Variation_V4] as a, [AP_Inventory].[dbo].[Temp_vs_CY_LM] as b WHERE a.[Organization] = b.[Sub-Region] and a.[SBU] = b.[SBU] and a.[Dimension] = '[Country]' and a.[vs_CY_LM_Rank] = b.[RowNo] and DATEDIFF(MONTH, DATEFROMPARTS(a.[Report_Year], a.[Report_Month], 1), GETDATE()) = 1; GO DROP TABLE [AP_Inventory].[dbo].[Temp_vs_CY_LM]; GO
时间: 2023-08-18 14:05:20 浏览: 131
这段代码的作用是从表 [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] 中获取当前月份(CM)和上个月(LM)的库存数据,并按照一定的排序规则计算出差值。然后将计算结果更新到目标表 [AP_Inventory].[dbo].[Calculate_Top_Variation_V4] 中的对应字段。
首先,通过两个子查询获取当前月份和上个月份的库存数据,并使用 SUM 函数计算每个组合的总库存量。然后,使用 ROW_NUMBER 函数为每个组合计算一个排序号(RowNo),排序规则是根据当前月份库存量与上个月份库存量之差的绝对值进行排序。
接下来,将计算结果插入到临时表 [AP_Inventory].[dbo].[Temp_vs_CY_LM] 中,用于后续的更新操作。
最后,通过 UPDATE 语句将临时表中的差值更新到目标表中的相应字段。更新的条件是目标表中的组织(Organization)、业务单位(SBU)、维度(Dimension)以及排名(Rank)与临时表中的对应字段匹配。
最后,使用 DROP TABLE 语句删除临时表。
整个代码段的目的是根据当前月份和上个月份的库存数据,计算出差值,并将差值更新到目标表中。通过使用临时表和动态计算,可以实现对差值的灵活计算和更新。
相关问题
DECLARE @dimension_ID int = 1; DECLARE @dimension_Content nvarchar(50); WHILE @dimension_ID <= (SELECT MAX([ID]) FROM [AP_Inventory].[dbo].[MD_Variation_Dimension]) BEGIN SET @dimension_Content = (SELECT [Dimension] FROM [AP_Inventory].[dbo].[MD_Variation_Dimension] WHERE [ID] = @dimension_ID); EXEC(' SELECT a.[CompanyCtry], a.[SBU], a.'+@dimension_Content+', a.[Inv_CM], b.[Inv_LM], ROW_NUMBER() OVER ( PARTITION BY a.[CompanyCtry], a.[SBU] ORDER BY ABS(a.[Inv_CM] - b.[Inv_LM]) DESC ) as RowNo INTO [AP_Inventory].[dbo].[Temp_vs_CY_LM] FROM ( SELECT [CompanyCtry], [SBU_2023] as [SBU], '+@dimension_Content+', SUM([TotalInv_EUR]) as [Inv_CM] FROM [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] WHERE DATEDIFF(MONTH, DATEFROMPARTS([ReportYear], [ReportMonth], 1), GETDATE()) = 1 and [StockCategory] <> ''Vendor Consignment'' and '+@dimension_Content+' is not null GROUP BY [CompanyCtry], [SBU_2023], '+@dimension_Content+' ) as a, ( SELECT [CompanyCtry], [SBU_2023] as [SBU], '+@dimension_Content+', SUM([TotalInv_EUR]) as [Inv_LM] FROM [AP_Inventory].[dbo].[Inv_Details_for_PowerBI] WHERE DATEDIFF(MONTH, DATEFROMPARTS([ReportYear], [ReportMonth], 1), GETDATE()) = 2 and [StockCategory] <> ''Vendor Consignment'' and '+@dimension_Content+' is not null GROUP BY [CompanyCtry], [SBU_2023], '+@dimension_Content+' ) as b WHERE a.[CompanyCtry] = b.[CompanyCtry] and a.[SBU] = b.[SBU] and a.'+@dimension_Content+' = b.'+@dimension_Content+' ') EXEC(' UPDATE a SET a.[vs_CY_LM_Content] = b.'+@dimension_Content+', a.[vs_CY_LM_Content_Qty] = b.[Inv_CM], a.[vs_CY_LM_Delta_Qty] = b.[Inv_CM] - b.[Inv_LM] FROM [AP_Inventory].[dbo].[Calculate_Top_Variation_V4] as a, [AP_Inventory].[dbo].[Temp_vs_CY_LM] as b WHERE a.[Organization] = b.[CompanyCtry] and a.[SBU] = b.[SBU] and a.[Dimension] = '''+@dimension_Content+''' and a.[vs_CY_LM_Rank] = b.[RowNo] and DATEDIFF(MONTH, DATEFROMPARTS(a.[Report_Year], a.[Report_Month], 1), GETDATE()) = 1; '); DROP TABLE [AP_Inventory].[dbo].[Temp_vs_CY_LM]; SET @dimension_ID = @dimension_ID + 1; 在这段代码里面exec函数有什么意义呢,并且一整段函数有什么意义。
在这段代码中,EXEC函数用于执行动态SQL语句。动态SQL是指在运行时根据参数或条件构建SQL语句并执行。使用EXEC函数可以将字符串作为SQL语句执行,从而实现动态生成和执行SQL语句。
整个代码段的意义是根据不同的维度(Dimension)计算出与上个月(LM)相比的差值,并更新到目标表中。代码中的循环用于遍历每个维度,并根据维度的内容动态生成并执行SQL语句。
具体来说,代码中的第一个EXEC语句用于创建一个临时表,该临时表存储了当前月份(CM)与上个月(LM)之间的差值,并按照指定规则进行排序。这个临时表用于后续更新目标表中的相关字段。
第二个EXEC语句用于更新目标表,将临时表中计算得到的差值和相关维度内容更新到目标表中的相应字段。更新的条件是目标表中的组织(Organization)、业务单位(SBU)、维度(Dimension)以及排名(Rank)与临时表中的对应字段匹配。
最后,使用DROP TABLE语句删除临时表。
整个代码段的目的是根据不同维度计算出与上个月相比的差值,并将计算结果更新到目标表中。通过使用动态SQL语句,可以根据不同的维度内容动态生成和执行SQL语句,实现对目标表的灵活更新。
select distinct b.[ProfitCenter] AS PC, 'new' AS CHANGETYPE, a.[2020_SBU_Abbr] as MAYSBU, sum(b. [TotalInv_EUR]) as inv, b. [SBU_2023] AS NEWSBU FROM #temple_now6 b left JOIN #temple_now a on a.[NEWKEY] = b. [NEWKEY] WHERE a.[ProfitCenter] IS NULL group by b.[ProfitCenter],b. [SBU_2023],a.[2020_SBU_Abbr]
您的查询语句是用于从 #temple_now6 表中选择符合条件的数据,并进行一些计算和聚合操作。具体的 SQL 语句如下所示:
```sql
SELECT
DISTINCT b.[ProfitCenter] AS PC,
'new' AS CHANGETYPE,
a.[2020_SBU_Abbr] AS MAYSBU,
SUM(b.[TotalInv_EUR]) AS inv,
b.[SBU_2023] AS NEWSBU
FROM
#temple_now6 b
LEFT JOIN
#temple_now a ON a.[NEWKEY] = b.[NEWKEY]
WHERE
a.[ProfitCenter] IS NULL
GROUP BY
b.[ProfitCenter], b.[SBU_2023], a.[2020_SBU_Abbr]
```
这个查询会返回根据一些条件和列进行分组后的数据,其中包括了 #temple_now6 表中满足条件的数据,并对其中的某些列进行了聚合操作。结果会包括唯一的 ProfitCenter (PC)、固定的 CHANGETYPE (new)、2020_SBU_Abbr (MAYSBU)、TotalInv_EUR 的求和结果 (inv),以及 NEWSBU 列的值。查询中还使用了左连接将 #temple_now 表与 #temple_now6 表进行关联,并根据条件筛选出在 #temple_now 表中 ProfitCenter 列为空的记录。最后,根据 ProfitCenter、SBU_2023 和 2020_SBU_Abbr 列进行分组。
阅读全文