Analytic functions in Netezza

Analytic functions:-

Below are the Analytic function available in Netezza

2) RANK ()


4) FIRST()
5) LAST()
6) LEAD() and LAG()
The advantages of Analytic function over Group by Function.

GROUP BY Function

The group by function allow us to select  columns which are part of group by and column on which aggregation is performed. 

I want to display maximum salary employee from each dept.

Query and result:-

Here is the Question i want to know the Employee name as well along with max sal and in which dept he works for.

Query and result:-

As we discussed we can’t use Ename as its is not part of group by nor we are doing aggregation on this column.

Lets see what flexibility we will get by using Analytic functions.

SELECT maxsal, 
FROM   ( 
                SELECT   Max(sal) OVER (partition BY detpno ORDER BY sal)        AS maxsal, 
                         row_number over (partition BY detpno ORDER BY sal DESC) AS id, 
                FROM     emp) 
WHERE  id=1;

just place id=2 instead of id=1 for second max salary 
if ur are expecting more than 1 records with max sal 
replace ROW_NUMBER with DENSE_RANK()

Please refer the below site to know more about Analytic function.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s