SQL数据透视表动态导致错误"选择列表中的元素数超过允许的最大数量4096个元素。"

时间:2015-10-20 11:36:40

标签: sql-server pivot pivot-table

我是MS SQL Server PIVOT功能的新手,到目前为止还不是很好,我不知道我做错了什么...... 我已经看了很多其他例子,但每个人似乎都有不同的要求。 我是一辆车,每辆车都有不同的价格,例如:净成本,正常销售,净销售和特别。我想进行调整,以便所有价格都在同一行,而不是每个价格的多行。 这是我的尝试失败的错误"选择列表中的元素数量超过允许的最大数量4096个元素。"

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

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(v.[Id]) 
                    FROM [Vehicle] v
                    INNER JOIN [VehiclePrice] vp ON vp.[VehicleId]=v.[Id]
                    INNER JOIN [VehiclePriceLevel] vpl ON vpl.Id=vp.VehiclePriceLevelId
                    GROUP BY 
                        v.[Id],v.[Category],v.[Status],v.[Publication],v.[VIN], v.[EngineNumber],v.MMCode,
                        v.[Year],v.Kilometers,v.Fuel,v.Transmission,v.Colour,v.PrimaryColour,v.BodyType,v.[IsSpecialVehicle],
                        vpl.[Description], vp.[Amount], vpl.Id
                    ORDER BY v.[Id]
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @query = N'SELECT ' + @cols + N' from 
            (
                SELECT
                    v.[Id],v.[Category],v.[Status],v.[Publication],v.[VIN], v.[EngineNumber],v.MMCode,
                    v.[Year],v.Kilometers,v.Fuel,v.Transmission,v.Colour,v.PrimaryColour,v.BodyType,v.[IsSpecialVehicle],
                    vpl.[Description] AS [PriceDescription], vp.[Amount], vpl.Id AS [PriceLevelId]
                FROM [Vehicle] v
                    INNER JOIN [VehiclePrice] vp ON vp.[VehicleId]=v.[Id]
                    INNER JOIN [VehiclePriceLevel] vpl ON vpl.Id=vp.VehiclePriceLevelId
                WHERE
                    v.[Status]=7 AND v.[Publication] >= 11 AND v.[Publication] <= 13 AND vp.[Approved]=1 AND vp.[VehiclePriceLevelId] IN (1,2,10)
            ) x
            pivot 
            (
                MAX([Amount])
                FOR [Vehicle] IN (' + @cols + N')
            ) p '

EXEC sp_executesql @query;

提前致谢...

2 个答案:

答案 0 :(得分:1)

此错误是因为您尝试选择超过一个SELECT语句中允许的最大列数的列数,请参阅Maximum Capacity Specifications for SQL Server

  

每个SELECT语句的最大列数4,096

问题在于此处,查询返回的列:

SELECT ',' + QUOTENAME(v.[Id]) 
                    FROM [Vehicle] v
                    INNER JOIN [Vehicle] ...
                    ...

大于4096,即你有超过4096辆车辆。您可以运行此查询并确保返回的列少于4096。

您还可以添加DISTINCT以消除重复项(可能是问题STUFF((SELECT DISTINCT ',' + QUOTENAME(v.[Id])并删除GROUP BY

GROUP BY v.[Id],v.[Category],v.[Status],
   v.[Publication],v.[VIN], v.[EngineNumber],v.MMCode,
  v.[Year],v.Kilometers,v.Fuel,v.Transmission,
  v.Colour,v.PrimaryColour,v.BodyType,v.[IsSpecialVehicle], 
  vpl.[Description], vp.[Amount], vpl.Id

答案 1 :(得分:0)

由于你没有真正包含你的架构,这只是猜测..但我认为你正在寻找这个......这会给你一个PriceDescription的列和每个车辆的一列ID。对于PriceDescription,每一行的每一行都有PriceDescription和最大金额...车辆ID可能不具有列标题的描述性,因此您可能想要更改它。

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

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(v.[Id]) 
                    FROM [Vehicle] v
                    WHERE v.[Status]=7 AND v.[Publication] >= 11 AND v.[Publication] <= 13
                    ORDER BY v.[Id]
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @query = N'SELECT PriceDescription, ' + @cols + N' from 
            (
                SELECT
                    v.[Id] as Vehicle, vpl.[Description] AS [PriceDescription], vp.[Amount]
                FROM [Vehicle] v
                    INNER JOIN [VehiclePrice] vp ON vp.[VehicleId]=v.[Id]
                    INNER JOIN [VehiclePriceLevel] vpl ON vpl.Id=vp.VehiclePriceLevelId
                WHERE
                    v.[Status]=7 AND v.[Publication] >= 11 AND v.[Publication] <= 13 AND vp.[Approved]=1 AND vp.[VehiclePriceLevelId] IN (1,2,10)
            ) x
            pivot 
            (
                MAX([Amount])
                FOR [Vehicle] IN (' + @cols + N')
            ) p '

EXEC sp_executesql @query;