在 SQL Server 2012 中动态取消透视时出错

Getting error when dynamically unpivoting in SQL Server 2012

目标是动态旋转数据然后动态取消旋转数据。

我收到一条错误消息 Invalid ControlNo

我假设它是因为变量 @colsUnpivot 包含 ControlNo 列。

但是我该如何解决呢?我绝对需要那个 ControlNo.

IF OBJECT_ID('tempdb..##A') IS NOT NULL DROP TABLE ##A
IF OBJECT_ID('tempdb..#table1') IS NOT NULL DROP TABLE #table1

Create table #Table1 ( ControlNo INT, Bound INT, Declined INT, Rated INT, Quoted INT, QuoteStatus VARCHAR(50) )

INSERT INTO #Table1 (ControlNo, Bound, Declined, Rated, Quoted, QuoteStatus) 
    VALUES  (1111,1,0,1,1,'Lost'), 
            (2222,0,1,0,1,'No Action'), 
            (3333,1,1,0,0,NULL), 
            (4444,1,0,0,1,'Lost'), 
            (5555,0,1,1,1,'No Action')

DECLARE @columns AS NVARCHAR(MAX), 
        @finalquery AS NVARCHAR(MAX);

SET @columns = STUFF((SELECT distinct ',' + QUOTENAME(QuoteStatus) FROM #Table1 FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')
--PRINT @columns

set @finalquery = '
        select  p.controlno,
                p.Bound, 
                p.Declined, 
                p.Rated,
                p.Quoted,' + @columns + '
        into ##A 
        from ( select ControlNo, Bound, Declined, Rated, Quoted, QuoteStatus
                from #Table1
            )a 
    pivot 
    ( 
     COUNT(QuoteStatus) 
     for QuoteStatus IN (' + @columns + ') 
    )p '

exec(@finalquery)

--SELECT * 
--FROM ##a


DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)

select @colsUnpivot 
  = stuff((select ','+quotename(C.name)
           FROM tempdb.sys.columns c
           WHERE c.object_id = OBJECT_ID('tempdb..##A') 
           for xml path('')), 1, 1, '')
--PRINT @colsUnpivot

set @query 
  = 'select ControlNo, Counts, Status
     from ##A
     unpivot
     (
        Counts
        for Status in ('+ @colsunpivot +')
     ) u'

exec sp_executesql @query;

输出应如下所示:

我假设您的示例输出是一个示例,并且您排除了其他控制编号。如果是这样,更改 stuff 中的 where 子句将从 unpivot 组中删除该列,并给出您想要的结果。您不能 return 正在旋转或取消旋转的列。

IF OBJECT_ID('tempdb..##A') IS NOT NULL DROP TABLE ##A
IF OBJECT_ID('tempdb..#table1') IS NOT NULL DROP TABLE #table1

Create table #Table1 ( ControlNo INT, Bound INT, Declined INT, Rated INT, Quoted INT, QuoteStatus VARCHAR(50) )

INSERT INTO #Table1 (ControlNo, Bound, Declined, Rated, Quoted, QuoteStatus) 
    VALUES  (1111,1,0,1,1,'Lost'), 
            (2222,0,1,0,1,'No Action'), 
            (3333,1,1,0,0,NULL), 
            (4444,1,0,0,1,'Lost'), 
            (5555,0,1,1,1,'No Action')

DECLARE @columns AS NVARCHAR(MAX), 
        @finalquery AS NVARCHAR(MAX);

SET @columns = STUFF((SELECT distinct ',' + QUOTENAME(QuoteStatus) FROM #Table1 FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')
--PRINT @columns

set @finalquery = '
        select  p.controlno,
                p.Bound, 
                p.Declined, 
                p.Rated,
                p.Quoted,' + @columns + '
        into ##A 
        from ( select *
                from #Table1
            )a 
    pivot 
    ( 
     COUNT(QuoteStatus) 
     for QuoteStatus IN (' + @columns + ') 
    )p '

exec(@finalquery)

--SELECT * 
--FROM ##a


DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)

select @colsUnpivot 
  = stuff((select ','+quotename(C.name)
           FROM tempdb.sys.columns c
           --notice i remove controlno here...
           WHERE c.object_id = OBJECT_ID('tempdb..##A') and c.name <> 'controlno'
           for xml path('')), 1, 1, '')
--PRINT @colsUnpivot


set @query 
  = 'select  Controlno, Counts, Status
     from ##A
     unpivot
     (
        Counts
        for Status in ('+ @colsunpivot +')
     ) u'

exec sp_executesql @query;

要获得准确的输出,只需将 where 子句添加到 set @query

set @query 
  = 'select  Controlno, Counts, Status
     from ##A
     unpivot
     (
        Counts
        for Status in ('+ @colsunpivot +')
     ) u
     where controlno = 1111'