如何取消透视表与动态列

时间:2019-09-19 13:37:23

标签: sql sql-server

我正在尝试转置名为CHECK_CARD的表。看起来像这样:

 _________________________________________________________________
|       anc_report_date       |  RiskSignal  | group_company_attr |
|_____________________________|______________|____________________|
| 2019-01-01 00:00:00.0000000 | NoRiskSignal |       4894543      |
| 2016-07-01 00:00:00.0000000 |  RiskSignal  |       1242151      |

我希望它看起来像这样:

 ____________________________________________________________________________
|   anc_report_date  |2019-01-01 00:00:00.0000000|2016-07-01 00:00:00.0000000|
|____________________|___________________________|___________________________|
| RiskSignal         |        NoRiskSignal       |        RiskSignal         |
| group_company_attr |          1242151          |          1242151          |

anc_report_date中的行数是动态的,所以我想做的是:

DECLARE @Column NVARCHAR(MAX)
SET @Column
  = 'SELECT cc.anc_report_date
        from CHECK_CARD cc
        inner join CHECK_CARD chc on cc.group_company_attr=chc.group_company_attr and cc.anc_report_date<=chc.anc_report_date and chc.id=1832307
    where cc.status=1'
exec sp_executesql @Column

我在这里收集所有列名。

DECLARE @Column NVARCHAR(MAX)
SET @Column
  = 'SELECT cc.anc_report_date
        from CHECK_CARD cc
        inner join CHECK_CARD chc on cc.group_company_attr=chc.group_company_attr and cc.anc_report_date<=chc.anc_report_date and chc.id=1832307
    where cc.status=1'
DECLARE @Transpose as NVARCHAR(MAX)
set @Transpose
  = 'SELECT RiskSignal, group_company_attr
     from CHECK_CARD
     unpivot
     (
        kek
        for anc_report_date in ('+ @Column +')
     ) u'

exec sp_executesql @Transpose;

但是当我一起使用它时,它会返回“关键字'SELECT'附近的语法不正确”。
我认为这行('+ @Columns +')是原因。
有什么想法吗?

     PRINT @Transpose;
[2019-09-19 18:54:42] [S0001] SELECT RiskSignal, group_company_attr
[2019-09-19 18:54:42] from CHECK_CARD
[2019-09-19 18:54:42] unpivot
[2019-09-19 18:54:42] (
[2019-09-19 18:54:42] kek
[2019-09-19 18:54:42] for anc_report_date in (SELECT cc.anc_report_date
[2019-09-19 18:54:42] from CHECK_CARD cc
[2019-09-19 18:54:42] inner join CHECK_CARD chc on cc.group_company_attr=chc.group_company_attr and cc.anc_report_date<=chc.anc_report_date and chc.id=1832307 where cc.status=1)
[2019-09-19 18:54:42] ) u
[2019-09-19 18:54:42] completed in 3 ms

1 个答案:

答案 0 :(得分:0)

做到了,谢谢大家!

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

select @cols = STUFF((SELECT ',' + QUOTENAME(cc.id)
from CHECK_CARD cc
         inner join CHECK_CARD chc
                    on cc.group_company_attr = chc.group_company_attr and cc.anc_report_date <= chc.anc_report_date and
                       chc.id = 1832307
where cc.status = 1
      group by cc.id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

set @query = N'SELECT AttrName,' + @cols + N' from
(
  select id, AttrName, value
  from CHECK_CARD
  cross apply
  (
select ''RiskSignal'', cast(RiskSignalas varchar(50)) union all
select ''group_company_attr'', cast(group_company_attr varchar(50))
  ) c(AttrName, value)
) d
pivot
(
  max(value)
  for id in (' + @cols + N')
) piv';

exec sp_executesql @query;