小计_合计_统计
--例如 with a as ( select 'a' A, 'b' B ,5 C ,2 D from dual union all select 'w', 'b', 1, 3 from dual union all select 'x' ,'x' ,3 ,1 from dual ) select decode(grouping(A),1,'合計',A) A ,B,sum(C) C,SUM(D) D from a group by rollup ((a,b))
create table [tb]([客户编码] varchar(10),[客户名称] varchar(10),[数量] int) insert [tb] select '001','A',2 union all select '001','A',3 union all select '001','A',4 union all select '002','B',1 union all select '002','B',2 --统计 select * from (select * from tb union all select 客户编码 , 客户名称 = '小计' , sum(数量) 数量 from tb group by 客户编码 union all select 客户编码 = '', 客户名称 = '合计' , sum(数量) 数量 from tb ) t order by case 客户编码 when '' then 2 else 1 end ,客户编码 , case 客户名称 when '小计' then 2 else 1 end drop table tb /* 客户编码 客户名称 数量 ---------- ---------- ----------- 001 A 2 001 A 3 001 A 4 001 小计 9 002 B 1 002 B 2 002 小计 3 合计 12 (所影响的行数为 8 行) */ create table tb (date char(10),col varchar(10)) insert tb select '2005-05-09','胜' insert tb select '2005-05-09','胜' insert tb select '2005-05-09','负' insert tb select '2005-05-09','负' insert tb select '2005-05-10','胜' insert tb select '2005-05-10','负' insert tb select '2005-05-10','负' select date ,sum(case when col='胜' then 1 else 0 end) as [胜] ,sum(case when col='负' then 1 else 0 end) as 负 from tb group by date date 胜 负 ---------- ----------- ----------- 2005-05-09 2 2 2005-05-10 1 2 (2 行受影响)
以上代码都是转自他人博客
ORACLE ROLLUP和CUBE的使用:(转自:http://blog.csdn.net/wanghai__/article/details/4817920)
ROLLUP,是GROUP BY子句的一种扩展,可以为每个分组返回小计记录以及为所有分组返回总计记录。
CUBE,也是GROUP BY子句的一种扩展,可以返回每一个列组合的小计记录,同时在末尾加上总计记录。
在文章的最后附上了相关表和记录创建的脚本。
1、向ROLLUP传递一列
SQL> select division_id,sum(salary) 2 from employees2 3 group by rollup(division_id) 4 order by division_id; DIV SUM(SALARY) --- ----------- BUS 1610000 OPE 1320000 SAL 4936000 SUP 1015000 8881000 SQL>
再来看一下如果使用普通的GROUP BY,而没有ROLLUP是个什么情况
SQL> select division_id,sum(salary) 2 from employees2 3 group by division_id 4 order by division_id; DIV SUM(SALARY) --- ----------- BUS 1610000 OPE 1320000 SAL 4936000 SUP 1015000
可以看到,缺少了最后的统计信息。
2、向ROLLUP传递多列
SQL> select division_id,job_id,sum(salary) 2 from employees2 3 group by rollup(division_id,job_id) 4 order by division_id,job_id; DIV JOB SUM(SALARY) --- --- ----------- BUS MGR 530000 BUS PRE 800000 BUS WOR 280000 BUS 1610000 OPE ENG 245000 OPE MGR 805000 OPE WOR 270000 OPE 1320000 SAL MGR 4446000 SAL WOR 490000 SAL 4936000 DIV JOB SUM(SALARY) --- --- ----------- SUP MGR 465000 SUP TEC 115000 SUP WOR 435000 SUP 1015000 8881000 16 rows selected.
可以看到,除了在最后有一个求和记录外,每个division_id分组也会有一个求和记录。
那么我们现在交换一下ROLLUP中数据列的顺序,看看结果怎样
SQL> select job_id,division_id,sum(salary) 2 from employees2 3 group by rollup(job_id,division_id) 4 order by job_id,division_id; JOB DIV SUM(SALARY) --- --- ----------- ENG OPE 245000 ENG 245000 MGR BUS 530000 MGR OPE 805000 MGR SAL 4446000 MGR SUP 465000 MGR 6246000 PRE BUS 800000 PRE 800000 TEC SUP 115000 TEC 115000 JOB DIV SUM(SALARY) --- --- ----------- WOR BUS 280000 WOR OPE 270000 WOR SAL 490000 WOR SUP 435000 WOR 1475000 8881000 17 rows selected.
好像和ROLLUP没什么区别哦,呵呵,继续往下看。
4、向CUBE传递多列
SQL> select job_id,division_id,sum(salary) 2 from employees2 3 group by cube(job_id,division_id) 4 order by job_id,division_id; JOB DIV SUM(SALARY) --- --- ----------- ENG OPE 245000 ENG 245000 MGR BUS 530000 MGR OPE 805000 MGR SAL 4446000 MGR SUP 465000 MGR 6246000 PRE BUS 800000 PRE 800000 TEC SUP 115000 TEC 115000 JOB DIV SUM(SALARY) --- --- ----------- WOR BUS 280000 WOR OPE 270000 WOR SAL 490000 WOR SUP 435000 WOR 1475000 BUS 1610000 OPE 1320000 SAL 4936000 SUP 1015000 8881000 21 rows selected.
可以看到工资是根据job_id和division_id求和的,CUBE在每一个job_id中都返回一条记录,表示其中的工资总数,同时在接近末尾处显示每一种division_id的工资总数,最后一条记录显示所有工资的总数。
把两列的顺序换换会怎样?呵呵,真的有兴趣那就自己动手试试吧。
==================================================================================
CREATE TABLE divisions ( division_id CHAR(3) CONSTRAINT divisions_pk PRIMARY KEY, name VARCHAR2(15) NOT NULL ); CREATE TABLE jobs ( job_id CHAR(3) CONSTRAINT jobs_pk PRIMARY KEY, name VARCHAR2(20) NOT NULL ); CREATE TABLE employees2 ( employee_id INTEGER CONSTRAINT employees2_pk PRIMARY KEY, division_id CHAR(3) CONSTRAINT employees2_fk_divisions REFERENCES divisions(division_id), job_id CHAR(3) REFERENCES jobs(job_id), first_name VARCHAR2(10) NOT NULL, last_name VARCHAR2(10) NOT NULL, salary NUMBER(6, 0) ); INSERT INTO divisions ( division_id, name ) VALUES ( 'SAL', 'Sales' ); INSERT INTO divisions ( division_id, name ) VALUES ( 'OPE', 'Operations' ); INSERT INTO divisions ( division_id, name ) VALUES ( 'SUP', 'Support' ); INSERT INTO divisions ( division_id, name ) VALUES ( 'BUS', 'Business' ); INSERT INTO jobs ( job_id, name ) VALUES ( 'WOR', 'Worker' ); INSERT INTO jobs ( job_id, name ) VALUES ( 'MGR', 'Manager' ); INSERT INTO jobs ( job_id, name ) VALUES ( 'ENG', 'Engineer' ); INSERT INTO jobs ( job_id, name ) VALUES ( 'TEC', 'Technologist' ); INSERT INTO jobs ( job_id, name ) VALUES ( 'PRE', 'President' ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 1, 'BUS', 'PRE', 'James', 'Smith', 800000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 2, 'SAL', 'MGR', 'Ron', 'Johnson', 350000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 3, 'SAL', 'WOR', 'Fred', 'Hobbs', 140000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 4, 'SUP', 'MGR', 'Susan', 'Jones', 200000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 5, 'SAL', 'WOR', 'Rob', 'Green', 350000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 6, 'SUP', 'WOR', 'Jane', 'Brown', 200000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 7, 'SUP', 'MGR', 'John', 'Grey', 265000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 8, 'SUP', 'WOR', 'Jean', 'Blue', 110000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 9, 'SUP', 'WOR', 'Henry', 'Heyson', 125000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 10, 'OPE', 'MGR', 'Kevin', 'Black', 225000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 11, 'OPE', 'MGR', 'Keith', 'Long', 165000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 12, 'OPE', 'WOR', 'Frank', 'Howard', 125000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 13, 'OPE', 'WOR', 'Doreen', 'Penn', 145000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 14, 'BUS', 'MGR', 'Mark', 'Smith', 155000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 15, 'BUS', 'MGR', 'Jill', 'Jones', 175000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 16, 'OPE', 'ENG', 'Megan', 'Craig', 245000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 17, 'SUP', 'TEC', 'Matthew', 'Brant', 115000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 18, 'OPE', 'MGR', 'Tony', 'Clerke', 200000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 19, 'BUS', 'MGR', 'Tanya', 'Conway', 200000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 20, 'OPE', 'MGR', 'Terry', 'Cliff', 215000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 21, 'SAL', 'MGR', 'Steve', 'Green', 275000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 22, 'SAL', 'MGR', 'Roy', 'Red', 375000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 23, 'SAL', 'MGR', 'Sandra', 'Smith', 335000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 24, 'SAL', 'MGR', 'Gail', 'Silver', 225000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 25, 'SAL', 'MGR', 'Gerald', 'Gold', 245000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 26, 'SAL', 'MGR', 'Eileen', 'Lane', 235000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 27, 'SAL', 'MGR', 'Doreen', 'Upton', 235000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 28, 'SAL', 'MGR', 'Jack', 'Ewing', 235000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 29, 'SAL', 'MGR', 'Paul', 'Owens', 245000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 30, 'SAL', 'MGR', 'Melanie', 'York', 255000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 31, 'SAL', 'MGR', 'Tracy', 'Yellow', 225000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 32, 'SAL', 'MGR', 'Sarah', 'White', 235000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 33, 'SAL', 'MGR', 'Terry', 'Iron', 225000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 34, 'SAL', 'MGR', 'Christine', 'Brown', 247000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 35, 'SAL', 'MGR', 'John', 'Brown', 249000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 36, 'SAL', 'MGR', 'Kelvin', 'Trenton', 255000 ); INSERT INTO employees2 ( employee_id, division_id, job_id, first_name, last_name, salary ) VALUES ( 37, 'BUS', 'WOR', 'Damon', 'Jones', 280000 );
ORACLE WITH AS 用法(转自:http://blog.csdn.net/a9529lty/article/details/4923957/)
先举个例子吧:
有两张表,分别为A、B,求得一个字段的值先在表A中寻找,如果A表中存在数据,则输出A表的值;如果A表中不存在,则在B表中寻找,若B表中有相应记录,则输出B表的值;如果B表中也不存在,则输出"no records”字符串。
with sql1 as (select to_char(a) s_name from test_tempa), sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1)) select * from sql1 union all select * from sql2 union all select 'no records' from dual where not exists (select s_name from sql1 where rownum=1) and not exists (select s_name from sql2 where rownum=1);
再举个简单的例子
with a as (select * from test) select * from a;
其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它
这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了
下面是搜索到的英文文档资料
About Oracle WITH clause Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.
The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:
• The SQL “WITH clause” only works on Oracle 9i release 2 and beyond. • Formally, the “WITH clause” is called subquery factoring • The SQL “WITH clause” is used when a subquery is executed multiple times • Also useful for recursive queries (SQL-99, but not Oracle SQL)
To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query. We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH subquery_name AS (the aggregation SQL statement) SELECT (query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH clause”:
WITH sum_sales AS select /*+ materialize */ sum(quantity) all_sales from stores number_stores AS select /*+ materialize */ count(*) nbr_stores from stores sales_by_store AS select /*+ materialize */ store_name, sum(quantity) store_sales from store natural join sales SELECT store_name FROM store, sum_sales, number_stores, sales_by_store where store_sales > (all_sales / nbr_stores) ;
Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.
It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.
To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH subquery_name AS (the aggregation SQL statement) SELECT (query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:
=================================================================================
下面自己小试一把,当然,一点都不复杂,很简单很简单的例子,呵呵。
SQL> create table t2(id int); Table created. SQL> create table t3(id int); Table created. SQL> insert into t2 values(1); 1 row created. SQL> insert into t2 values(2); 1 row created. SQL> insert into t3 values(3); 1 row created. SQL> commit; Commit complete. SQL> select * from t2; ID ---------- 1 2 SQL> select * from t3; ID ---------- 3 SQL> with 2 sql1 as (select * from t2), 3 sql2 as (select * from t3) 4 select * from t2 5 union 6 select * from t3; sql2 as (select * from t3) * ERROR at line 3: ORA-32035: unreferenced query name defined in WITH clause --从这里可以看到,你定义了sql1和sql2,就得用它们哦,不然会报错的。 SQL> with 2 sql1 as (select * from t2), 3 sql2 as (select * from t3) 4 select * from sql1 5 union 6 select * from sql2; ID ---------- 1 2 3 --下面加个WHERE条件试试 SQL> with 2 sql1 as (select * from t2), 3 sql2 as (select * from t3) 4 select * from sql1 5 union 6 select * from sql2 7 where id in(2,3); ID ---------- 1 2 3 --奇怪?为什么加了WHERE条件还是输出ID=1的记录了,继续往下看: SQL> with 2 sql1 as (select * from t2), 3 sql2 as (select * from t3) 4 select * from sql1 5 where id=3 6 union 7 select * from sql2 8 where id=3; ID ---------- 3 --可以看到,每个条件是要针对每个SELECT语句的。