SQL Server Dynamic Pivot出现问题

时间:2015-04-03 04:29:58

标签: sql-server dynamic pivot

我创建了一个Static Pivot就好了,但是当我使用Dynamic版本时,我不断收到错误,例如HAVING子句中的语法不正确以及其他错误,例如无效标识符。

 Select Color,Gender,Size,PreBuyQty,[5277],[5294],[5365]FROM
(SELECT pb.Color, o.OrderID, pb.Gender, pb.SizeNo, pb.Size, pb.PreBuyQty, od.Quantity
FROM         Orders AS o INNER JOIN
                      OrderDetails AS od ON o.OrderID = od.OrderID INNER JOIN
                      ProductsPreBuy AS pb ON od.ProductCode = pb.productcode
GROUP BY pb.Color, pb.Gender, pb.SizeNo, pb.Size, pb.PreBuyQty, od.Quantity, o.OrderID
HAVING     (pb.Color LIKE N'Black') AND (pb.Gender LIKE N'Men')
) As T
PIVOT
(
 SUM(Quantity) FOR OrderID IN ([5277],[5294],[5365])
) As P;

以下是我迄今为止动态检索的内容。

DECLARE @cols AS NVARCHAR(MAX),
        @query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(o.OrderID)
            FROM         Orders AS o INNER JOIN
                      OrderDetails AS od ON o.OrderID = od.OrderID INNER JOIN
                      ProductsPreBuy AS pb ON od.ProductCode = pb.productcode
         GROUP BY pb.Color, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity, o.OrderID
         HAVING     (pb.Color LIKE 'Black') AND (pb.Gender LIKE 'Men')
         FOR XML PATH(''), TYPE
         ).value('.', 'NVARCHAR(MAX)')
         ,1,1,'')

Set @query = 'SELECT pb.Color, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity,' + @cols + ' FROM
(
SELECT pb.Color, o.OrderID, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity
        FROM Orders AS o INNER JOIN OrderDetails AS od ON o.OrderID = od.OrderID INNER JOIN ProductsPreBuy AS pb ON od.ProductCode = pb.productcode
GROUP BY pb.Color, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity, o.OrderID
HAVING     (pb.Color LIKE 'Black') AND (pb.Gender LIKE 'Men')
) x 
pivot
(
    Sum(Quantity)
    for OrderID in (' + @cols + ')
    ) p '
execute @query

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(o.OrderID)
            FROM         Orders AS o INNER JOIN
                      OrderDetails AS od ON o.OrderID = od.OrderID
                      INNER JOIN ProductsPreBuy AS pb ON od.ProductCode = pb.productcode
         WHERE (pb.Color = 'Black') AND (pb.Gender = 'Men')
         GROUP BY pb.Color, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity, o.OrderID
         FOR XML PATH(''), TYPE
         ).value('.', 'NVARCHAR(MAX)')
         ,1,1,'')

Set @query = 'SELECT Color, Gender, Size, PreBuyQty,' + @cols + ' FROM
(
SELECT pb.Color, o.OrderID, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity
        FROM Orders AS o
        INNER JOIN OrderDetails AS od 
            ON o.OrderID = od.OrderID 
        INNER JOIN ProductsPreBuy AS pb 
            ON od.ProductCode = pb.productcode
WHERE (pb.Color = ''Black'') AND (pb.Gender = ''Men'')
GROUP BY pb.Color, pb.Gender, pb.Size, pb.PreBuyQty, od.Quantity, o.OrderID
) x 
PIVOT
(
    Sum(Quantity)
    for OrderID in (' + @cols + ')
    ) p '
execute @query

在您的查询中,您不需要使用HAVING使用数据进行过滤,因为您的过滤器没有聚合函数。你可以使用一个简单的WHERE条款。您的过滤器中也没有通配符。在这种情况下使用=更好更清洁。