MS SQL汇总百分比列

时间:2011-09-28 10:35:57

标签: sql sql-server

我使用汇总来汇总包含字段companysalescostmargin的表格。这给了我汇总的列,但我需要显示保证金的总结百分比,作为每个公司摘要的销售百分比。

这是我目前的陈述:

SELECT coalesce(CAST(Company AS VARCHAR(30)), 'Grand Total:') AS Company,
       SUM(Sales)                                             AS 'TOTSALES',
       SUM(CostVal)                                           AS 'TOTCOST',
       SUM(MarginVal)                                         AS 'TOTMAR'
FROM   DailySalesSum
GROUP  BY Company WITH ROLLUP;  

1 个答案:

答案 0 :(得分:0)

SELECT coalesce(CAST(Company AS VARCHAR(30)), 'Grand Total:') AS Company,
       SUM(Sales)                                             AS 'TOTSALES',
       SUM(CostVal)                                           AS 'TOTCOST',
       SUM(MarginVal)                                         AS 'TOTMAR',
       100.0 * SUM(MarginVal) / SUM(Sales)                    AS 'MARGINPERCENT'
FROM   DailySalesSum
GROUP  BY Company WITH ROLLUP;

如同马丁所说,并将其作为额外字段添加到您的陈述中,如上所示。