SQL 服务器动态数据透视有问题

Trouble with 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

你可以尝试这样的事情。

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 子句。您的过滤器中也没有通配符。在这种情况下使用 = 会更好更干净。