透视分组?

时间:2016-02-23 20:49:07

标签: tsql pivot-table ssrs-2008-r2

我正在为SSRS报告构建一个查询,我有一个位置和数量的支点。我为每个位置获得两行,但是当我转动时,我希望最终结果是一行。

SELECT * 
FROM ( SELECT DISTINCT  ItemNmbr ,
          ITEMDESC ,
          Location ,
          QtyOnhand ,
          QtyOnOrder ,
          QtyBackordered ,
          TotalInventory ,
          EXTDCOST ,
          InventoryValue 
          FROM #ssrsFinal
) AS p PIVOT
(SUM(QTYOnHand) FOR Location IN ( Loc1, Loc2, HUB, INLAND, Loc, [OLD Loc],[IN TRANSIT] )
) AS pvt;

我的结果是两行,我怎么才能得到它? enter image description here

我已经尝试将数据结果插入到临时表中然后进行分组,但它不会带来选择中的位置。

Pivot之前的结果。 3个不同的地点。enter image description here

1 个答案:

答案 0 :(得分:2)

我认为这就是你要找的东西:

您已经创建了数据透视表,您只需要相应地聚合数据透视表的结果。

假设:按ItemNmbr,ITEMDESC,EXTDCOST,Loc1,Loc2,HUB,INLAND,Loc,[OLD Loc],[IN TRANSIT]分组

SELECT    ItemNmbr ,
          ITEMDESC ,
          QtyOnOrder=SUM(QtyOnOrder) ,
          QtyBackordered=SUM(QtyBackordered) ,
          TotalInventory=SUM(TotalInventory) ,
          EXTDCOST,
          InventoryValue = SUM(InventoryValue) ,
          Loc1, 
          Loc2, 
          HUB, 
          INLAND, 
          Loc, 
          [OLD Loc],
          [IN TRANSIT]
FROM ( SELECT DISTINCT  ItemNmbr ,
          ITEMDESC ,
          Location ,
          QtyOnhand ,
          QtyOnOrder ,
          QtyBackordered ,
          TotalInventory ,
          EXTDCOST ,
          InventoryValue 
          FROM #ssrsFinal
) AS p PIVOT
(SUM(QTYOnHand) FOR Location IN ( Loc1, Loc2, HUB, INLAND, Loc, [OLD Loc],[IN TRANSIT] )
) AS pvt
GROUP BY  ItemNmbr ,
          ITEMDESC ,
          EXTDCOST,
          Loc1, 
          Loc2, 
          HUB, 
          INLAND, 
          Loc, 
          [OLD Loc],
          [IN TRANSIT] ; 

回应评论:

样本数据集:

CREATE TABLE #ssrsFinal (ItemNmbr VARCHAR(10),
          ITEMDESC VARCHAR(100),
          Location VARCHAR(10),
          QtyOnhand INT,
          QtyOnOrder INT,
          QtyBackordered INT,
          TotalInventory INT,
          EXTDCOST DECIMAL(20,5),
          InventoryValue  DECIMAL(20,2))

INSERT INTO #ssrsFinal
VALUES 
('94185BJGD','SS MESH ROAST PAN, 2X GRILL CLAWS, STAM HNDL, GD','Something',0,0,0,0,14.18000,0),
('94185BJGD','SS MESH ROAST PAN, 2X GRILL CLAWS, STAM HNDL, GD','Something',0,0,0,0,14.18000,0),
('94185BJGD','SS MESH ROAST PAN, 2X GRILL CLAWS, STAM HNDL, GD','Something',3,0,0,3,14.18000,42.54)

enter image description here

在回复评论“...为位置插入值而不是”某些东西“尝试从示例中添加不同的位置然后再次运行查询时,它将拉回两行。我尝试了INLAND,HUB和LOC“:

SELECT    ItemNmbr ,
          ITEMDESC ,
          QtyOnOrder=SUM(QtyOnOrder) ,
          QtyBackordered=SUM(QtyBackordered) ,
          TotalInventory=SUM(TotalInventory) ,
          EXTDCOST,
          InventoryValue = SUM(InventoryValue) ,
          Loc1=SUM(Loc1), 
          Loc2=SUM(Loc2), 
          HUB=SUM(HUB), 
          INLAND=SUM(INLAND),
          Loc=SUM(Loc), 
          [OLD Loc]=SUM([OLD Loc]),
          [IN TRANSIT]=SUM([IN TRANSIT])
FROM ( SELECT DISTINCT  ItemNmbr ,
          ITEMDESC ,
          Location ,
          QtyOnhand ,
          QtyOnOrder ,
          QtyBackordered ,
          TotalInventory ,
          EXTDCOST ,
          InventoryValue 
          FROM #ssrsFinal
) AS p PIVOT
(SUM(QTYOnHand) FOR Location IN ( Loc1, Loc2, HUB, INLAND, Loc, [OLD Loc],[IN TRANSIT] )
) AS pvt
GROUP BY  ItemNmbr ,
          ITEMDESC ,
          EXTDCOST  ; 

结果:

enter image description here