首页 > 开发 > 综合 > 正文

用SQL语句生成带有小计合计的数据集脚本

2024-07-21 02:13:56
字体:
来源:转载
供稿:网友

使用sql语句生成带有小计合计的数据集:

测试用户: scott

测试用表: dept,emp

//////////////////////////////////

//检索出需要进行统计的数据集

select dept.dname,emp.job,sal from emp,dept

where emp.deptno=dept.deptno;

//////////////////////////////////

//根据部门名称以及职位进行汇总,并为每个部门

生成'小计',最后生成'合计'.

select

decode(grouping(dept.dname),1,'合计:',dept.dname)dname,

decode(grouping(emp.job)+grouping(dept.dname),1,'小计:',emp.job)job,sum(sal) sum_sal from emp,dept where emp.deptno=dept.deptno group by rollup(dept.dname,emp.job);

运行结果如下:

sql> select dept.dname,emp.job,sal from emp,d

dname job sal

-------------- --------- ----------

research clerk 800

sales salesman 1600

sales salesman 1250

research manager 2975

sales salesman 1250

sales manager 2850

accounting manager 2450

research analyst 3000

accounting president 5000

sales salesman 1500

research clerk 1100

dname job sal

-------------- --------- ----------

sales clerk 950

research analyst 3000

accounting clerk 1300


已选择14行。


sql> select

2 decode(grouping(dept.dname),1,'合计:',de

3 decode(grouping(emp.job)+grouping(dept.d

ept where emp.deptno=dept.deptno group by rol


dname job sum_sal

-------------- --------- ----------

accounting clerk 1300

accounting manager 2450

accounting president 5000

accounting 小计: 8750

research analyst 6000

research clerk 1900

research manager 2975

research 小计: 10875

sales clerk 950

sales manager 2850

sales salesman 5600


dname job sum_sal

-------------- --------- ----------

sales 小计: 9400

合计: 29025


已选择13行。


sql>



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表