生成 sql 份报告

building sql reports

我正在寻找按位置显示预订摘要的报告,我想查看的数据是销售额、销售订单 (gm) 和利润百分比。 采用与此类似的格式

Sales Orders - Source Loc ID    Sales Orders - Sales Location   Sale Amt    Sales Orders - Gross Margin$    Sales Orders - Gross Margin%    Profit%(p21)
10  Mobile  ,892.00   3.00     25.00%  25.00%
20  Louisiana   ,834.90   ,966.97   20.00%  0.00%
30  Florida ,547.32   ,324.01   37.30%  0.00%
40  Birmingham  8.48     .65  9.60%   0.00%
50  Northeast   ,336.52  ,067.30   20.00%  0.00%

销售额将是所有销售额的总和,与该地点的毛利率相同。该百分比将是该位置所有利润百分比的平均值。

你必须使用GROUP BY and Aggregate Function

下面的查询应该会给你想要的结果:

SELECT [Sales Orders - Source Loc ID],[Sales Orders - Sales Location],
       SUM([Sale Amt]) AS [Sale Amt],
       SUM([Sales Orders - Gross Margin$]) AS [Sales Orders - Gross Margin$],
       AVG([Sales Orders - Gross Margin%]) AS [Sales Orders - Gross Margin%],
       AVG([Profit%(p21)]) AS [Profit%(p21)]
FROM Table
GROUP BY [Sales Orders - Source Loc ID],[Sales Orders - Sales Location]