SQL Script Window Functions-Part 2
SQL Script Window Functions-Part 2
-- FIRST_VALUE
-- Write query to display the most expensive product under each category
(corresponding to each record)
select *,
first_value(product_name) over(partition by product_category order by price desc)
as most_exp_product
from product;
-- LAST_VALUE
-- Write query to display the least expensive product under each category
(corresponding to each record)
select *,
first_value(product_name)
over(partition by product_category order by price desc)
as most_exp_product,
last_value(product_name)
over(partition by product_category order by price desc
range between unbounded preceding and unbounded following)
as least_exp_product
from product
WHERE product_category ='Phone';
-- NTH_VALUE
-- Write query to display the Second most expensive product under each category.
select *,
first_value(product_name) over w as most_exp_product,
last_value(product_name) over w as least_exp_product,
nth_value(product_name, 5) over w as second_most_exp_product
from product
window w as (partition by product_category order by price desc
range between unbounded preceding and unbounded following);
-- NTILE
-- Write a query to segregate all the expensive phones, mid range phones and the
cheaper phones.
select x.product_name,
case when x.buckets = 1 then 'Expensive Phones'
when x.buckets = 2 then 'Mid Range Phones'
when x.buckets = 3 then 'Cheaper Phones' END as Phone_Category
from (
select *,
ntile(3) over (order by price desc) as buckets
from product
where product_category = 'Phone') x;
-- Query to identify how much percentage more expensive is "Galaxy Z Fold 3" when
compared to all products.
select product_name, per
from (
select *,
percent_rank() over(order by price) ,
round(percent_rank() over(order by price)::numeric * 100, 2) as per
from product) x
where x.product_name='Galaxy Z Fold 3';