FIRST and LAST functions to get the first and last values in an ordered group : First Last « Analytical Functions « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » Analytical Functions » First Last 
FIRST and LAST functions to get the first and last values in an ordered group


SQL> CREATE TABLE all_sales (
  2    year INTEGER,
  3    month INTEGER,
  4    prd_type_id INTEGER,
  5    emp_id INTEGER ,
  6    amount NUMBER(82)
  7  );

Table created.

SQL>
SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,1    ,1          ,21    ,16034.84);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,2    ,1          ,21    ,15644.65);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,3    ,2          ,21    ,20167.83);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,4    ,2          ,21    ,25056.45);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,5    ,2          ,21    ,NULL);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,6    ,1          ,21    ,15564.66);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,7    ,1          ,21    ,15644.65);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,8    ,1          ,21    ,16434.82);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,9    ,1          ,21    ,19654.57);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,10   ,1          ,21    ,21764.19);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,11   ,1          ,21    ,13026.73);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2006,12   ,2          ,21    ,10034.64);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,1    ,2          ,22    ,16634.84);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,1    ,2          ,21    ,26034.84);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,2    ,1          ,21    ,12644.65);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,3    ,1          ,21    ,NULL);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,4    ,1          ,21    ,25026.45);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,5    ,1          ,21    ,17212.66);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,6    ,1          ,21    ,15564.26);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,7    ,2          ,21    ,62654.82);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,8    ,2          ,21    ,26434.82);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,9    ,2          ,21    ,15644.65);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,10   ,2          ,21    ,21264.19);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,11   ,1          ,21    ,13026.73);

row created.

SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
  2                  values(2005,12   ,1          ,21    ,10032.64);

row created.

SQL>
SQL> select from all_sales;

      YEAR      MONTH PRD_TYPE_ID     EMP_ID     AMOUNT
---------- ---------- ----------- ---------- ----------
      2006          1           1         21   16034.84
      2006          2           1         21   15644.65
      2006          3           2         21   20167.83
      2006          4           2         21   25056.45
      2006          5           2         21
      2006          6           1         21   15564.66
      2006          7           1         21   15644.65
      2006          8           1         21   16434.82
      2006          9           1         21   19654.57
      2006         10           1         21   21764.19
      2006         11           1         21   13026.73
      2006         12           2         21   10034.64
      2005          1           2         22   16634.84
      2005          1           2         21   26034.84
      2005          2           1         21   12644.65
      2005          3           1         21
      2005          4           1         21   25026.45
      2005          5           1         21   17212.66
      2005          6           1         21   15564.26
      2005          7           2         21   62654.82
      2005          8           2         21   26434.82
      2005          9           2         21   15644.65
      2005         10           2         21   21264.19
      2005         11           1         21   13026.73
      2005         12           1         21   10032.64

25 rows selected.

SQL>
SQL> --FIRST and LAST functions to get the first and last values in an ordered group.
SQL>
SQL> --You can use FIRST and LAST with the following functions: MIN(), MAX(), COUNT(), SUM(), AVG(), STDDEV(), and VARIANCE().
SQL>
SQL> SELECT
  2   MIN(monthKEEP (DENSE_RANK FIRST ORDER BY SUM(amount))
  3    AS highest_sales_month,
  4   MIN(monthKEEP (DENSE_RANK LAST ORDER BY SUM(amount))
  5    AS lowest_sales_month
  6  FROM all_sales
  7  GROUP BY month
  8  ORDER BY month;

HIGHEST_SALES_MONTH LOWEST_SALES_MONTH
------------------- ------------------
                  5                  7

SQL>
SQL>
SQL>
SQL> drop table all_sales;

Table dropped.

SQL>
SQL>

           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.