动态附加填充的计算列以生成结果表

时间:2018-07-18 14:26:55

标签: sql-server tsql ssms dynamic-sql temp-tables

使用:Microsoft SQL Server 2014-12.0.4213.0

我有一张表[Database_STG].[org].[Order_Feature_Counts],其中更新了按州销售的订单功能,其结构如下:

SELECT *  
FROM [Database_STG].[org].[Order_Feature_Counts]
ORDER BY State


State   BuildDate    Count_1    ...    Count_400
AK      2018-04-20   53200      ...    ...
AK      2018-06-05   53300      ...    ...
AL      2018-06-02   33800      ...    ...
AL      2018-03-09   33300      ...    ...
FL      2018-05-19   12000      ...    ...

我认为类似这样的方法可以生成部分样本(对于Count_1;总共有400个,名称是估计年龄范围,然后是有关订单的其他功能):

CREATE TABLE [Database_STG].[org].[Order_Feature_Counts](
   State      VARCHAR(2) NOT NULL PRIMARY KEY
  ,Build_Date DATE  NOT NULL
  ,Count_1    INTEGER  NOT NULL
);
INSERT INTO [Database_STG].[org].[Order_Feature_Counts] .(State,Build_Date,Count_1) VALUES ('AK','2018-04-20',53200);
INSERT INTO [Database_STG].[org].[Order_Feature_Counts](State,Build_Date,Count_1) VALUES ('AK','2018-06-05',53300);
INSERT INTO [Database_STG].[org].[Order_Feature_Counts](State,Build_Date,Count_1) VALUES ('AL','2018-06-02',34000);
INSERT INTO [Database_STG].[org].[Order_Feature_Counts](State,Build_Date,Count_1) VALUES ('AL','2018-03-09',33000);

有400个这样的计数。

每次更新基础销售表(节奏不规则)时,该表就会填充该州销售不同功能的相关计数(有多少来自电子邮件营销,有多少使用优惠券等) )

我想要一个表,该表显示以前对一个州的订单特征进行的调查与现在之间的差异以及从上次到最近的变化百分比。

这是我在做什么:

SELECT name, column_id
INTO #CountColumns
FROM sys.columns AS c
WHERE c.OBJECT_ID = OBJECT_ID('[Database_STG].[org].[Order_Feature_Counts]')
ORDER BY column_id    

DECLARE Count_Column_Cursor CURSOR SCROLL DYNAMIC FOR
    SELECT DISTINCT name
    FROM #CountColumns
    WHERE column_id > 2
DECLARE @Count_Column NVARCHAR(MAX)



OPEN Count_Column_Cursor
FETCH NEXT FROM Count_Column_Cursor INTO @Count_Column
WHILE @@FETCH_STATUS = 0
BEGIN

    DECLARE @SQL NVARCHAR(MAX)
    SET @SQL = N'

    SELECT DISTINCT *
    INTO #Temp_1
    FROM(
    SELECT *, Row_Number() OVER(PARTITION BY [State] ORDER BY [BuildDate] ASC) AS Build_Count
    FROM [Database_STG].[org].[Order_Feature_Counts]) AS StateDates
    WHERE Build_Count <= 2
    ORDER BY [State] ASC, [BuildDate] DESC


    ;WITH CTE AS (SELECT *,ROW_NUMBER() OVER (PARTITION BY [State] ORDER BY [BuildDate]) AS rn
                    FROM #Temp_1)

    SELECT c1.[State],
        c1.[BuildDate] AS "Most Recent Build",
        c1.['+@Count_Column+'] - c2.['+@Count_Column+'] as "['+@Count_Column+'] Change",
        STR(ISNULL(((c1.['+@Count_Column+']-c2.['+@Count_Column+'])/NULLIF(CAST(c2.['+@Count_Column+'] AS NUMERIC(10,2)),0)),0) * 100 ,5,2) AS "['+@Count_Column+'] Percent_Change"
    INTO #Temp_2
    FROM CTE AS c1
    LEFT JOIN CTE AS c2
    ON c1.[State] = c2.[State] AND c1.rn = c2.rn + 1
    DELETE FROM #temp_2
    WHERE "['+@Count_Column+'] Change" is NULL;'
    PRINT @SQL
    EXECUTE sp_executesql @SQL
    ,N'@Count_Column'
    ,@Count_Column

FETCH NEXT FROM Count_Column_Cursor INTO @Count_Column;
END;
CLOSE Count_Column_Cursor;
DEALLOCATE Count_Column_Cursor; 

此错误,并且在print语句中,每次查询后,我得到的查询打印数量都与我想要的一样(在'附近有'不正确的语法')”,例如:

    SELECT DISTINCT *
    INTO #Temp_1
    FROM(
    SELECT *, Row_Number() OVER(PARTITION BY [State] ORDER BY [BuildDate] ASC) AS Build_Count
    FROM [Database_STG].[org].[Order_Feature_Counts]) AS StateDates
    WHERE Build_Count <= 2
    ORDER BY [State] ASC, [BuildDate] DESC


    ;WITH CTE AS (SELECT *,ROW_NUMBER() OVER (PARTITION BY [State] ORDER BY [BuildDate]) AS rn
                    FROM #Temp_1)

    SELECT c1.[State],
        c1.[BuildDate] AS "Most Recent Build",
        c1.[Count_26-35] - c2.[Count_26-35] as "[Count_26-35] Change",
        STR(ISNULL(((c1.[Count_26-35]-c2.[Count_26-35])/NULLIF(CAST(c2.[Count_26-35] AS NUMERIC(10,2)),0)),0) * 100 ,5,2) AS "[Count_26-35] Percent_Change"
    INTO #Temp_2
    FROM CTE AS c1
    LEFT JOIN CTE AS c2
    ON c1.[State] = c2.[State] AND c1.rn = c2.rn + 1
    DELETE FROM #temp_2
    WHERE "[Count_26-35] Change" is NULL;

如果我运行这些查询之一,它将成功(带有三个不同的“行受影响”消息,就像我期望的那样)-一个这样的输出以及其他结果集:

    SELECT *  FROM [Database_STG].[org].[Order_Feature_Counts] 
    ORDER BY State
    SELECT * FROM #Temp_1
    SELECT * FROM #Temp_2

返回:

1)第一个表格示例

2)一张表,其结构类似,但每个州只有两个最新版本(即,如果初始表中有8个位于加利福尼亚州,则最近两个版本,或者如果佛罗里达只有一个记录,则只有那个记录)

3)如下表:

State   Most Recent Build   [Count_26-35] Change    [Count_26-35] Percent_Change
AK      2018-06-05          1500                    2.80
AL      2018-06-02          10400                   1.45

(数字抖动)

这#Temp_2结果几乎 正是我想要的。

我想要状态,最新版本以及每个计数的变化和百分比变化-所有这400个计数!

这样我将获得状态,构建日期,[Count_17-25]更改,[Count_17-25] Percent_Change,[Count_26-35]更改,[Count_26-35] Percent_Change等。(我将其命名为例如Count_1,Count_2等)。

如何将这些单个查询的结果“从左到右”附加到计算出来的完整结果表中?

0 个答案:

没有答案