Window Function
Windowing in Hive includes the following functions.
Lead:- The number of rows to lead can optionally be specified. If the number of rows to lead is not specified, the lead is one row. Returns null when the lead for the current row extends beyond the end of the window.
select ticker,date_,close,lag(close,1) over(partition by ticker) as yesterday_price from stocks;

Lag:- The number of rows to lag can optionally be specified. If the number of rows to lag is not specified, the lag is one row. Returns null when the lag for the current row extends before the beginning of the window.
FIRST_VALUE:- It returns the value of the first row from that window. With the below query, you can see the first row high price of the ticker for all the days.
select ticker,first_value(high) over(partition by ticker) as first_high from stocks;

LAST_VALUE:- It is the reverse of FIRST_VALUE. It returns the value of the last row from that window. With the below query, you can see the last row high price value of the ticker for all the days.
select ticker,last_value(high) over(partition by ticker) as first_high from stocks

OVER clause:- OVER with standard aggregates with COUNT, SUM, MIN, MAX, AVG,OVER with a PARTITION BY statement with one or more partitioning columns. OVER with PARTITION BY and ORDER BY with one or more partitioning and/or ordering columns.
Analytics functions
- RANK
- ROW_NUMBER
- DENSE_RANK
- CUME_DIST
- PERCENT_RANK
- NTILE
To give you a brief idea of these windowing functions in Hive, we will be using stock market data. You can download the sample Stock data from here and load into your stocks table.
create table stocks
(date_ String,
Ticker String,
Open Double,
High Double,
Low Double,
Close Double,
Volume_for_the_day int
) row format delimited fields terminated by ',';
load data local inpath '/home/cloudera/Desktop/data/stocks.txt' into table stocks;
Count:- It returns the count of all the values for the expression written in the over clause. From the below query, we can find the number of rows present for each ticker.
select ticker,count(ticker) over(partition by ticker) as cnt from stocks

Sum:- It returns the sum of all the values for the expression written in the over clause. From the below query, we can find the sum of all the closing stock prices for that particular ticker.
Find Running Total
select ticker,date_,volume_for_the_day,sum(volume_for_the_day) over(partition by ticker order by date_) as running_total from stocks
Finding the percentage of each row value
Now let’s take a scenario where you need to find the percentage of the volume_for_the_day on the total volumes for that particular ticker and that can be done as follows.
Now let’s take a scenario where you need to find the percentage of the volume_for_the_day on the total volumes for that particular ticker and that can be done as follows.
select ticker,date_,volume_for_the_day,(volume_for_the_day*100/(sum(volume_for_the_day) over(partition by ticker))) from stocks;

Min
It returns the minimum value of the column for the rows in that over clause. From the below query, we can find the minimum closing stock price for each particular ticker.
It returns the minimum value of the column for the rows in that over clause. From the below query, we can find the minimum closing stock price for each particular ticker.
select ticker, min(close) over(partition by ticker) as minimum from stocks
Max
It returns the maximum value of the column for the rows in that over clause. From the below query, we can find the maximum closing stock price for each particular ticker.
It returns the maximum value of the column for the rows in that over clause. From the below query, we can find the maximum closing stock price for each particular ticker.
select ticker, max(close) over(partition by ticker) as maximum from stocks;
AVG
It returns the average value of the column for the rows that over clause returns. From the below query, we can find the average closing stock price for each particular ticker.
It returns the average value of the column for the rows that over clause returns. From the below query, we can find the average closing stock price for each particular ticker.
select ticker, avg(close) over(partition by ticker) as average from stocks;
Rank
The rank function will return the rank of the values as per the result set of the over clause. If two values are same then it will give the same rank to those 2 values and then for the next value, the sub-sequent rank will be skipped. The below query will rank the closing prices of the stock for each ticker. The same you can see in the below screenshot.
The rank function will return the rank of the values as per the result set of the over clause. If two values are same then it will give the same rank to those 2 values and then for the next value, the sub-sequent rank will be skipped. The below query will rank the closing prices of the stock for each ticker. The same you can see in the below screenshot.
select ticker,close,rank() over(partition by ticker order by close) as closing from stocks

Row_number
Row number will return the continuous sequence of numbers for all the rows of the result set of the over clause. From the below query, you will get the ticker, closing price and its row number for each ticker.
Row number will return the continuous sequence of numbers for all the rows of the result set of the over clause. From the below query, you will get the ticker, closing price and its row number for each ticker.
select ticker,close,row_number() over(partition by ticker order by close) as num from stocks;
Dense_rank
It is same as the rank() function but the difference is if any duplicate value is present then the rank will not be skipped for the subsequent rows. Each unique value will get the ranks in a sequence. The below query will rank the closing prices of the stock for each ticker. The same you can see in the below screenshot.
It is same as the rank() function but the difference is if any duplicate value is present then the rank will not be skipped for the subsequent rows. Each unique value will get the ranks in a sequence. The below query will rank the closing prices of the stock for each ticker. The same you can see in the below screenshot.
select ticker,close,dense_rank() over(partition by ticker order by close) as closing from stocks
Percent_rank
It returns the percentage rank of each row within the result set of over clause. Percent_rank is calculated in accordance with the rank of the row and the calculation is as follows (rank-1)/(total_rows_in_group – 1). If the result set has only one row then the percent_rank will be 0. The below query will calculate the percent_rank for every row in each partition and you can see the same in the below screen shot.
It returns the percentage rank of each row within the result set of over clause. Percent_rank is calculated in accordance with the rank of the row and the calculation is as follows (rank-1)/(total_rows_in_group – 1). If the result set has only one row then the percent_rank will be 0. The below query will calculate the percent_rank for every row in each partition and you can see the same in the below screen shot.
select ticker,close,percent_rank() over(partition by ticker order by close) as closing from stocks
The TO_DATE function returns the date part of the timestamp in the format ‘yyyy-MM-dd’.
select TO_DATE('2000-01-01 10:20:30');
The YEAR function returns the year part of the date.
select YEAR('2000-01-01 10:20:30');
The MONTH function returns the year part of the date.
select MONTH('2000-01-01 10:20:30');
STR_TO_MAP
STR_TO_MAP explained:
str_to_map(arg1,arg2,arg3)
arg1 => String to process
arg2 => Key Value Pair separator
arg3 => Key Value separator
select str_to_map("a=1 b=42 x=abc", " ", "=") as test;
select str_to_map("a=1 b=42 x=abc", " ", "=")["a"] as test;
No comments:
Post a Comment