我在MS Access中有一个cros标签查询,我希望在T-SQL中进行复制, T-SQL表'#tmpZSPO_DMD'有Part,Location,Qty,FiscalMonthPeriod。当我运行时,数据如下所示。
Part LOCATION Qty FiscalMonthPeriod
123 4040_0086 1 CON00
123 4040_0086 1 CON00
123 4200_0010 1 CON00
123 2070_0060 2 CON01
123 2080_0061 1 CON01
123 4040_0070 1 CON02
123 4040_0070 2 CON02
123 4040_0086 1 CON02
123 2020_0060 2 CON03
123 2020_0064 1 CON03
123 2040_0060 1 CON03
123 4040_0061 1 CON03
123 4040_0061 1 CON03
123 4040_0069 1 CON03
123 4040_0070 1 CON03
我希望获得以下结果。
Part LOCATION CON00 CON01 CON02 CON03
123 2020_0060 2
123 2020_0064 1
123 2040_0060 1
123 2070_0060 2
123 2080_0061 1
123 4040_0061 2
123 4040_0069 1
123 4040_0070 3 1
123 4040_0086 2 1
123 4200_0010 1
答案 0 :(得分:0)
一个非常简单的PIVOT将完成这项工作。
SELECT *
FROM
(
SELECT Part, LOCATION, Qty, FiscalMonthPeriod
FROM @Table
) t
PIVOT
(
SUM(Qty)
FOR FiscalMonthPeriod IN ([CON00], [CON01], [CON02], [CON03])
) p
答案 1 :(得分:0)
SELECT tm.PART, tm.Location,
SUM(IIF(tm.FiscalMonthPeriod= 'CON00', [Sum], NULL)) As CON00,
SUM(IIF(tm.FiscalMonthPeriod= 'CON01', [Sum], NULL)) As CON01,
SUM(IIF(tm.FiscalMonthPeriod= 'CON02', [Sum], NULL)) As CON02,
SUM(IIF(tm.FiscalMonthPeriod= 'CON03', [Sum], NULL)) As CON03
FROM #tmpZSPO_DMD tm
GROUP BY tm.PART, tm.Location;
答案 2 :(得分:0)
Select Part
,LOCATION
,ISNULL(CON00 , 0) AS CON00
,ISNULL(CON01 , 0) AS CON01
,ISNULL(CON02 , 0) AS CON02
,ISNULL(CON03 , 0) AS CON03
FROM tablename T
PIVOT (SUM(Qty)
FOR FiscalMonthPeriod
IN(CON00 , CON01, CON02, CON03)
)p