CTE根据输入创建递归动态行和列

时间:2019-03-29 05:26:17

标签: sql-server tsql sql-server-2012 common-table-expression dynamic-pivot

我正在根据用户输入创建动态行和列。输出与此类似。

enter image description here

到目前为止,我只能使用CTE计算一列。

    DECLARE @ROWStart INT = 1
DECLARE @NOOFROWS INT = 5
DECLARE @NOOFOLUMNS INT = 5
DECLARE @RACK NVARCHAR(10) = 'TAB-'
;WITH   cte
AS     (SELECT @ROWStart AS n, 1 AS col,CONVERT(VARCHAR(20),@RACK + 'R1C1') AS RW -- anchor member
        UNION ALL
        SELECT n + 1,cte.col, CONVERT(VARCHAR(20),@RACK + 'R' + CONVERT(VARCHAR(10),n+1) + 'C' + CONVERT(VARCHAR(10),col))  -- recursive member
        FROM   cte
        WHERE  n < @NOOFROWS -- terminator
       )
SELECT cte.RW
FROM   cte;

如何创建后续列?

2 个答案:

答案 0 :(得分:1)

尝试一下:

DECLARE @RowsLimit INT
       ,@columnsLimit INT;

SELECT @RowsLimit = 5
      ,@columnsLimit = 7;

DECLARE @DynammicTSQLStatement NVARCHAR(MAX)
       ,@DynamicPIVOTColumns NVARCHAR(MAX)
       ,@DynamicPIVOTResultsColumns NVARCHAR(MAX);

CREATE TABLE #Rows
(
    [value] INT
);

CREATE TABLE #Colmuns
(
    [RowID] INT
   ,[value] INT
);

INSERT INTO #Rows ([value])
SELECT TOP (@RowsLimit) + ROW_NUMBER() OVER(ORDER BY t1.[number])
FROM [master]..[spt_values] t1 
CROSS JOIN [master]..[spt_values] t2

INSERT INTO #Colmuns ([RowID], [value])
SELECT TOP (@columnsLimit)  ROW_NUMBER() OVER(ORDER BY t1.[number]) 
                           ,ROW_NUMBER() OVER(ORDER BY t1.[number])
FROM [master]..[spt_values] t1 
CROSS JOIN [master]..[spt_values] t2

SET @DynamicPIVOTResultsColumns = STUFF
                          (
                                (
                                SELECT ',REPLACE(''TAB-R#C' + CAST([value] AS VARCHAR(12)) + ''', ''#'', [value])'
                                FROM #Colmuns
                                GROUP BY [value]
                                FOR XML PATH('') ,TYPE
                                ).value('.', 'NVARCHAR(MAX)')
                                ,1
                                ,1
                                ,''
                          );

SET @DynamicPIVOTColumns = STUFF
                          (
                                (
                                SELECT ',[' + CAST([value] AS VARCHAR(12)) + ']'
                                FROM #Colmuns
                                GROUP BY [value]
                                FOR XML PATH('') ,TYPE
                                ).value('.', 'NVARCHAR(MAX)')
                                ,1
                                ,1
                                ,''
                          );


SET @DynammicTSQLStatement = N'
SELECT '+ @DynamicPIVOTResultsColumns +'
FROM #Rows
CROSS APPLY 
(
    SELECT *
    FROM #Colmuns
    PIVOT
    (
        MAX([value]) FOR [RowID] IN (' + @DynamicPIVOTColumns + ')
    ) PVT
) DS';


EXEC sp_executesql @DynammicTSQLStatement;


DROP TABLE #Rows;
DROP TABLE #Colmuns;

enter image description here

答案 1 :(得分:1)

尝试

<svg width="200" height="200">
  <circle id="bg" r="100" cx="100" cy="100" />
  
  
  
  <path d="M100,50A50,50 0 0 1 100,150A50,50 0 0 1 100,50" id="p1" r="50" cx="100" cy="100" stroke="yellowgreen" stroke-width="100" fill="none" />
</svg>