Easy and Simple Solution | Beginner-friendly🚀
SQL QueryIf you found this solution helpful, please give it an upvote! 🌟✨ It really motivates me to share more solutions like this. 😊👍
Happy coding! 💻🐱‍🏍
Intuit
## **SQL Query** 
```sql
SELECT A.machine_id, 
       ROUND(AVG(B.timestamp - A.timestamp), 3) AS processing_time
FROM Activity A
JOIN Activity B
ON A.machine_id = B.machine_id 
AND A.process_id = B.process_id
AND A.activity_type = 'start' 
AND B.activity_type = 'end'
GROUP BY A.machine_id;
```
**If you found this solution helpful, please give it an upvote! 🌟✨ It really motivates me to share more solutions like this. 😊👍
Happy coding! 💻🐱‍🏍**
<img src="https://assets.leetcode.com/users/images/6e8c623a-1111-4d21-9a5b-54e00e7feabb_1738093236.165277.jpeg" width=250px>
### **Intuition**  
Each machine processes multiple tasks, with each task having a **start** and **end** timestamp. Our goal is to find the **average time** each machine takes to complete a task. To do this, we need to:  

1. **Pair Start and End Times** → Each `process_id` has a **start** and **end** timestamp. We need to match them.  
2. **Compute Processing Time** → Subtract `start` from `end` for each process.  
3. **Find the Average** → Group by `machine_id` and calculate the **average processing time**.  

---

### **Approach**  
1. **Self-Join on `machine_id` and `process_id`**  
   - Match rows where `activity_type = 'start'` with rows where `activity_type = 'end'`.  
   - This gives both timestamps in one row.  

2. **Calculate Processing Time for Each Process**  
   - Compute `end_timestamp - start_timestamp`.  

3. **Group by `machine_id` and Compute Average**
---
## **Complexity Analysis**  
- **Time Complexity**: **O(N)** → The query scans all `N` rows in the `Activity` table, grouping by `machine_id` and aggregating processing times.  
- **Space Complexity**: **O(1)** → The query does not use extra space beyond the result set.  

The **SELF JOIN** or **CASE WHEN** approach efficiently extracts start and end timestamps, ensuring correct computation of processing times per machine.

['MySQL']