计算每种产品类型的游戏数量

时间:2016-06-07 07:11:59

标签: sql sql-server-2008

如何根据NumOfGames%等每种产品类型计算% = TOTAL/PERPRODUT_TYPE。 这是我对TOTAL NumOfGames

的查询
 SELECT     
        DPT.[Name] [ProductType],
        SUM([FinishedGameCycleCount]) [NumOfGames]  
    FROM [WarehouseMgmt].[FactGameAgr] FWA
    JOIN [WarehouseMgmt].[DimPlayer] DPL ON FWA.[PlayerId] = DPL.[Id]
    JOIN [WarehouseMgmt].[DimGame] DG ON FWA.[GameId] = DG.[Id]
    JOIN [WarehouseMgmt].[DimProductType] DPT ON DPT.Id = FWA.ProductTypeId
    WHERE  [WarehouseMgmt].[GetDateTimeFromTimeId](TimeId) >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
    GROUP BY DPT.[Name]

1 个答案:

答案 0 :(得分:1)

使用SUM()OVER()

SELECT     
        DPT.[Name] [ProductType],
        SUM([FinishedGameCycleCount]) [NumOfGames],
        SUM([FinishedGameCycleCount]) *100. / SUM(SUM([FinishedGameCycleCount])) OVER() [percentage]
    FROM [WarehouseMgmt].[FactGameAgr] FWA
    JOIN [WarehouseMgmt].[DimPlayer] DPL ON FWA.[PlayerId] = DPL.[Id]
    JOIN [WarehouseMgmt].[DimGame] DG ON FWA.[GameId] = DG.[Id]
    JOIN [WarehouseMgmt].[DimProductType] DPT ON DPT.Id = FWA.ProductTypeId
    WHERE  [WarehouseMgmt].[GetDateTimeFromTimeId](TimeId) >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
    GROUP BY DPT.[Name]