ROW_NUMBER():
3rd Highest salary using rownum:
1) select empname, empno, sal from
(select empname, empno, sal, row_num() over (order by sal desc) rn from emp) a
where a.rn =3;
2)
Select * from(select row_number() over(partition by item order by item) as row_number, * from @t) Twhere row_number<=N
3)
Select item,price from(select row_number() over(order by item) as row_number, * from @t) Twhere row_number between 1 and 5
Examples:
CREATE TABLE vote_count (
submit_date DATE NOT NULL,
num_votes NUMBER NOT NULL);
INSERT INTO vote_count VALUES (TRUNC(SYSDATE)-4, 100);
INSERT INTO vote_count VALUES (TRUNC(SYSDATE)-3, 150);
INSERT INTO vote_count VALUES (TRUNC(SYSDATE)-2, 75);
INSERT INTO vote_count VALUES (TRUNC(SYSDATE)-3, 25);
INSERT INTO vote_count VALUES (TRUNC(SYSDATE)-1, 50);
COMMIT;
SELECT * FROM vote_count;
AVG: Returns a running average.
SELECT submit_date, num_votes, TRUNC(AVG(num_votes)OVER(ORDER BY submit_date ROWS UNBOUNDED PRECEDING)) AVG_VOTE_PER_DAY
FROM vote_count
ORDER BY submit_date;
SELECT submit_date, num_votes, TRUNC(AVG(num_votes)OVER(PARTITION BY submit_date ORDER BY submit_date ROWS UNBOUNDED PRECEDING)) AVG_VOTE_PER_DAY
FROM vote_count
ORDER BY submit_date;
CORR:Returns the coefficient of correlation of a set of number pairs.
SELECT t.calendar_month_number,
CORR (SUM(s.amount_sold), SUM(s.quantity_sold))
OVER (ORDER BY t.calendar_month_number) AS CUM_CORR
FROM sales s, times t
WHERE s.time_id = t.time_id AND calendar_year = 1998
GROUP BY t.calendar_month_number;
COUNT: Returns a running count of all records or by partition.
SELECT submit_date, num_votes, TRUNC(COUNT(num_votes)
OVER(ORDER BY submit_date ROWS UNBOUNDED PRECEDING)) AS DAY_COUNT
FROM vote_count
ORDER BY submit_date;
SELECT submit_date, COUNT(*)
OVER(PARTITION BY submit_date ORDER BY submit_date
ROWS UNBOUNDED PRECEDING) NUM_RECS
FROM vote_count;
COVAR_POP: Returns the population covariance of a set of number pairs.
SELECT job_id,
COVAR_POP(SYSDATE-hire_date, salary) AS covar_pop,
COVAR_SAMP(SYSDATE-hire_date, salary) AS covar_samp
FROM employees
WHERE department_id in (50, 80)
GROUP BY job_id;
Objective of this blog is to share my oracle knowledge with the people who are working in oracle.
Labels
Accrual
(1)
Advanced Pricing
(1)
Alert in OAF
(1)
Alter Session in PLSQL
(3)
AME
(1)
AP
(8)
APEX
(6)
API
(2)
AR
(5)
AR Receipts
(1)
ASCP
(1)
Auto Invoice
(1)
BOM
(6)
Bulk Operations
(1)
Conversion Util PKG
(1)
Cost Management
(4)
Costing
(4)
CST
(1)
Customer
(1)
DBA
(1)
DFF
(1)
Discoverer
(6)
FND
(3)
FNDLOADER
(1)
Forms
(3)
General issues
(1)
GL
(6)
HCM
(1)
Hints
(1)
HRMS
(10)
Indexes
(1)
Install Base Conversion
(1)
Interview Questions
(2)
Inventory
(3)
Inverntory
(11)
iProcurement
(4)
Javascript
(1)
MOAC
(1)
OAF
(9)
OBIEE
(1)
Oracle Alerts
(2)
Oracle DB
(4)
Oracle DBA
(1)
Oracle EBS
(1)
Oracle Installation
(1)
Oracle Reports
(4)
Order Management
(7)
Payables
(14)
Payments
(2)
PLSQL
(45)
PLSQL Tunning
(6)
PO
(11)
Queries
(1)
Receivables
(7)
Service Contracts
(1)
Service Requests
(2)
SESSION
(1)
Shell Scripts in Oracle apps
(1)
SLA
(2)
SQL
(31)
SQL LOADER
(4)
SQl TUNNING
(4)
Supplier
(1)
System Administrator
(46)
TCA
(5)
Unix
(4)
UTL_FILE
(1)
WIP
(4)
WORKFLOW
(4)
XLA
(3)
XML/BI Publisher
(11)
No comments:
Post a Comment