# Hive Solution

## 3.Hive Solution

```sql
-- **Example 1:**
create table leetcode.ex_2010_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_2010_candidates_1
 where experience  = 'Senior' ) a
 ) b 
 ;
 
create table leetcode.ex_2010_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_2010_candidates_1
 where experience  = 'Junior' ) a 
 ) t1
 join (select 'id' as id, coalesce(min(balance),70000) as balance_min_senior from leetcode.ex_2010_s1 
       where balance >0 
        ) t2  ON t1.id=t2.id 
;
 
select  employee_id 
    from leetcode.ex_2010_s1 where balance>=0 
UNION
select employee_id
    from leetcode.ex_2010_j1 where balance>=0
        ;

-- **Example 2:**
create table leetcode.ex_2010_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_2010_candidates_2
 where experience  = 'Senior' ) a
 ) b 
 ;
 
create table leetcode.ex_2010_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_2010_candidates_2
 where experience  = 'Junior' ) a 
 ) t1
 join (select 'id' as id, coalesce(min(balance),70000) as balance_min_senior from leetcode.ex_2010_s2 
       where balance >0 
        ) t2  ON t1.id=t2.id 
;
 
select  employee_id 
    from leetcode.ex_2010_s2 where balance>=0 
UNION
select employee_id
    from leetcode.ex_2010_j2 where balance>=0
        ;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chiu-kuohsin.gitbook.io/leetcode-database-solution-with-hive-sql/exercise-28-2010.the-number-of-seniors-and-juniors-to-join-the-company-ii/hive-solution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
