sql入門之23 pivoting insert等
-- 大綱:
Use the following types of multitable inserts
?Unconditional INSERT
?Pivoting INSERT
?Conditional ALL INSERT
?Conditional FIRST INSERT
:) Create and use external tables
:) Name the index at the time of creating a primary key constraint[@more@]
unconditional insert: all into_clause
The oracle server executes each insert_into_clause once for each row returned by the subquery.
在使用all關鍵字時,oracle將執行每個insert,不進行排除
SQL> insert all
2 into sal_history values(empid,hiredate,sal)
3 into mgr_history values(empid,mgr,sal)
4 select employee_id empid,hire_date hiredate,salary sal,manager_id mgr
5 from employees
6 where employee_id>200;
8 rows created.
SQL> select employee_id from sal_history
2 union all
3 select employee_id from mgr_history
EMPLOYEE_ID
-----------
201
202
205
206
201
202
205
206
-- 有條件的insert all
insert all
when sal>10000 then
into sal_history values (empid,hiredate,sal)
when mgr>200 then
into mgr_history values (empid,mgr,sal)
select employee_id empid,hire_date hiredate,salary sal,manager_id mgr
from employees
where employee_id>200;
-- insert first是符合第一個的記錄不再應用到下面的條件判斷
insert first
when sal >25000 then
into special_sal values(deptid,sal)
when hiredate like ('%00%') then
into hiredate_history_00 values (deptid,hiredate)
when hiredate like ('%99%') then
into hiredate_history_99 values(deptid,hiredate)
else
into hiredate_history values(deptid,hiredate)
select department_id deptid,sum(salary) sal,
max(hire_date) hiredate
from employees
group by department_id;
-- 什麼是pivoting insert
create table sales_source_data (
employee_id number(6),
week_id number(2),
sales_mon number(8,2),
sales_tue number(8,2),
sales_wed number(8,2),
sales_thur number(8,2),
sales_fri number(8,2)
);
insert into sales_source_data values (176,6,2000,3000,4000,5000,6000);
create table sales_info (
employee_id number(6),
week number(2),
sales number(8,2)
);
-- 現在要將上表的資料轉換到下表中,
insert all
into sales_info values(employee_id,week_id,sales_mon)
into sales_info values(employee_id,week_id,sales_tue)
into sales_info values(employee_id,week_id,sales_wed)
into sales_info values(employee_id,week_id,sales_thur)
into sales_info values(employee_id,week_id,sales_fri)
select employee_id,week_id,sales_mon,sales_tue,
sales_wed,sales_thur,sales_fri
from sales_source_data;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271063/viewspace-909918/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL入門之6 sql*plusSQL
- SQL入門之11 DatabaseTransactionsSQLDatabase
- SQL入門之7 鎖SQL
- 深入SQL之 insert allSQL
- SQL入門之10 MERGESQL
- SQL入門之9使用defaultSQL
- 10.spark sql之快速入門SparkSQL
- SQL入門之3 函式2SQL函式
- SQL入門之2 函式1SQL函式
- 資料分析師之SQL入門SQL
- SQL入門之12 Read ConsistencySQL
- SQL入門之1 select 聯接SQL
- SQL 入門SQL
- SQL Server Bulk Insert批量資料匯入SQLServer
- MongoDB入門系列(二):Insert、Update、Delete、DropMongoDBdelete
- SQL入門之4 group by 與子查詢SQL
- PL/SQL入門SQL
- sql*plus入門SQL
- SQL__INSERTSQL
- SQL入門之5 表的建立與修改1SQL
- LINQ to SQL語句之Insert/Update/Delete操作SQLdelete
- React從入門到精通系列之(23)ReactDOM的用法React
- 【SQL】9 SQL INSERT INTO 語句SQL
- ACM入門之新手入門ACM
- SQL入門基礎SQL
- SQL基礎入門SQL
- SQL入門之8 限制插入資料的範圍SQL
- 好程式設計師Java分享MySQL之SQL入門(一)程式設計師JavaMySql
- Mysql系列一:SQL入門MySql
- SQL入門-進階教程SQL
- 1、MySQL和SQL入門MySql
- 《Flutter 入門經典》之“Flutter 入門 ”Flutter
- Spark SQL 程式設計API入門系列之Spark SQL的作用與使用方式SparkSQL程式設計API
- SQL-小白最佳入門sql查詢一SQL
- SQL 注入攻防入門詳解SQL
- SQL語言快速入門(轉)SQL
- Sql Server系列:Insert語句SQLServer
- SQL INSERT批次插入方式SQL