将动态数据透视的存储过程加入SQL Server 2008中的现有视图

时间:2017-11-14 22:02:01

标签: sql-server-2008 stored-procedures dynamic pivot

我在SQL Server 2008中有一个包含22列和大约90行的视图。我还想使用动态PIVOT来为每行添加多列附加注释。

有没有办法加入这两个?我已经尝试创建一个表来存储动态数据透视信息,但由于我不知道有多少列或列名,这对我来说不起作用。

这就是我所拥有的。

Select * From bcvEqInfo

结果与此相似

+----+------------+-----------+------+-------+
| Co | EquipCode  | EquipName | Vin  | etc   |
+----+------------+-----------+------+-------+
| 1  | 1-0000-001 | D10N-1    | 1234 | asdfs |
| 1  | 1-0000-002 | D10N-2    | 4567 | afda  |
| 1  | 1-0000-003 | D10N-3    | 8910 | adfs  |
+----+------------+-----------+------+-------+

我使用以下内容作为动态支点;随着更多日期的添加,日期标题越来越多。

Declare @ColumnNames NVARCHAR(Max) = ''
DECLARE @SQL NVARCHAR(MAX) = ''
select @ColumnNames += QUOTENAME(Name) + ','
from udEqNotes
SET @ColumnNames = LEFT(@ColumnNames,LEN(@ColumnNames)-1)
--Print @ColumnNames
SET @SQL =
'select * from
(Select Co, Equipment, Name, Notes
from udEqNotes)PivotDate
pivot
(Max(Notes)
for Name in (' + @ColumnNames + ')
) as Pivoting'

EXECUTE sp_executesql @SQL

有这样的结果

    +----+------------+--------------+--------------+--------------+
    | Co | EquipCode  | 110117       | 110217       | 110317       |
    +----+------------+--------------+--------------+--------------+
    | 1  | 1-0000-001 | D10N-1 Note1 | D10N-1 Note2 | D10N-1 Note3 |
    | 1  | 1-0000-002 | D10N-2 Note1 | D10N-2 Note2 | D10N-2 Note3 |
    | 1  | 1-0000-003 | D10N-3 Note1 | D10N-3 Note2 | D10N-3 Note3 |
    +----+------------+--------------+--------------+--------------+

理想情况下,我希望结果

+----+------------+-----------+------+-------+--------------+--------------+--------------+
| Co | EquipCode  | EquipName | Vin  | etc   | 110117       | 110217       | 110317       |
| 1  | 1-0000-001 | D10N-1    | 1234 | asdfs | D10N-1 Note1 | D10N-1 Note2 | D10N-1 Note3 |
| 1  | 1-0000-002 | D10N-2    | 4567 | afda  | D10N-2 Note1 | D10N-2 Note2 | D10N-2 Note3 |
| 1  | 1-0000-003 | D10N-3    | 8910 | adfs  | D10N-3 Note1 | D10N-3 Note2 | D10N-3 Note3 |
+----+------------+-----------+------+-------+--------------+--------------+--------------+

1 个答案:

答案 0 :(得分:1)

当您“尝试创建用于存储动态数据透视信息的表格”时,您处于正确的轨道上。我原本以为本地#temporary表可能会有这个技巧,但事实证明我需要使用全局的##临时表。

例如表格

[product]

product  description
-------  -------------------
bacon    thin strips of pork
ham      big hunk of pork


[sales]

id  product  region  sales_qty
--  -------  ------  ---------
 1  bacon    North          10
 2  ham      North           6
 3  bacon    South           7
 4  ham      South           9

此存储过程

ALTER PROCEDURE PivotAndJoin AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @regionlist NVARCHAR(max) = N'';
    DECLARE @sql NVARCHAR(max) = N'CREATE TABLE ##pivottemp (product NVARCHAR(20)';
    DECLARE @region NVARCHAR(10);
    DECLARE crsr CURSOR FOR SELECT DISTINCT region FROM sales;
    OPEN crsr;
    FETCH NEXT FROM crsr INTO @region;
    DECLARE @listseparator NVARCHAR(1) = N'';
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @sql += N', ' + QUOTENAME(@region) + N' INT';
        SET @regionlist += @listseparator + QUOTENAME(@region);
        IF LEN(@listseparator) = 0
        BEGIN
            SET @listseparator = N',';
        END;
        FETCH NEXT FROM crsr INTO @region;
    END;
    CLOSE crsr;
    DEALLOCATE crsr;
    SET @sql += N')';
    -- @regionlist example:
    --   [North],[South]
    -- @sql example:
    --   CREATE TABLE ##pivottemp (product NVARCHAR(20), [North] INT, [South] INT)
    BEGIN TRY
        DROP TABLE ##pivottemp;
    END TRY
    BEGIN CATCH
        -- ignore error (assume table simply didn't exist)
    END CATCH
    EXECUTE(@sql);
    SET @sql = 
            N'INSERT INTO ##pivottemp (product, ' + @regionlist + N') ' +
            N'SELECT product, ' + @regionlist +
            N'  FROM (SELECT product, region, sales_qty FROM sales) AS SourceTable ' +
            N'  PIVOT (SUM([sales_qty]) FOR [region] IN (' + @regionlist + N')) AS results';
    EXECUTE(@sql);
    SET @sql =
            N'SELECT product.product, product.description, ' + @regionlist + 
            N'  FROM ##pivottemp INNER JOIN product ' +
            N'      ON product.product = ##pivottemp.product';
    EXECUTE(@sql);
END

产生

product  description          North  South
-------  -------------------  -----  -----
bacon    thin strips of pork     10      7
ham      big hunk of pork         6      9