没有聚合的SQL Pivot-重复条目

时间:2018-07-03 11:16:51

标签: sql pivot

我具有以下表格结构

Create Table PivotTabSample (
    ID INT IDENTITY(100000001,1) NOT NULL,
    Product Nvarchar(30),
    StoreNumber INT,
    StoreSalesEstimate DECIMAL,
    StoreSalesActual DECIMAL
)

样本值填充有以下插入内容-

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0001','101',500,450)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0002','101',300,350)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0003','101',50,61)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0004','101',100,900)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0005','101',10,9)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0001','102',1500,1450)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0002','102',1400,3500)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0003','102',150,610)

INSERT INTO [dbo].[PivotTabSample]
    ([Product],[StoreNumber],[StoreSalesEstimate],[StoreSalesActual])
 VALUES ('P0004','102',800,900)

我正在尝试对此数据集进行PIVOT。然后运行以下查询-

SELECT Product, [101], [102]
FROM PivotTabSample 
PIVOT
(   min(StoreSalesActual) 
    FOR StoreNumber IN ([101],[102])
)AS p

获得的结果不是所希望的方式-我没有获得产品和商店详细信息。每个商店有两个产品行。 我不要那个。这里不需要聚合,但是我不确定如何实现。

我想要这样的结果-

Product   101ActualSales 101EstimatedSales 102ActualSales 102estimatedSales
P001      100              101                90            91
P002      
P003      
P001      

请告知。

2 个答案:

答案 0 :(得分:1)

WITH CTE
AS (SELECT Product,
           ISNULL([101], 0) AS '101ActualSales',
           ISNULL([102], 0) AS '102ActualSales',
           0 AS '101EstimatedSales',
           0 AS '102EstimatedSales'
    FROM PivotTabSample
        PIVOT
        (
            SUM(StoreSalesActual)
            FOR StoreNumber IN ([101], [102])
        ) AS p)
SELECT product,
       SUM([101ActualSales]) AS [101actualsales],
       SUM([101EstimatedSales]) AS [101EstimateSales],
       SUM([102ActualSales]) AS [102actualsales],
       SUM([102EstimatedSales]) AS [102EstimateSales]
FROM
(
    SELECT *
    FROM CTE
    UNION
    SELECT pt.Product,
           0 AS '101ActualSales',
           0 AS '102ActualSales',
           ISNULL([101], 0) AS '101EstimateSales',
           ISNULL([102], 0) AS '102EstimateSales'
    FROM PivotTabSample
        PIVOT
        (
            SUM(StoreSalesEstimate)
            FOR StoreNumber IN ([101], [102])
        ) AS pt
) AS x
GROUP BY product;

http://rextester.com/GFID38296

答案 1 :(得分:0)

在工作中遇到了一个类似的问题,即我不得不修改一个与您的输出相同的数据透视图,我发现将其包装到CTE中会有所帮助。我只需要弄清楚为什么,但是我怀疑添加 StoreSalesActual 可以帮助...

WITH cte AS
(
SELECT Product, 
StoreNumber,
StoreSalesActual
FROM #PivotTabSample
)
SELECT DISTINCT Product, [101], [102]
FROM cte
PIVOT
(   
MAX(StoreSalesActual) 
FOR StoreNumber IN ([101],[102])
)AS p
GROUP BY Product, [101], [102]