sql求合计值(rollup函数应用)

CREATE TABLE `test1` (
  `id` varchar(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `count` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `test2` (
  `id` varchar(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

-- oracle、mysql通用
select case when t2.id = '0' then '合计' else t2.id end id, sum(count) count from test1 t1,test2 t2 
where t1.id = t2.name group by t2.id;
-- mysql
select case when id is null then '合计' else id end id, sum(count) from test1 GROUP BY id with ROLLUP;

-- oracle
select id,sum(count) from test1 group by rollup(id);

 

posted @ 2020-07-22 09:31  小朝0  阅读(1299)  评论(0编辑  收藏  举报