|
| 1 | +/* |
| 2 | +=================================================== |
| 3 | +IRCTC Real-Time Data Pipeline - SQL Queries |
| 4 | +Author: Sujit Mahapatra |
| 5 | +Description: This file contains essential SQL scripts |
| 6 | +for data processing, transformation, and analysis |
| 7 | +in BigQuery as part of the IRCTC real-time data pipeline. |
| 8 | +=================================================== |
| 9 | +*/ |
| 10 | + |
| 11 | +-- ========================================= |
| 12 | +-- 1. Insert Sample Data into IRCTC Table |
| 13 | +-- ========================================= |
| 14 | +INSERT INTO `irctc_dwh.irctc_stream_tb` |
| 15 | +(row_key, name, age, email, join_date, last_login, loyalty_points, account_balance, is_active, inserted_at, updated_at, loyalty_status, account_age_days) |
| 16 | +VALUES |
| 17 | +('1', 'Amit Kumar', 28, 'amit.k@example.com', '2020-01-15', CURRENT_TIMESTAMP(), 500, 2000.50, TRUE, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 'Gold', 1500); |
| 18 | + |
| 19 | +-- ========================================= |
| 20 | +-- 2. Retrieve Recently Active Users |
| 21 | +-- ========================================= |
| 22 | +SELECT name, email, last_login, loyalty_status |
| 23 | +FROM `irctc_dwh.irctc_stream_tb` |
| 24 | +WHERE is_active = TRUE |
| 25 | +ORDER BY last_login DESC |
| 26 | +LIMIT 100; |
| 27 | + |
| 28 | +-- ========================================= |
| 29 | +-- 3. Compute Loyalty Points Summary |
| 30 | +-- ========================================= |
| 31 | +SELECT loyalty_status, COUNT(*) AS total_users, AVG(loyalty_points) AS avg_points |
| 32 | +FROM `irctc_dwh.irctc_stream_tb` |
| 33 | +GROUP BY loyalty_status |
| 34 | +ORDER BY avg_points DESC; |
| 35 | + |
| 36 | +-- ========================================= |
| 37 | +-- 4. Identify Users with Low Account Balance |
| 38 | +-- ========================================= |
| 39 | +SELECT name, email, account_balance |
| 40 | +FROM `irctc_dwh.irctc_stream_tb` |
| 41 | +WHERE account_balance < 500 |
| 42 | +ORDER BY account_balance ASC; |
| 43 | + |
| 44 | +-- ========================================= |
| 45 | +-- 5. Detect Inactive Users (Last Login > 1 Year) |
| 46 | +-- ========================================= |
| 47 | +SELECT name, email, last_login |
| 48 | +FROM `irctc_dwh.irctc_stream_tb` |
| 49 | +WHERE last_login < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 YEAR); |
| 50 | + |
| 51 | +-- ========================================= |
| 52 | +-- 6. Update Loyalty Status Based on Points |
| 53 | +-- ========================================= |
| 54 | +UPDATE `irctc_dwh.irctc_stream_tb` |
| 55 | +SET loyalty_status = |
| 56 | + CASE |
| 57 | + WHEN loyalty_points >= 1000 THEN 'Platinum' |
| 58 | + WHEN loyalty_points BETWEEN 500 AND 999 THEN 'Gold' |
| 59 | + ELSE 'Silver' |
| 60 | + END |
| 61 | +WHERE is_active = TRUE; |
| 62 | + |
| 63 | +-- ========================================= |
| 64 | +-- 7. Remove Inactive Users (More than 2 Years) |
| 65 | +-- ========================================= |
| 66 | +DELETE FROM `irctc_dwh.irctc_stream_tb` |
| 67 | +WHERE is_active = FALSE |
| 68 | +AND last_login < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 2 YEAR); |
0 commit comments