skip to main | skip to sidebar

SQL Tutorial with Examples

Pages

  • Home
 
  • RSS
  • Facebook
  • Twitter
Monday, October 22, 2012

Advance SQL

Posted by Raju Gupta at 11:33 AM – 0 comments
 

The query that uses partition OVER PARTITION BY analytic function

The below example shows the cumulative salary within a departement row by row, with each row including a summation of the prior rows salary.

SELECT ename "Ename", deptno "Deptno", sal "Sal",
  SUM(sal)
    OVER (ORDER BY deptno, ename) "Running Total",
  SUM(SAL)
    OVER (PARTITION BY deptno
          ORDER BY ename) "Dept Total",
  ROW_NUMBER()
    OVER (PARTITION BY deptno
          ORDER BY ENAME) "Seq"
FROM emp
ORDER BY deptno, ename


Result:
-------

Ename Deptno Sal Running Total Dept Total Seq
------ ------ ------ ------------- ---------- ----
CLARK 10 2450 2450 2450 1
KING 5000 7450 7450 2
MILLER 1300 8750 8750 3

ADAMS 20 1100 9850 1100 1
FORD 3000 12850 4100 2
JONES 2975 15825 7075 3
SCOTT 3000 18825 10075 4
SMITH 800 19625 10875 5

ALLEN 30 1600 21225 1600 1
BLAKE 2850 24075 4450 2
JAMES 950 25025 5400 3
MARTIN 1250 26275 6650 4
TURNER 1500 27775 8150 5
WARD 1250 29025 9400 6

Execution Plan
---------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 WINDOW (SORT)
2 1 TABLE ACCESS (FULL) OF 'EMP'
Statistics
---------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
1658 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed

The example shows how to calculate a "Running Total" for the entire query. This is done using the entire ordered result set, via SUM(sal) OVER (ORDER BY deptno, ename).

Further, we were able to compute a running total within each department, a total that would be reset at the beginning of the next department. The PARTITION BY deptno in that SUM(sal) caused this to happen, a partitioning clause was specified in the query in order to break the data up into groups.

The ROW_NUMBER() function is used to sequentially number the rows returned in each group, according to our ordering criteria (a "Seq" column was added to in order to display this position).

The execution plan shows, that the whole query is very well performed with only 3 consistent gets, this can never be accomplished with standard SQL or even PL/SQL.

Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • Comparing data (rows)in two tables in PL/SQL
    Pl/sql code have Cursors that will fetch table and columns that you want to compare and it will Iterate through all tables in the local d...
  • Oracle Sql Queries to view dependencies and partitioning.
    This is the list of queries to find out the dependencies between databse objects and to find out the partitioning details of a table. 1...
  • Oracle PL/SQL Query for Pagination
    Oracle PL/SQL function makes the input query statement such a way that the query out put returns for a start row index to end row index. ...
  • Serial Number Related Query
    This query will let us know the corresponding Sales Order number associated with a given serial number installed in Installed Base. -...
  • Oracle and SQL Server Database Search String procedure
    This Procedure/Query is used to search list of strings available in Oracle/SQL server database. The strings to be searched should be passed...
  • Multi User session Kill in Oracle
    To kill the inactive users login in Oracle from any schema in a database.If any unwanted transactions were kept withput roll back it will...
  • SQL tips
    Re-usable SQL Queries useful in day-to-day scenario. COMMON QUERIES *************** SELECT instance_name FROM v$instance; ...
  • Query to search Special character in word
    The query looks for any special characters in column field (specially useful for password field) and reports it.   SELECT DISTINCT emp...
  • Advance SQL
    The query that uses partition OVER PARTITION BY analytic function The below example shows the cumulative salary within a departement ...
  • TABLE FINDER
    Table_Finder is a procedure to find all the tables that are queried in a particular package or procedure. It takes Package name or procedur...
Powered by Blogger.

Archives

  • ▼  2012 (11)
    • ▼  October (11)
      • Comparing data (rows)in two tables in PL/SQL
      • Serial Number Related Query
      • SQL tips
      • Advance SQL
      • Query to search Special character in word
      • Multi User session Kill in Oracle
      • TABLE FINDER
      • Oracle PL/SQL Query for Pagination
      • Oracle and SQL Server Database Search String proce...
      • Deletion Records of Duplicate
      • Oracle Sql Queries to view dependencies and partit...
 

Labels

  • Delete Query (1)
  • Oracle SQL Query (4)
  • PL/SQL Example (2)
  • Stored Procedure Example (3)
  • String Search Query (1)
  • System Query (1)

Followers

 
 
© 2011 SQL Tutorial with Examples | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger