簡體   English   中英

在底部添加總計

[英]Add a total at the bottom

我需要在表格底部添加總計 在我的情況下這可能嗎?

select country, count(*) from customer
group by country

Country         Count
  USA              5  
  UK              10
 Canada           15 
 Russia           25
                  55  (Requested from SO community) 

使用rollup()

select country, count(*) 
from customer
group by rollup (country )

如果還需要標簽“總計”,則可以使用grouping功能:

select case 
          when grouping(country) = 1 then 'Total' 
          else country 
       end as country, 
       count(*) 
from customer
group by rollup (country )

在線示例: http : //rextester.com/PKFE63954

您可以使用類似方法人工生成一行

select country
    ,count(*) 
from customer
group by country

UNION ALL

SELECT 'Total'
    ,COUNT(*)
FROM customer

盡管這會影響您以后對該結果集進行的任何計算,因為它是數據中的新行。

就像是:

SELECT country
    , count(´1)
FROM customer
GROUP BY country

UNION

SELECT 'TOTAL'
    , count(1)
FROM customer;

您可以將另一列添加到名為Total的行中

        DECLARE @Total int = 0;
        SET @Total = (SELECT COUNT(*) FROM customer);

        SELECT country, COUNT(*), [Total] = @Total
        FROM customer
        GROUP BY country

暫無
暫無

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

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