Partition Window : PARTITION « Analytical Functions « Oracle PL / SQL






Partition Window

  
SQL> --Window
SQL>
SQL>
SQL> create table department(
  2          dept_no                 integer      primary key
  3         ,dept_name               varchar(20)      not null
  4         ,mgr_no                  integer
  5  );

Table created.

SQL>
SQL> insert into department(dept_no, dept_name, mgr_no)values(1, 'Design', 1);

1 row created.

SQL> insert into department(dept_no, dept_name, mgr_no)values(2, 'Sales', 1);

1 row created.

SQL> insert into department(dept_no, dept_name, mgr_no)values(3, 'Development', 1);

1 row created.

SQL>
SQL> create table employee(
  2           emp_no                 integer         primary key
  3          ,lastname               varchar2(20)    not null
  4          ,firstname              varchar2(15)    not null
  5          ,midinit                varchar2(1)
  6          ,street                 varchar2(30)
  7          ,city                   varchar2(20)
  8          ,state                  varchar2(2)
  9          ,zip                    varchar2(5)
 10          ,zip_4                  varchar2(4)
 11          ,area_code              varchar2(3)
 12          ,phone                  varchar2(8)
 13          ,salary                 number(3)
 14          ,birthdate              date
 15          ,hiredate               date
 16          ,title                  varchar2(20)
 17          ,dept_no                integer         references department
 18          ,mgr                    integer
 19          ,region                 number
 20          ,division               number
 21          ,total_sales            number
 22  );

Table created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (1,'Anderson','Nancy','N','33 Ave','London','NY','11111','1111','212','234-1111',3.75,'21-mar-1927','1-feb-1947','Sales Manager',2,null,100,10,40000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (2,'Last','First','F','12 Ave','Paris','CA','22222','2222','111','867-2222',7.75,'14-feb-1976','15-mar-1985','Sales Clerk',2,1,100,10,10000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (3,'Wash','Georgia','G','1 Street14','Barton','NJ','33333','3333','214','340-3333',11.50,'2-jul-1977','21-apr-2004','Designer',1,2,100,10,40000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (4,'Bush','Dave','D','56 Street','Island','RI','44444','4444','215','777-4444',21.65,'15-may-1945','2-aug-1975','Designer',1,2,100,10,40000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (5,'Will','Robin','W','56 Street','Island','MA','55555','5555','216','777-5555',24.65,'10-dec-1980','2-aug-2007','Designer',1,5,100,10,40000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (6,'Pete','Mona','M','13 Ave','York','MO','66666','6666','217','111-6666',9,'14-feb-1966','15-mar-1985','Sales Clerk',2,5,100,10,40000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (7,'Roke','John','J','67 Ave','New York','BC','77777','7777','218','122-7777',10.00,'14-jun-1955','15-mar-1975','Accountant',3,2,100,10,40000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2               values (8,'Horry','Tedi','T','1236 Lane','Newton','NY','88888','8888','219','222-8888',13.00,'10-jun-1955','15-aug-1985','Sales Representative',3,2,100,10,50000);

1 row created.

SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
  2                values (9,'Bar','Candi','C','400 East Street','Yorken','NY','99999','9999','220','321-9999',12.00,'10-oct-1933','15-jan-1969','Sales Representative',3,5,100,10,35000);

1 row created.

SQL>
SQL>
SQL> select lastname , dept_no, salary,
  2      sum(salary) over (partition by dept_no
  3                        order by lastname
  4                        rows 1 preceding) "EMP+PREV"
  5  from employee
  6  order by dept_no, lastname
  7  /
LASTNAME                DEPT_NO     SALARY   EMP+PREV
-------------------- ---------- ---------- ----------
Bush                          1         22         22
Wash                          1         12         34
Will                          1         25         37
Anderson                      2          4          4
Last                          2          8         12
Pete                          2          9         17
Bar                           3         12         12
Horry                         3         13         25
Roke                          3         10         23

9 rows selected.

SQL>
SQL> drop table employee;

Table dropped.

SQL> drop table department;

Table dropped.

SQL>
SQL> --

   
  








Related examples in the same category

1.partition clause
2.Partitioning with PARTITION_BY
3.Use partitioning in the OVER clause of the aggregate-analytical function like this
4.PARTITION BY: divide the groups into subgroups
5.Count(*) over partition
6.Dense_rank over partition by
7.rank and dense_rank over partition
8.count(*) over partition by, order by and range unbounded preceding
9.dense_rank() over partition by, order by
10.Top with partition
11.PARTITION BY (JOB title) and right outer join
12.SPREADSHEET PARTITION BY
13.sum salary over PARTITION BY