🙌
Hands-On Leetcode Database Solution with Hive SQL
(Online Course)Learning SQL with meMy BlogAbout meFollow me on FBFollow my IG
  • INTRODUCTION
    • About This Book
    • 作者簡介
  • Exercise 1:185.Department Top Three Salaries
    • Hive Solution
  • Exercise 2:262. Trips and Users
    • Hive Solution
  • Exercise 3:569.Median Employee Salary
    • Hive Solution
  • Exercise 4:571.Find Median Given Frequency of Numbers
    • Hive Solution
  • Exercise 5:579.Find Cumulative Salary Of An Employee Problem
    • Hive Solution
  • Exercise 6:601.Human Traffic of Stadium
    • Hive Solution
  • Exercise 7:615.Average Salary: Departments VS Company
    • Hive Solution
  • Exercise 8:618.Students Report By Geography
    • Hive Solution
  • Exercise 9:1097.Game Play Analysis V
    • Hive Solution
  • Exercise 10:1127.User Purchase Platform
    • Hive Solution
  • Exercise 11:1159.Market Analysis II
    • Hive Solution
  • Exercise 12:1194.Tournament Winners
    • Hive Solution
  • Exercise 13:1225.Report Contiguous Dates
    • Hive Solution
  • Exercise 14:1336.The number of transactions per visit
    • Hive Solution
  • Exercise 15:1369.Get The Second Most Recent Activity
    • Hive Solution
  • Exercise 16:1384.Total Sales Amount by Year
    • Hive Solution
  • Exercise 17:1412.Find the Quiet Students in All Exams
    • Hive Solution
  • Exercise 18:1479. Sales by Day of the Week
    • Hive Solution
  • Exercise 19:1635. Hopper Company Queries I
    • Hive Solution
  • Exercise 20:1645. Hopper Company Queries II
    • Hive Solution
  • Exercise21 :1651. Hopper Company Queries III
    • Hive Solution
  • Exercise 22:1767.Find the Subtasks That Did Not Execute
    • Hive Solution
  • Exercise 23:1892.Page Recommendations II
    • Hive Solution
  • Exercise 24:1917.Leetcodify Friends Recommendations
    • Hive Solution
  • Exercise 25:1919.Leetcodify Similar Friends
    • Hive Solution
  • Exercise 26:1972.First and Last Call On the Same Day
    • Hive Solution
  • Exercise 27:2004.The Number of Seniors and Juniors to Join the Company
    • Hive Solution
  • Exercise 28:2010.The Number of Seniors and Juniors to Join the Company II
    • Hive Solution
Powered by GitBook
On this page
  1. Exercise 27:2004.The Number of Seniors and Juniors to Join the Company

Hive Solution

3.Hive Solution

-- **Example 1:**

create table leetcode.ex_2004_s1 as 
SELECT 
    employee_id,
    experience,
    salary,
    salary_sum,
    (70000 - salary_sum) as balance  
FROM
(select *,sum(salary)over(ORDER BY rn) as salary_sum 
from  (select *, ROW_NUMBER() OVER( ORDER BY salary) AS rn from leetcode.ex_2004_candidates_1
 where experience  = 'Senior' ) a
 ) b 
 ;

create table leetcode.ex_2004_j1 as 
SELECT 
    t1.employee_id,
    t1.experience,
    t1.salary,
    t1.salary_sum,
    (t2.balance_min_senior - t1.salary_sum) as balance 
FROM 
( select 'id' as id,*,sum(salary)over(ORDER BY rn) as salary_sum
from (select *, ROW_NUMBER() OVER(ORDER BY salary) AS rn from leetcode.ex_2004_candidates_1
 where experience  = 'Junior' ) a 
 ) t1
 join (select 'id' as id, min(balance) as balance_min_senior from leetcode.ex_2004_s 
        where balance>0 ) t2  ON t1.id=t2.id 
;
    
    
select experience,count(employee_id) as accepted_candidates 
    from leetcode.ex_2004_s1 where balance>=0 
        group by experience
UNION
select  experience,count(employee_id) as accepted_candidates 
    from leetcode.ex_2004_j1 where balance>=0
        group by experience
        ;


-- **Example 2:**
create table leetcode.ex_2004_s2 as 
SELECT 
    employee_id,
    experience,
    salary,
    salary_sum,
    (70000 - salary_sum) as balance, 
FROM
(select *,sum(salary)over(ORDER BY rn) as salary_sum 
from  (select *, ROW_NUMBER() OVER( ORDER BY salary) AS rn from leetcode.ex_2004_candidates_2
 where experience  = 'Senior' ) a
 ) b 
 ;
 select * from leetcode.ex_2004_s2;

create table leetcode.ex_2004_j2 as 
SELECT 
    t1.employee_id,
    t1.experience,
    t1.salary,
    t1.salary_sum,
    (t2.balance_min_senior - t1.salary_sum) as balance 
FROM 
( select 'id' as id,*,sum(salary)over(ORDER BY rn) as salary_sum
from (select *, ROW_NUMBER() OVER(ORDER BY salary) AS rn from leetcode.ex_2004_candidates_2
 where experience  = 'Junior' ) a 
 ) t1
 join (select 'id' as id, coalesce(min(balance),70000) as balance_min_senior from leetcode.ex_2004_s2 
       where balance >0 
        ) t2  ON t1.id=t2.id 
    ;
 

select 'Senior' as experience,coalesce(count(employee_id),0) as accepted_candidates 
    from leetcode.ex_2004_s2 where balance>=0 
UNION
select 'Junior' as experience,coalesce(count(employee_id),0) as accepted_candidates 
    from leetcode.ex_2004_j2 where balance>=0
        ;

PreviousExercise 27:2004.The Number of Seniors and Juniors to Join the CompanyNextExercise 28:2010.The Number of Seniors and Juniors to Join the Company II

Last updated 3 years ago