显示分组总和占总和的百分比

时间:2018-11-10 00:46:52

标签: sql sql-server tsql

我目前有类似

的结果
total sales | total cost | total profit | department
----------------------------------------------------
100          50            50             A
80           20            60             B
250          120           130            C

使用表中的列

发票明细

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的总数。我尝试过类似

中的建议

Percentage from Total SUM after GROUP BY SQL Server

但这似乎对我不起作用。我遇到绑定错误。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

这应该有效:

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 

答案 1 :(得分:0)

您可以使用窗口功能来做到这一点:

with t as (
      <your query here>
     )
select t.*,
       profit * 100.0 / sum(profit) over () as profit_percentage
from t;
相关问题