July 31, 2018

Find leave taken in alternate intervals within a month

We saw the Rank Function in my previous post for sorting records. In the same way using the function we can get the count of leaves taken in alternate intervals with a month for an employee.

For example:
Leaves taken on 1,2,3,4 of a month and came to office on 5th and again went on leave from 6th. the summary of absence using the query can be obtained.

select min(date_taken) date_from,
       max(date_taken) date_to,
       count(*) num_days
from (
  select date_taken,
         date_taken-row_number() over(order by date_taken) as daysto
         from Absence
     )
group by daysto
order by 1
DATE_FROMDATE_TONUM_SAMPLES
01-DEC-1504-DEC-154
07-DEC-1510-DEC-154
14-DEC-1516-DEC-153
19-DEC-1520-DEC-152


July 29, 2018

RANK Function

RANK Function:

Each entry can be ranked based on any of the given criteria. For e.g:- grading system in a school.

Syntax:
Function
(arg1,arg2)
OVER(
Partition Clause-Sort as per a particular criteria(eg.job,grade)
Sorting Clause-Normal sort of full record
Windowing Clause- sort based on any one record value.
)


Function -Rank, Dense Rank, Row number

Rank:

1 2 3 3 - Two person are in tie while ranking both will be given same ranking by the next rank will be  added value of one more position i.e

1 2 3 3 5- the 3rd position has a tie and occupies 2 position with same ranking, so the next person will be pushed to 5th in order ranking.

E.g:

Select empno,empname,job,sal,
rank() OVER (order by sal) as rank_sal from emp order by sal.

Dense Ranking:

1 2 3 3 4 -  the 3rd position has a tie and occupies 2 position with same ranking, but the next person will be ranked to next sequence 4th.


Select empno,empname,job,sal,
dense_rank() OVER (order by sal) as rank_sal from emp order by sal.



Row number

1 2 3 4 5 6  -Tie records will be ordered as per the column we add to order and the tie will replaced to sequential

Based on what field we need to position the tie records we can mention in sorting clause.

E.g:

Select empno,empname,job,sal,
row_number() OVER (order by sal,empno) as rank_sal from emp order by sal.

here empno column next to order by clause indicates that the tie should be ranked with precedance of employee number.


July 26, 2018

Basics of Tables


Concepts in 18 C:

Let see about tables.

In a relational database tables play a vital part to organize data and regularize it as per size and requirement.

Let’s see about permanent tables, temporary tables, external tables, heap organized tables, index-organized tables.

You can define table into two categories: Relational and object tables:

Relational tables are simple and common tables. For example, recording list of students, employees etc. No need to look for dependencies while creating.

Object type tables are table with reference to the application it is related to. The table will be designed for a purpose and also validating or summarizing the dependence’s.

Relational Tables:

S.no
Emp Name
Emp ID
Dept ID
1
a
12
0001
2
b
23
0002

Object Type:

Test
Maths
Science
English
Total
1
100
100
100
300
2
100
100
100
300


create type stud as object (
          maths number(3),
          phy   number(3),
          chem  number(3),
          member function tot return number);

functions can be called inside the object type column.


Ok, Let’s see about table structures:

 heap-organized table :Stores data where ever the storage is available. As normal as our own table at office. Place the things as we go.
index-organized table: Stores data with its primary key as key reference for sequential ordering of storing of data. Or by using pseudocolumn to be referenced to sequence data. Index is not a separate column which points to the data. Instead works as organized data which is sequential and easy to identify in the order we arrange it.
Temporary table:  for a particular session and only be accessed by its owner. While the session gets completed the data will be erased.
 External table: is a read-only table whose metadata is stored in the database but whose data is stored outside the database. For example, the text file needs to be loaded in ETL from a other database and which creates a virtual column in our database while loading.

COALESCE-SQL

Coalesce- return the null values from the expression. It works similar to a case statement where if expression 1 is false then goes to expr...