Leetcode Problem 1336. Number of Transactions per Visit

1336. Number of Transactions per Visit

Leetcode Solutions

Counting Transactions Per Visit and Generating a Distribution Chart

  1. Perform a LEFT JOIN between the Visits and Transactions tables on user_id and visit_date = transaction_date to include all visits, even those without transactions.
  2. Group the result by user_id and visit_date, and count the number of transactions for each group to get the transactions_count.
  3. Group the counts obtained in step 2 by transactions_count and count the number of visits for each transaction count to get the visits_count.
  4. Generate a series of numbers from 0 to the maximum transactions_count observed.
  5. LEFT JOIN the distribution data obtained in step 3 on the series generated in step 4 to ensure all transaction counts are represented.
  6. Replace any NULL values in visits_count with 0, as these represent transaction counts with no visits.
  7. Order the final result by transactions_count.
erDiagram
    Visits {
        int user_id
        date visit_date
    }
    Transactions {
        int user_id
        date transaction_date
        int amount
    }
    Visits ||--o{ Transactions : ""

Alternative Approach Using a Numbers Table

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...