创建具有多个动态列的数据透视表时出错

时间:2017-05-09 15:52:05

标签: sql-server

我正在尝试关注此SO post中的示例 但我收到以下错误:

  

(82938行受影响)消息1056,级别15,状态1,行1号码   选择列表中的元素超出了允许的最大数量   4096个元素。消息102,级别15,状态1,行5语法不正确   靠近'x'。

这是我的SQL:

public void init(){
    text1=new TextField(8);

    add(text1);

    text1.setText("0");

}
public void paint(Graphics g){
    int x=0;
    String s1,str = null;
    g.drawString("input in the  box",10,50);
    try{
        s1=text1.getText();
        str=String.valueOf(s1);


    }
    catch(Exception e){}
    g.drawString(str,75,75);
}
public boolean action (Event event,Object object){
    repaint();
    return true;
}

我不知道我做错了什么。如果我通过标记每个列来手动创建数据透视表,如下所示:

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

select @cols = STUFF((SELECT ',' + QUOTENAME(Description) 
                    from (Select distinct CompanyID, vchCompanyName as CompanyName, vchPlanDescription as Description, Convert(INT,bitRequiredByMatrix) As Required from tblCompany C
inner join
(Select distinct ContractorID, SUM(decAuditScore) as Score from 
[tblTrainingStandardAudits]
where bitRequiredByMatrix = 1 and TrainingStandardID = 1
Group By ContractorID) CS on CS.ContractorID = C.CompanyID and C.bitActive = 1
Inner Join tblTrainingStandardAuditSummary SAS on SAS.ContractorID = C.CompanyID
Inner join(SELECT distinct [ContractorID],[bitRequiredByMatrix],TP.vchPlanDescription
  FROM [PECV4].[dbo].[tblTrainingStandardAudits] SA
  inner join tblTrainingPlans TP on TP.BitwiseMatrixID = SA.BitwiseMatrixID AND SA.TrainingStandardID = TP.TrainingStandardID where TP.TrainingStandardID=1) RQ on RQ.ContractorID = C.CompanyID)OPP
                    group by Description, CompanyID
                    order by CompanyID
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select value, Description
                from (Select distinct CompanyID, vchCompanyName as CompanyName, vchPlanDescription as Description, Convert(INT,bitRequiredByMatrix) As Required from tblCompany C
inner join
(Select distinct ContractorID, SUM(decAuditScore) as Score from 
[tblTrainingStandardAudits]
where bitRequiredByMatrix = 1 and TrainingStandardID = 1
Group By ContractorID) CS on CS.ContractorID = C.CompanyID and C.bitActive = 1
Inner Join tblTrainingStandardAuditSummary SAS on SAS.ContractorID = C.CompanyID
Inner join(SELECT distinct [ContractorID],[bitRequiredByMatrix],TP.vchPlanDescription
  FROM [PECV4].[dbo].[tblTrainingStandardAudits] SA
  inner join tblTrainingPlans TP on TP.BitwiseMatrixID = SA.BitwiseMatrixID AND SA.TrainingStandardID = TP.TrainingStandardID where TP.TrainingStandardID=1) RQ on RQ.ContractorID = C.CompanyID)OPP
            ) x
            pivot 
            (
                max(Required)
                for Description in (' + @cols + N')
            ) p '

exec sp_executesql @query;

有2634行。共有48列,所以我不确定错误中4096个元素的来源。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

Select语句中的列重复太多了。解决方案是使用另一个选择,返回一个不同的列名列表。这是有用的:

    SET @cols = STUFF((SELECT DISTINCT',' + QUOTENAME(Description) 
from (SELECT vchPlanDescription as Description from 
tblTrainingPlans
where TrainingStandardID = 1) cols

            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

然后设置查询:

set @query = N'SELECT ' + @cols + N' from 
             (
                select value, Description
                from (Select distinct CompanyID, vchCompanyName as CompanyName, vchPlanDescription as Description, Convert(INT,bitRequiredByMatrix) As Required from tblCompany C
inner join
(Select distinct ContractorID, SUM(decAuditScore) as Score from 
[tblTrainingStandardAudits]
where bitRequiredByMatrix = 1 and TrainingStandardID = 1
Group By ContractorID) CS on CS.ContractorID = C.CompanyID and C.bitActive = 1
Inner Join tblTrainingStandardAuditSummary SAS on SAS.ContractorID = C.CompanyID
Inner join(SELECT distinct [ContractorID],[bitRequiredByMatrix],TP.vchPlanDescription
  FROM [PECV4].[dbo].[tblTrainingStandardAudits] SA
  inner join tblTrainingPlans TP on TP.BitwiseMatrixID = SA.BitwiseMatrixID AND SA.TrainingStandardID = TP.TrainingStandardID where TP.TrainingStandardID=1) RQ on RQ.ContractorID = C.CompanyID)OPP
            ) x
            pivot 
            (
                max(Required)
                for Description in (' + @cols + N')
            ) p '

exec sp_executesql @query;

希望这有助于其他人。