sql - 显示分组 SUM 占总 SUM 的百分比
问题描述
我目前有类似的结果
total sales | total cost | total profit | department
----------------------------------------------------
100 50 50 A
80 20 60 B
250 120 130 C
使用表中的列
Invoice_Itemized
itemnum | costper | priceper | quantity | invoice_number
--------------------------------------------------------
发票_总计
invoice_number | datetime
---------------------------
存货
itemnum | dept_id
------------------
部门
dept_id | description
----------------------
使用以下代码
select sum(invoice_itemized.priceper* invoice_itemized.quantity) as "Total Sales",
sum(invoice_itemized.quantity*inventory.cost) as "Total Cost",
sum(invoice_itemized.priceper* invoice_itemized.quantity)-
sum(invoice_itemized.quantity*inventory.cost) as "Total Profit",
departments.description as Department
from invoice_itemized, invoice_totals, inventory, departments
where invoice_itemized.invoice_number=invoice_totals.invoice_number
and year(invoice_totals.datetime)=2018 and month(invoice_totals.datetime)=10
and inventory.itemnum=invoice_itemized.itemnum
and inventory.dept_id=departments.dept_id
and departments.description<>'shop use'
and departments.description<>'none'
and departments.description<>'ingredients'
group by departments.description
order by "total profit" desc
我想要这样的结果
total sales | total cost | total profit | percentage total profit | department
-------------------------------------------------------------------------------
100 50 50 20.83 A
80 20 60 25 B
250 120 130 54.17 C
我遇到的问题是我试图将 SUM-SUM 的分组结果除以相同 SUM-SUM 的总数。我已经尝试过类似于在
但这似乎对我不起作用。我遇到了绑定错误。有什么建议么?
解决方案
这应该有效:
Select q.[Total Sales],
q.[Total Cost],
q.[Total Profit],
q.Total Profit] / q1.Total Profit] as [Percentage Total Profit],
q.Department
from (
select sum(invoice_itemized.priceper* invoice_itemized.quantity) as [Total Sales],
sum(invoice_itemized.quantity*inventory.cost) as [Total Cost],
sum(invoice_itemized.priceper* invoice_itemized.quantity) - sum(invoice_itemized.quantity*inventory.cost) as [Total Profit],
departments.description as Department
from invoice_itemized, invoice_totals, inventory, departments
where invoice_itemized.invoice_number=invoice_totals.invoice_number
and year(invoice_totals.datetime)=2018 and month(invoice_totals.datetime)=10
and inventory.itemnum=invoice_itemized.itemnum
and inventory.dept_id=departments.dept_id
and departments.description<>'shop use'
and departments.description<>'none'
and departments.description<>'ingredients'
group by departments.description) q
join (
select sum(t.[Total Profit]) as [Total Profit]
from (select sum(invoice_itemized.priceper* invoice_itemized.quantity) as [Total Sales],
sum(invoice_itemized.quantity*inventory.cost) as [Total Cost],
sum(invoice_itemized.priceper* invoice_itemized.quantity) - sum(invoice_itemized.quantity*inventory.cost) as [Total Profit],
departments.description as Department
from invoice_itemized, invoice_totals, inventory, departments
where invoice_itemized.invoice_number=invoice_totals.invoice_number
and year(invoice_totals.datetime)=2018 and month(invoice_totals.datetime)=10
and inventory.itemnum=invoice_itemized.itemnum
and inventory.dept_id=departments.dept_id
and departments.description<>'shop use'
and departments.description<>'none'
and departments.description<>'ingredients'
group by departments.description) t
) q1 on q1.[Total Profit] = q1.[Total Profit]
order by q.[Total Profit] desc
推荐阅读
- python - 查找数组的最大可能值
- c# - 有没有办法在 ML.NET 中保存经过训练的 FastTree-Algorithm -Model 的结果?
- c++ - C++ 新手,想知道如何使用 VS Code 2019 播放音频文件
- python - 在 Windows 10 上安装 Turbodbc
- angular - 将 Angular 8 应用程序本地部署到 IIS 错误,出现“服务器以非 Javascript MIME 类型的“text/html”响应?
- python - 创建一个字符串数据框列,该列是数据框中现有列的拼接
- workflow - 如何从 Informatica 的子文件夹中删除不需要的工作流?
- docker - 在一组外部和内部主机上创建 docker swarm 覆盖网络
- mysql - 检查日期的日期名称是否在日期名称列数据中
- python - 使用 python 的 Azure 存储队列数据开销