-- 1)
describe EMP_CUSTOM_TABLE;
-- -------------------------------------------------------------------------------------------------------------
-- 2)
SELECT * FROM EMP_CUSTOM_TABLE ;
select count(*) as Numer_of_Records FROM EMP_CUSTOM_TABLE ;
-- -------------------------------------------------------------------------------------------------------------
-- 3)
SELECT EMPNO,ENAME, SAL as Salary, DEPTNO,
count(*) OVER () Numer_of_Records,
SUM(SAL) OVER () TOTALSAL,
SUM(SAL) OVER (PARTITION BY DEPTNO) DEPTTOTALSAL,
SUM(SAL) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC ,ENAME
DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) CUM_SAL_DEPT,
SUM(SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMSAL_TABLE
FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
-- 4)
SELECT EMPNO,ENAME, SAL, DEPTNO,
LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) PREVIOUS_SAL,
LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) NEXT_SAL,
SAL + LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) CURRENT_PLUS_NEXT_SAL,
SAL + LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) CURRENT_PLUS_PREV_SAL ,
LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) +
LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) NEXT_PLUS_PREV_SAL
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
--5)
SELECT ENAME, SAL, CUMLATIVE_SAL,
COALESCE ((CUMLATIVE_SAL - LAG (CUMLATIVE_SAL, 1) OVER (ORDER BY CUMLATIVE_SAL)), CUMLATIVE_SAL) AS GETTING_ACTUAL
FROM
(SELECT ENAME, SAL, SUM (SAL) OVER (ORDER BY SAL ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL ASC );
-- -------------------------------------------------------------------------------------------------------------
-- 6)
SELECT EMPNO,ENAME, DEPTNO,SAL, CUMLATIVE_SAL,CUMLATIVE_SAL_DEPT,
COALESCE ((CUMLATIVE_SAL - LAG (CUMLATIVE_SAL, 1) OVER (ORDER BY CUMLATIVE_SAL)), CUMLATIVE_SAL) AS GETTING_ACTUAL_TABLE,
COALESCE ((CUMLATIVE_SAL_DEPT - LAG (CUMLATIVE_SAL_DEPT, 1) OVER (PARTITION BY DEPTNO ORDER BY CUMLATIVE_SAL_DEPT)), CUMLATIVE_SAL_DEPT) AS GETTING_ACTUAL_DEPTNO
FROM
(SELECT EMPNO,ENAME, DEPTNO,SAL,
SUM (SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL,
SUM (SAL) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL_DEPT
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
) ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
;
-- -------------------------------------------------------------------------------------------------------------
-- 7)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL, CUMLATIVE_SAL,CUMLATIVE_SAL_DEPT,
COALESCE ((CUMLATIVE_SAL - LAG (CUMLATIVE_SAL, 1) OVER (ORDER BY CUMLATIVE_SAL)), CUMLATIVE_SAL) AS GETTING_ACTUAL_TABLE,
COALESCE ((CUMLATIVE_SAL_DEPT - LAG (CUMLATIVE_SAL_DEPT, 1) OVER (PARTITION BY DEPTNO ORDER BY CUMLATIVE_SAL_DEPT)), CUMLATIVE_SAL_DEPT) AS GETTING_ACTUAL_DEPTNO
FROM --- CREATING CUMULATIVE SALARIES AT TSBLE, DEPT LEVEL
(SELECT EMPNO,ENAME, DEPTNO,SAL,
SUM (SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL,
SUM (SAL) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL_DEPT
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
) ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
;
-- -------------------------------------------------------------------------------------------------------------
-- 8)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
LAG(SAL, 1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) PREV_SAL_DEPT_LAG,
LEAD(SAL, 1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) NEXT_SAL_DEPT_LEAD,
MIN(SAL) OVER ( PARTITION BY DEPTNO ) MIN_SAL_DEPT,
MAX(SAL) OVER ( PARTITION BY DEPTNO ) MAX_SAL_DEPT,
ROUND(AVG(SAL) OVER ( PARTITION BY DEPTNO ),3) AS AVG_SAL_DEPT
FROM ( SELECT * FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC)
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
;
-- -------------------------------------------------------------------------------------------------------------
-- 9)
SELECT DEPTNO,
MIN(SAL) KEEP (DENSE_RANK FIRST ORDER BY SAL) DEPT_MIN_SAL,
MAX(SAL) KEEP (DENSE_RANK LAST ORDER BY SAL) DEPT_MAX_SAL
, MAX(SAL) MAXSAL
, MIN(SAL) MINSAL
FROM EMP_CUSTOM_TABLE GROUP BY DEPTNO ORDER BY DEPTNO
;
-- -------------------------------------------------------------------------------------------------------------
-- 10)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) RANKS,
RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DEPT_RANKS,
DENSE_RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC) DENSE_RANKS,
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DENSE_RANKS_DEPT
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
;
-- -------------------------------------------------------------------------------------------------------------
-- 11)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
LAG(SAL,1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) PREV_SAL_DEPT_LAG,
LEAD(SAL,1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) NEXT_SAL_DEPT_LAG
FROM ( SELECT * FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC)
;
-- -------------------------------------------------------------------------------------------------------------
-- 12)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
SUM(SAL) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMULATIVE_DEPT,
SUM(SAL) OVER ( ORDER BY DEPTNO ASC, SAL DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMULATIVE_TABLE
FROM (SELECT * FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC)
;
-- -------------------------------------------------------------------------------------------------------------
-- 13)
SELECT * FROM (
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DENSE_RANKS_DEPT
FROM ( SELECT * FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC))
WHERE DENSE_RANKS_DEPT<=2
;
-- -------------------------------------------------------------------------------------------------------------
-- 14)
SELECT * FROM (
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL ASC) DENSE_RANKS_DEPT
FROM (SELECT * FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC))
WHERE DENSE_RANKS_DEPT<=2
;
-- -------------------------------------------------------------------------------------------------------------
-- 15)
SELECT ENAME,DEPTNO, SAL,
DENSE_RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC ) DENSE_RANKS,
RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC ) RANKS
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
--16)
SELECT ENAME,DEPTNO, SAL,
DENSE_RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC ) DENSE_RANKS,
RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC ) RANKS
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
-- 17)
SELECT EMPNO,ENAME, SAL, DEPTNO,
SUM(SAL) OVER () TOTALSAL,
SUM(SAL) OVER (PARTITION BY DEPTNO) DEPTTOTALSAL,
SUM(SAL) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC ,ENAME DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) CUM_SAL_DEPT,
SUM(SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMSAL_TABLE
FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC;
-- -------------------------------------------------------------------------------------------------------------
-- 18)
SELECT EMPNO,ENAME, SAL, DEPTNO,
LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) PREVIOUS_SAL,
LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) NEXT_SAL,
SAL + LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) CURRENT_PLUS_NEXT_SAL,
SAL + LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) CURRENT_PLUS_PREV_SAL ,
LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) +
LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) NEXT_PLUS_PREV_SAL
FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC ;
---------------------------------------------------------------------------------------------------------------
--19)
SELECT ENAME, SAL, CUMLATIVE_SAL,
COALESCE ((CUMLATIVE_SAL - LAG (CUMLATIVE_SAL, 1) OVER (ORDER BY CUMLATIVE_SAL)), CUMLATIVE_SAL) AS GETTING_ACTUAL
FROM
(SELECT ENAME, SAL, SUM (SAL) OVER (ORDER BY SAL ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL ASC );
---------------------------------------------------------------------------------------------------------------
--20)
SELECT EMPNO,ENAME, DEPTNO,SAL, CUMLATIVE_SAL,CUMLATIVE_SAL_DEPT,
COALESCE ((CUMLATIVE_SAL - LAG (CUMLATIVE_SAL, 1) OVER (ORDER BY CUMLATIVE_SAL)), CUMLATIVE_SAL) AS GETTING_ACTUAL_TABLE,
COALESCE ((CUMLATIVE_SAL_DEPT - LAG (CUMLATIVE_SAL_DEPT, 1) OVER (PARTITION BY DEPTNO ORDER BY CUMLATIVE_SAL_DEPT)), CUMLATIVE_SAL_DEPT) AS GETTING_ACTUAL_DEPTNO
FROM
(SELECT EMPNO,ENAME, DEPTNO,SAL,
SUM (SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL,
SUM (SAL) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL_DEPT
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
) ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ;
-- -------------------------------------------------------------------------------------------------------------
-- 21)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL, CUMLATIVE_SAL,CUMLATIVE_SAL_DEPT,
COALESCE ((CUMLATIVE_SAL - LAG (CUMLATIVE_SAL, 1) OVER (ORDER BY CUMLATIVE_SAL)), CUMLATIVE_SAL) AS GETTING_ACTUAL_TABLE,
COALESCE ((CUMLATIVE_SAL_DEPT - LAG (CUMLATIVE_SAL_DEPT, 1) OVER (PARTITION BY
DEPTNO ORDER BY CUMLATIVE_SAL_DEPT)), CUMLATIVE_SAL_DEPT) AS GETTING_ACTUAL_DEPTNO
FROM --- CREATING CUMULATIVE SALARIES AT TABLE, DEPT LEVEL
(SELECT EMPNO,ENAME, DEPTNO,SAL,
SUM (SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL,
SUM (SAL) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMLATIVE_SAL_DEPT
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
) ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ;
-- -------------------------------------------------------------------------------------------------------------
-- 22)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
LAG(SAL, 1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) PREV_SAL_DEPT_LAG,
LEAD(SAL, 1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) NEXT_SAL_DEPT_LEAD,
MIN(SAL) OVER ( PARTITION BY DEPTNO ) MIN_SAL_DEPT,
MAX(SAL) OVER ( PARTITION BY DEPTNO ) MAX_SAL_DEPT,
ROUND(AVG(SAL) OVER ( PARTITION BY DEPTNO ),3) AS AVG_SAL_DEPT
FROM ( SELECT * FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC)
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
;
-- -------------------------------------------------------------------------------------------------------------
-- 23)
SELECT DEPTNO,
MIN(SAL) KEEP (DENSE_RANK FIRST ORDER BY SAL) DEPT_MIN_SAL,
MAX(SAL) KEEP (DENSE_RANK LAST ORDER BY SAL) DEPT_MAX_SAL
, MAX(SAL) MAXSAL
, MIN(SAL) MINSAL
FROM EMP_CUSTOM_TABLE GROUP BY DEPTNO ORDER BY DEPTNO
;
-- -------------------------------------------------------------------------------------------------------------
-- 24)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) RANKS,
RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DEPT_RANKS,
DENSE_RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC) DENSE_RANKS,
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DENSE_RANKS_DEPT
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC
;
-- -------------------------------------------------------------------------------------------------------------
-- 25)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
LAG(SAL,1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) PREV_SAL_DEPT_LAG,
LEAD(SAL,1,0) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC) NEXT_SAL_DEPT_LAG
FROM ( SELECT * FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC)
;
-- -------------------------------------------------------------------------------------------------------------
-- 26)
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
SUM(SAL) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMULATIVE_DEPT,
SUM(SAL) OVER ( ORDER BY DEPTNO ASC, SAL DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMULATIVE_TABLE
FROM (SELECT * FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC)
;
-- -------------------------------------------------------------------------------------------------------------
-- 27)
SELECT * FROM (
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DENSE_RANKS_DEPT
FROM ( SELECT * FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC))
WHERE DENSE_RANKS_DEPT<=2
;
-- -------------------------------------------------------------------------------------------------------------
-- 27)
SELECT * FROM (
SELECT ROWNUM SEQ ,ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL ASC ) SEQ_DEPT,
EMPNO,ENAME, DEPTNO,SAL ACTUAL_SAL,
DENSE_RANK() OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL ASC) DENSE_RANKS_DEPT
FROM (SELECT * FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC, ENAME ASC))
WHERE DENSE_RANKS_DEPT<=2
;
-- -------------------------------------------------------------------------------------------------------------
--28)
SELECT ENAME,DEPTNO, SAL,
DENSE_RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC ) DENSE_RANKS,
RANK() OVER ( ORDER BY DEPTNO ASC, SAL DESC ) RANKS
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC, SAL DESC
;
-- -------------------------------------------------------------------------------------------------------------
-- 29)
SELECT ENAME,JOB,DEPTNO, SAL
, SUM(SAL) OVER() AS TOTAL_SAL_TABLE
, SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY DEPTNO ASC,SAL DESC ) AS TOTAL_SAL_DEPT
, SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY DEPTNO ASC,SAL DESC, ENAME ASC ) AS TOTAL_SAL_DEPT_EMP
, SUM(SAL) OVER(PARTITION BY JOB ORDER BY JOB ASC,SAL DESC) AS TOTAL_SAL_JOB
, SUM(SAL) OVER(PARTITION BY JOB ORDER BY JOB ASC,SAL DESC, ENAME ASC) AS TOTAL_SAL_JOB_ENAME
FROM EMP_CUSTOM_TABLE ORDER BY JOB ASC, SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
-- 30)
SELECT * FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO, JOB , SAL DESC ;
---------------------------------------------------------------------------------------------------------------
--31)
SELECT ENAME,JOB,DEPTNO, SAL FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO, JOB , SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
-- 32)
SELECT ENAME,JOB,DEPTNO, SAL
, SUM(SAL) OVER() TOTAL_SAL_TABLE
, SUM(SAL) OVER(PARTITION BY DEPTNO ) AS TOTAL_SAL_DEPT
, SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY DEPTNO ASC,SAL DESC ) AS RUNNING_TOTAL_SAL_DEPT
--, SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY DEPTNO ASC,SAL DESC, ENAME ASC ) AS TOTAL_SAL_DEPT_ENAME
FROM EMP_CUSTOM_TABLE ORDER BY DEPTNO ASC,SAL DESC ;
-- -------------------------------------------------------------------------------------------------------------
-- 33)
SELECT EMPNO,ENAME, SAL, DEPTNO,
SUM(SAL) OVER () TOTALSAL,
SUM(SAL) OVER (PARTITION BY DEPTNO) DEPTTOTALSAL,
SUM(SAL) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) DEPTTOTALSAL2,
SUM(SAL) OVER ( PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC ,ENAME
DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) CUM_SAL_DEPT,
SUM(SAL) OVER (ORDER BY DEPTNO ASC, SAL DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CUMSAL_TABLE
FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC
;
-- -------------------------------------------------------------------------------------------------------------
-- 34)
SELECT EMPNO,ENAME, SAL, DEPTNO
, LAG(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) PREVIOUS_SAL_WITH_IN_DEPT
, LEAD(SAL, 1,0) OVER (PARTITION BY DEPTNO ORDER BY DEPTNO ASC, SAL DESC) NEXT_SAL_WITH_IN_DEPT
FROM EMP_CUSTOM_TABLE
ORDER BY DEPTNO ASC, SAL DESC;
-- ========================================================
-- ***** END - WINDOW FUNCTIONS (ANALYTICAL FUNCTION) *****
-- ========================================================
No comments:
Post a Comment