如何显示这样的Sub Total值?

时间:2018-10-21 17:59:52

标签: sql sql-server database sql-server-2005

我有一个这样的表:-

Branch       SL       Month     Amount1 Amount2 Amount3
    A         1       January    100       0        0
    A         2       February     0       0        0
    A         3       March       80       0        0
    A         4       April        0      10       10
    A         5       May          0       0        0
    A         6       June         0       0        0
    A         7       July         0       0       10
    A         8       August       0      10        0
    A         9       September    0      10        0
    A        10       October     40       0       10
    A        11       November    10      10       10
    A        12       December     0      10       10
    B         1       January      0       0       10
    B         2       February    10      10       10
    B         3       March        0       0       10

现在,我试图显示每个分支的小计,这将给出这样的输出,其中“ Subtotal”在每12行之后显示一次,并且在“月”列“ Subtotal”下面是书面。并且前2列将为空白,我正在尝试使用UNION ALL来实现,但失败了。

Branch  SL    Month   Amount1     Amount2    Amount3
A        1    January     100           0          0
A        2    February      0           0          0
A        3    March        80           0          0
A        4    April         0          10         10
A        5    May           0           0          0
A        6    June          0           0          0
A        7    July          0           0         10
A        8    August        0          10          0
A        9    September     0          10          0
A        10   October      40           0         10
A        11   November     10          10         10
A        12   December      0          10         10
              Subtotal    230          50         50
B        1    January       0           0         10
B        2    February     10          10         10
B        3    March         0           0         10

2 个答案:

答案 0 :(得分:2)

一种方法使用grouping sets,但是您需要一个聚合查询。您可以这样写:

select Branch, SL, Month, sum(Amount1), sum(Amount2), sum(Amount3)
from t
group by grouping sets ( (Branch, SL, Month), (branch) );

grouping sets仅从2008年起可用。在早期版本中,您可以执行以下操作:

select t.*
from ((select branch, sl, month, amount1, amount2, amount3
       from t
      ) union all
      (select Branch, null, 'Subtotal', sum(Amount1), sum(Amount2), sum(Amount3)
       from t
       group by branch
      )
     ) t
order by branch,
         (case when sl is not null then 1 else 2 end),
         sl;

答案 1 :(得分:0)

尝试

CREATE TABLE T
(
   Branch VARCHAR(1),
   SL INT,
   Month VARCHAR(3),
   Amount1 INT,
   Amount2 INT,
   Amount3 INT
);

INSERT INTO T VALUES
('A', 1, 'Jan', 10, 0, 10),
('A', 2, 'Feb', 20, 0, 20),
('B', 1, 'Jan', 5, 5, 5),
('B', 2, 'Feb', 20, 0, 20),
('C', 1, 'Jan', 55, 44, 33);

WITH CTE AS
(
  SELECT *
  FROM T
  UNION ALL
  SELECT Branch + ' SubTotal', NULL, 'SubTotal', SUM(Amount1), SUM(Amount2), SUM(Amount3)
  FROM T
  GROUP BY Branch
)
SELECT Branch,
       SL,
       Month,
       Amount1,
       Amount2,
       Amount3
FROM CTE
ORDER BY Branch;

结果:

+------------+----+----------+---------+---------+---------+
|   Branch   | SL |  Month   | Amount1 | Amount2 | Amount3 |
+------------+----+----------+---------+---------+---------+
| A          |  1 | Jan      |      10 |       0 |      10 |
| A          |  2 | Feb      |      20 |       0 |      20 |
| A SubTotal |    | SubTotal |      30 |       0 |      30 |
| B          |  1 | Jan      |       5 |       5 |       5 |
| B          |  2 | Feb      |      20 |       0 |      20 |
| B SubTotal |    | SubTotal |      25 |       5 |      25 |
| C          |  1 | Jan      |      55 |      44 |      33 |
| C SubTotal |    | SubTotal |      55 |      44 |      33 |
+------------+----+----------+---------+---------+---------+

如果您确实需要将BranchSL空白('')设为空白,则可以

WITH CTE AS
(
  SELECT Seq = Branch + 'X1',*
  FROM T
  UNION ALL
  SELECT Branch + 'X2', '', NULL, 'SubTotal', SUM(Amount1), SUM(Amount2), SUM(Amount3)
  FROM T
  GROUP BY Branch
)
SELECT Branch,
       SL,
       Month,
       Amount1,
       Amount2,
       Amount3
FROM CTE
ORDER BY Seq;

并且结果与您的预期结果相同

+--------+----+----------+---------+---------+---------+
| Branch | SL |  Month   | Amount1 | Amount2 | Amount3 |
+--------+----+----------+---------+---------+---------+
| A      |  1 | Jan      |      10 |       0 |      10 |
| A      |  2 | Feb      |      20 |       0 |      20 |
|        |    | SubTotal |      30 |       0 |      30 |
| B      |  1 | Jan      |       5 |       5 |       5 |
| B      |  2 | Feb      |      20 |       0 |      20 |
|        |    | SubTotal |      25 |       5 |      25 |
| C      |  1 | Jan      |      55 |      44 |      33 |
|        |    | SubTotal |      55 |      44 |      33 |
+--------+----+----------+---------+---------+---------+