Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Analytical Functions
For DWH
Emrah METE
i2i-Systems
Expert Analyst&Developer
TROUG – BI/DW SIG Day 2014
03.04.2014
İTÜ
http://emrahmete.wordpress.com/about/
Marmara Üniversitesi - Bilgisayar Teknolojisi ve
Programlama
Yıldız Teknik Üniversit...
Agenda
• Analytical Functions..Why?
• Aggregate and Analytic
• First/Last Value
• LAG/LEAD
• Pivoting Operations
• Ranking...
Analytical Functions.. Why?
Aggregate and Analytic
Aggregation (Step 1 - Traditional)
SQL> select department_id,
sum(salary)
from employees
group by department_id;
Aggregation (Step 2 - Analytic)
SQL> select first_name, salary,
sum(salary) over
(order by first_name) as empsal
from empl...
Aggregation (Step 2 - Analytic)
SQL> select first_name, department_id,
sum(salary) over
(partition by department_id order ...
Aggregation (Step 3 - Windowing)
SQL> select employee_id, first_name, salary,
sum(salary) over ( order by employee_id rows...
Aggregation (Step 3 - Windowing)
SQL> select employee_id, first_name, salary,
sum(salary) over ( order by employee_id rows...
Aggregation (Step 3 - Windowing)
First_Value
SQL> select first_name, department_id,
first_value(salary) over
(partition by department_id order by employee_...
Last_Value
SQL> select first_name, department_id, salary,
last_value(salary) over
(partition by department_id order by dep...
LAG & LEAD
SQL> select employee_id, first_name, salary,
LAG (salary, 1) OVER (ORDER BY salary )
AS LAG1
FROM employees;
LAG & LEAD
SQL> select employee_id, first_name, salary,
LEAD (salary, 1) OVER (ORDER BY salary ) AS
LAG1
FROM employees;
LISTAGG
SQL> select department_id, listagg(first_name,’,
’) within group (order by department_id)
as newList
FROM employee...
PIVOT & UNPIVOT
PIVOT & UNPIVOT
Ranking – (Rank)
SQL> SELECT department_id, last_name, salary,
RANK() OVER (PARTITION BY department_id
ORDER BY salary DES...
Ranking – (Dense Rank)
SQL> SELECT department_id, last_name, salary,
RANK () OVER (PARTITION BY department_id
ORDER BY sal...
Ranking – (Percent Rank)
SQL> SELECT department_id, last_name, salary,
PERCENT_RANK () OVER (PARTITION BY
department_id OR...
- Financial applications seeking
patterns of pricing, trading
volume, and other behavior
- Security applications where
unu...
Pattern Matching
Pattern:
Start
Bottom
End
Pattern Matching
Pattern Matching
Pattern:
Start End
References
http://connormcdonald.wordpress.com/
http://allthingsoracle.com/experts/alex-nuijten/
Dan Stober [ORACLE ANALYT...
Questions/Answers
Analytical Functions for DWH
Analytical Functions for DWH
Upcoming SlideShare
Loading in …5
×

Analytical Functions for DWH

1,396 views

Published on

Analytical Functions for DWH

Published in: Data & Analytics
  • Be the first to comment

Analytical Functions for DWH

  1. 1. Analytical Functions For DWH Emrah METE i2i-Systems Expert Analyst&Developer TROUG – BI/DW SIG Day 2014 03.04.2014 İTÜ
  2. 2. http://emrahmete.wordpress.com/about/ Marmara Üniversitesi - Bilgisayar Teknolojisi ve Programlama Yıldız Teknik Üniversitesi - Bilgisayar Mühendisliği i2i-Systems – Analyst&Developer emrahmete.wordpress.com TROUG - Yönetim Kurulu Üyeliği Emrah METE Yahoo OracleTurk Grubu Moderatör Oracle Certified Expert
  3. 3. Agenda • Analytical Functions..Why? • Aggregate and Analytic • First/Last Value • LAG/LEAD • Pivoting Operations • Ranking • Oracle Pattern Matching • Questions and Answers
  4. 4. Analytical Functions.. Why?
  5. 5. Aggregate and Analytic
  6. 6. Aggregation (Step 1 - Traditional) SQL> select department_id, sum(salary) from employees group by department_id;
  7. 7. Aggregation (Step 2 - Analytic) SQL> select first_name, salary, sum(salary) over (order by first_name) as empsal from employees;
  8. 8. Aggregation (Step 2 - Analytic) SQL> select first_name, department_id, sum(salary) over (partition by department_id order by salary) as deptsal from employees order by department_id;
  9. 9. Aggregation (Step 3 - Windowing) SQL> select employee_id, first_name, salary, sum(salary) over ( order by employee_id rows between unbounded preceding and current row) as cumul from employees order by employee_id;
  10. 10. Aggregation (Step 3 - Windowing) SQL> select employee_id, first_name, salary, sum(salary) over ( order by employee_id rows between 1preceding and 1 following) as cumul from employees order by employee_id;
  11. 11. Aggregation (Step 3 - Windowing)
  12. 12. First_Value SQL> select first_name, department_id, first_value(salary) over (partition by department_id order by employee_id) as deptsal from employees order by department_id;
  13. 13. Last_Value SQL> select first_name, department_id, salary, last_value(salary) over (partition by department_id order by department_id) as deptsal from employees order by department_id;
  14. 14. LAG & LEAD SQL> select employee_id, first_name, salary, LAG (salary, 1) OVER (ORDER BY salary ) AS LAG1 FROM employees;
  15. 15. LAG & LEAD SQL> select employee_id, first_name, salary, LEAD (salary, 1) OVER (ORDER BY salary ) AS LAG1 FROM employees;
  16. 16. LISTAGG SQL> select department_id, listagg(first_name,’, ’) within group (order by department_id) as newList FROM employees group by department_id;
  17. 17. PIVOT & UNPIVOT
  18. 18. PIVOT & UNPIVOT
  19. 19. Ranking – (Rank) SQL> SELECT department_id, last_name, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) "Rank" FROM employees WHERE department_id = 60 ORDER BY department_id, "Rank", salary;
  20. 20. Ranking – (Dense Rank) SQL> SELECT department_id, last_name, salary, RANK () OVER (PARTITION BY department_id ORDER BY salary DESC) "Rank", DENSE_RANK () OVER (PARTITION BY department_id ORDER BY salary DESC) "Drank" FROM employees WHERE department_id = 60 ORDER BY "Rank";
  21. 21. Ranking – (Percent Rank) SQL> SELECT department_id, last_name, salary, PERCENT_RANK () OVER (PARTITION BY department_id ORDER BY salary) AS pr FROM employees WHERE department_id = 60 ORDER BY department_id, pr; (rank of row in its partition - 1) / (number of rows in the partition - 1) Example: Last_Name: Ernst (4 - 1) / (5 - 1) = 0,75
  22. 22. - Financial applications seeking patterns of pricing, trading volume, and other behavior - Security applications where unusual behavior must be detected. - Fraud Detection - Sensor Data analysis.(CEP App.) Pattern Matching
  23. 23. Pattern Matching Pattern: Start Bottom End
  24. 24. Pattern Matching
  25. 25. Pattern Matching Pattern: Start End
  26. 26. References http://connormcdonald.wordpress.com/ http://allthingsoracle.com/experts/alex-nuijten/ Dan Stober [ORACLE ANALYTIC FUNCTIONS WINDOWING CLAUSE] http://docs.oracle.com/cd/E16655_01/server.121/e17749/toc.htm http://www.oracle.com/technetwork/database/bi-datawarehousing/sql-analytics-index- 1984365.html http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions056.htm http://docs.oracle.com/cd/E16655_01/server.121/e17749/analysis.htm#DWHSG0202 http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions101.htm http://www.oracle.com/technetwork/database/bi-datawarehousing/sql-analytics-index- 1984365.html
  27. 27. Questions/Answers

×