🙌
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.Description
  • 2.Create Table and insert into values

Exercise 22:1767.Find the Subtasks That Did Not Execute

1.Description

Table: Tasks

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| task_id        | int     |
| subtasks_count | int     |
+----------------+---------+
task_id is the primary key for this table.
Each row in this table indicates that task_id was divided into subtasks_count subtasks labelled from 1 to subtasks_count.
It is guaranteed that 2 <= subtasks_count <= 20.

Table: Executed

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| task_id       | int     |
| subtask_id    | int     |
+---------------+---------+
(task_id, subtask_id) is the primary key for this table.
Each row in this table indicates that for the task task_id, the subtask with ID subtask_id was executed successfully.
It is guaranteed that subtask_id <= subtasks_count for each task_id.

Write an SQL query to report the IDs of the missing subtasks for each task_id.

Return the result table in any order.

The query result format is in the following example:

Tasks table:
+---------+----------------+
| task_id | subtasks_count |
+---------+----------------+
| 1       | 3              |
| 2       | 2              |
| 3       | 4              |
+---------+----------------+

Executed table:
+---------+------------+
| task_id | subtask_id |
+---------+------------+
| 1       | 2          |
| 3       | 1          |
| 3       | 2          |
| 3       | 3          |
| 3       | 4          |
+---------+------------+

Result table:
+---------+------------+
| task_id | subtask_id |
+---------+------------+
| 1       | 1          |
| 1       | 3          |
| 2       | 1          |
| 2       | 2          |
+---------+------------+
Task 1 was divided into 3 subtasks (1, 2, 3). Only subtask 2 was executed successfully, so we include (1, 1) and (1, 3) in the answer.
Task 2 was divided into 2 subtasks (1, 2). No subtask was executed successfully, so we include (2, 1) and (2, 2) in the answer.
Task 3 was divided into 4 subtasks (1, 2, 3, 4). All of the subtasks were executed successfully.

2.Create Table and insert into values

create table if not exists leetcode.ex_1767_tasks
(task_id string, subtasks_count int) stored as orc ;

INSERT INTO table leetcode.ex_1767_tasks VALUES
('1','3'),
('2','2'),
('3','4') ; 

create table if not exists leetcode.ex_1767_executed
(task_id string , subtask_id string ) stored as orc ;

INSERT INTO table leetcode.ex_1767_executed VALUES
('1','2'),
('3','1'),
('3','2'),
('3','3'),
('3','4') ; 

PreviousHive SolutionNextHive Solution

Last updated 3 years ago

In SQL, we create an identity column to auto-generate incremental values by IDENTITY Function while this system function does not exist in Hive. We can create a UDF function() .

user-defined function