SQL Challenge: Calculate Immediate First Orders Percentage

🗓️ SQL Challenge Day #21: Immediate Food Delivery II 🔹 Calculate percentage of immediate first orders! 🚚 🔹 Problem:   Find percentage of immediate orders in first orders:   ✅ Immediate = order_date = customer_pref_delivery_date   ✅ First order = earliest order per customer   ✅ Round to 2 decimal places  🔹 Solution:   SELECT     ROUND(AVG(CASE           WHEN order_date = customer_pref_delivery_date THEN 1           ELSE 0         END) * 100.0, 2) AS immediate_percentage   FROM (     SELECT       MIN(order_date) AS order_date,       MIN(customer_pref_delivery_date) AS customer_pref_delivery_date     FROM delivery     GROUP BY customer_id   ) t;  ✅ Result: Accepted  💡 Key Takeaway:   **MIN() in subquery + AVG(CASE)** is perfect for "first occurrence" problems! The subquery isolates first orders, while AVG(CASE) elegantly calculates the percentage without separate counts.  👇 Your turn:   What's the most interesting real-world metric you've calculated using window functions or subqueries?  #SQL #LeetCode #DataEngineering #ProblemSolving #Coding #LearningInPublic #Database #DataAnalytics

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories