fork download
  1. -- HIVE SCRIPT FOR HABIT TRACKER
  2.  
  3. -- STEP 1: DROP EXISTING TABLES IF THEY ALREADY EXIST
  4. DROP TABLE IF EXISTS users;
  5. DROP TABLE IF EXISTS habits;
  6. DROP TABLE IF EXISTS habit_logs;
  7.  
  8. -- STEP 2: CREATE TABLES FOR USERS, HABITS, AND HABIT LOGS
  9.  
  10. -- Create users table to store user information
  11. CREATE TABLE users (
  12. user_id INT,
  13. name STRING,
  14. email STRING
  15. )
  16. ROW FORMAT DELIMITED
  17. FIELDS TERMINATED BY ','
  18. STORED AS TEXTFILE;
  19.  
  20. -- Create habits table to store habit details for each user
  21. CREATE TABLE habits (
  22. habit_id INT,
  23. user_id INT,
  24. habit_name STRING,
  25. description STRING,
  26. goal STRING
  27. )
  28. ROW FORMAT DELIMITED
  29. FIELDS TERMINATED BY ','
  30. STORED AS TEXTFILE;
  31.  
  32. -- Create habit_logs table to track daily habit completion
  33. CREATE TABLE habit_logs (
  34. log_id INT,
  35. habit_id INT,
  36. user_id INT,
  37. date STRING, -- format YYYY-MM-DD
  38. status STRING -- "completed" or "missed"
  39. )
  40. ROW FORMAT DELIMITED
  41. FIELDS TERMINATED BY ','
  42. STORED AS TEXTFILE;
  43.  
  44. -- STEP 3: INSERT SAMPLE DATA INTO THE TABLES
  45.  
  46. -- Insert sample data for users
  47. INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
  48. INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');
  49.  
  50. -- Insert sample data for habits
  51. INSERT INTO habits VALUES (1, 1, 'Exercise', 'Daily exercise routine', '30 days');
  52. INSERT INTO habits VALUES (2, 1, 'Read', 'Read a book for 15 minutes', '15 days');
  53. INSERT INTO habits VALUES (3, 2, 'Meditate', '10 minutes of meditation', '20 days');
  54.  
  55. -- Insert sample data for habit logs (dates are in YYYY-MM-DD format)
  56. INSERT INTO habit_logs VALUES (1, 1, 1, '2024-10-01', 'completed');
  57. INSERT INTO habit_logs VALUES (2, 1, 1, '2024-10-02', 'completed');
  58. INSERT INTO habit_logs VALUES (3, 1, 1, '2024-10-03', 'missed');
  59. INSERT INTO habit_logs VALUES (4, 2, 1, '2024-10-01', 'completed');
  60. INSERT INTO habit_logs VALUES (5, 3, 2, '2024-10-01', 'completed');
  61.  
  62. -- STEP 4: QUERIES FOR HABIT TRACKER REPORTING
  63.  
  64. -- QUERY 1: Retrieve All Habits for a User
  65. -- This query fetches all habits for a specified user by name.
  66. SELECT
  67. h.habit_name,
  68. h.description,
  69. h.goal
  70. FROM
  71. habits h
  72. JOIN
  73. users u ON h.user_id = u.user_id
  74. WHERE
  75. u.name = 'Alice';
  76.  
  77. -- QUERY 2: Check Daily Progress on a Specific Habit
  78. -- This query retrieves completion records for a specific habit by habit_id.
  79. SELECT
  80. date,
  81. status
  82. FROM
  83. habit_logs
  84. WHERE
  85. habit_id = 1 -- Replace with the habit_id you want to track
  86. ORDER BY
  87. date;
  88.  
  89. -- QUERY 3: Generate Habit Completion Rate for a User
  90. -- This query calculates the completion rate (in percentage) for each habit of a user.
  91. SELECT
  92. h.habit_name,
  93. COUNT(CASE WHEN hl.status = 'completed' THEN 1 END) * 100.0 / COUNT(*) AS completion_rate
  94. FROM
  95. habit_logs hl
  96. JOIN
  97. habits h ON hl.habit_id = h.habit_id
  98. WHERE
  99. hl.user_id = 1 -- Replace with specific user_id
  100. GROUP BY
  101. h.habit_name;
  102.  
  103. -- END OF SCRIPT
  104.  
Success #stdin #stdout #stderr 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 11: near "ROW": syntax error
Error: near line 21: near "ROW": syntax error
Error: near line 33: near "ROW": syntax error
Error: near line 47: no such table: users
Error: near line 48: no such table: users
Error: near line 51: no such table: habits
Error: near line 52: no such table: habits
Error: near line 53: no such table: habits
Error: near line 56: no such table: habit_logs
Error: near line 57: no such table: habit_logs
Error: near line 58: no such table: habit_logs
Error: near line 59: no such table: habit_logs
Error: near line 60: no such table: habit_logs
Error: near line 66: no such table: habits
Error: near line 79: no such table: habit_logs
Error: near line 91: no such table: habit_logs