簡體   English   中英

如何在空行后添加總行?

[英]How to add the total row after the blank line?

如何在最后添加總行和所有總數? 當我運行查詢時,我想查看空白行之后的行,以及總計。 請看下表。 如果該列不可求和,則該列也應為空白。

    select 
    t.Business_Unit_Description,
    billable_trades, 
    @rate rate, 
    billable_trades * @rate as charge,
    isnull(c.comm_adjustments, 0) as commission_adjustments,
    0.3 commission_adj_rate,
    isnull(c.comm_adjustments, 0) * 0.3 * -1 as credit,
    ((billable_trades * @rate) + (isnull(c.comm_adjustments, 0) * 0.3 * -1)) as total
  from   
       (
       select Business_Unit_Description, sum(billable_trades) as billable_trades
       from   #cte_combined
       group by Business_Unit_Description
       ) t
       left outer join #cte_comm_adj c on c.Business_Unit_Description = 
    t.Business_Unit_Description
   order by t.Business_Unit_Description

數據如下所示: 在此處輸入圖像描述

假設您的表格包含有關公司、費用、佣金的信息,並且您希望獲得每個公司以及每個列的總數。 由於您沒有提供任何表架構或數據樣本,我創建了一個表,其中填充了您問題中的數據。

我建議下一個查詢:

create table test
(
    Company varchar(20) not null,
    Charge int not null,
    Commission int not null
)

insert into test
values 
('A', 50, 20),
('B', 23, 10),
('C', 80, 23),
('D', 60, 12)

select Company, Charge, Commission, SUM(Charge + Commission) OVER (PARTITION BY Company ORDER BY Company ASC) as total 
from test
union all
select 'Total', SUM(Charge), SUM(Commission), SUM(Charge + Commission)
from test

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM