max and decode function : MAX « Aggregate Functions « Oracle PL/SQL Tutorial

Oracle PL/SQL Tutorial
1. Introduction
2. Query Select
3. Set
4. Insert Update Delete
5. Sequences
6. Table
7. Table Joins
8. View
9. Index
10. SQL Data Types
11. Character String Functions
12. Aggregate Functions
13. Date Timestamp Functions
14. Numerical Math Functions
15. Conversion Functions
16. Analytical Functions
17. Miscellaneous Functions
18. Regular Expressions Functions
19. Statistical Functions
20. Linear Regression Functions
21. PL SQL Data Types
22. PL SQL Statements
23. PL SQL Operators
24. PL SQL Programming
25. Cursor
26. Collections
27. Function Procedure Packages
28. Trigger
29. SQL PLUS Session Environment
30. System Tables Data Dictionary
31. System Packages
32. Object Oriented
33. XML
34. Large Objects
35. Transaction
36. User Privilege
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
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 Tutorial » Aggregate Functions » MAX 
12. 4. 4. max and decode function
SQL>
SQL> Create table objects oid int primary key, name varchar2(255) );

Table created.

SQL>
SQL> Create table attributes(
  2      attrId int primary key, attrName varchar2(255),
  3      datatype varchar2(25)
  4  );

Table created.

SQL>
SQL> Create table object_Attributes(
  2      oid int, attrId int, value varchar2(4000),
  3      primary key(oid,attrId)
  4  );

Table created.

SQL>
SQL> Create table Links (
  2      oid1 int, oid2 int,
  3      primary key (oid1, oid2)
  4  );

Table created.

SQL>
SQL> insert into attributes values 1'DATE_OF_BIRTH', 'DATE' );

row created.

SQL> insert into attributes values 2'FIRST_NAME',    'STRING' );

row created.

SQL> insert into attributes values 3'LAST_NAME',    'STRING' );

row created.

SQL>
SQL> insert into objects values 1'PERSON' );

row created.

SQL> insert into object_Attributes values11'15-mar-1965' );

row created.

SQL> insert into object_Attributes values12'Thomas' );

row created.

SQL> insert into object_Attributes values13'Kyte' );

row created.

SQL>
SQL> insert into objects values 2'PERSON' );

row created.

SQL> insert into object_Attributes values21'21-oct-1968' );

row created.

SQL> insert into object_Attributes values22'John' );

row created.

SQL> insert into object_Attributes values23'Smith' );

row created.

SQL>
SQL> select from (
  2  select
  3  max(decode(attrName, 'FIRST_NAME', value, null)) first_name,
  4  max(decode(attrName, 'LAST_NAME',  value, null)) last_name,
  5  max(decode(attrName, 'DATE_OF_BIRTH',  value, null)) date_of_birth
  6    from objects, object_attributes, attributes
  7  where attributes.attrName in 'FIRST_NAME', 'LAST_NAME', 'DATE_OF_BIRTH' )
  8     and object_attributes.attrId = attributes.attrId
  9     and object_attributes.oid = objects.oid
 10     and objects.name = 'PERSON'
 11  group by objects.oid
 12         )
 13  where last_name = 'Smith' or date_of_birth like '%-mar-%'
 14  /

FIRST_NAME
--------------------------------------------------------------------------------
LAST_NAME
--------------------------------------------------------------------------------
DATE_OF_BIRTH
--------------------------------------------------------------------------------
Thomas
Kyte
15-mar-1965

John
Smith
21-oct-1968

FIRST_NAME
--------------------------------------------------------------------------------
LAST_NAME
--------------------------------------------------------------------------------
DATE_OF_BIRTH
--------------------------------------------------------------------------------


SQL>
SQL> drop table objects;

Table dropped.

SQL> drop table attributes;

Table dropped.

SQL> drop table object_attributes;

Table dropped.

SQL> drop table links;

Table dropped.

SQL>
12. 4. MAX
12. 4. 1. MAX(x) gets the maximum values for x.
12. 4. 2. Use MAX() with strings
12. 4. 3. Use MAX() with dates
12. 4. 4. max and decode function
12. 4. 5. max(total_price) - min(total_price)
12. 4. 6. Greater than max(salary)
12. 4. 7. Who have the max value
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.