Professional Documents
Culture Documents
Day7 Assignment Solutions
Day7 Assignment Solutions
you need
to solve the questions only with the concepts that have been discussed so far.
Run below table script to create icc_world_cup table:
with all_teams as
(select team_1 as team, case when team_1=winner then 1 else 0 end as win_flag from
icc_world_cup
union all
select team_2 as team, case when team_2=winner then 1 else 0 end as win_flag from
icc_world_cup)
select team,count(1) as total_matches_played , sum(win_flag) as
matches_won,count(1)-sum(win_flag) as matches_lost
from all_teams
group by team
2- write a query to print first name and last name of a customer using orders
table(everything after first space can be considered as last name)
customer_name, first_name,last_name
3- write a query to print below output using drivers table. Profit rides are the no
of rides where end location of a ride is same as start location of immediate next
ride for a driver
id, total_rides , profit_rides
dri_1,5,1
dri_2,2,0
--lead function window
select id, count(1) as total_rides
,sum(case when end_loc=next_start_location then 1 else 0 end ) as profit_rides
from (
select *
, lead(start_loc,1) over(partition by id order by start_time asc) as
next_start_location
from drivers) A
group by id;
--self join
with rides as (
select *,row_number() over(partition by id order by start_time asc) as rn
from drivers)
select r1.id , count(1) total_rides, count(r2.id) as profit_rides
from rides r1
left join rides r2
on r1.id=r2.id and r1.end_loc=r2.start_loc and r1.rn+1=r2.rn
group by r1.id
4- write a query to print customer name and no of occurence of character 'n' in the
customer name.
customer_name , count_of_occurence_of_n
select customer_name , len(customer_name)-len(replace(lower(customer_name),'n',''))
as count_of_occurence_of_n
from orders
5-write a query to print below output from orders data. example output
hierarchy type,hierarchy
name ,total_sales_in_west_region,total_sales_in_east_region
category , Technology, ,
category, Furniture, ,
category, Office Supplies, ,
sub_category, Art , ,
sub_category, Furnishings, ,
--and so on all the category ,subcategory and ship_mode hierarchies
select
'category' as hierarchy_type,category as hierarchy_name
,sum(case when region='West' then sales end) as total_sales_in_west_region
,sum(case when region='East' then sales end) as total_sales_in_east_region
from orders
group by category
union all
select
'sub_category',sub_category
,sum(case when region='West' then sales end) as total_sales_in_west_region
,sum(case when region='East' then sales end) as total_sales_in_east_region
from orders
group by sub_category
union all
select
'ship_mode ',ship_mode
,sum(case when region='West' then sales end) as total_sales_in_west_region
,sum(case when region='East' then sales end) as total_sales_in_east_region
from orders
group by ship_mode