Skip to content

1336. Number of Transactions per Visit 👎

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
WITH
  Users AS (
    SELECT
      Visits.user_id,
      Visits.visit_date,
      COUNT(Transactions.transaction_date) AS transaction_count
    FROM Visits
    LEFT JOIN Transactions
      ON (
        Visits.user_id = Transactions.user_id
        AND Visits.visit_date = Transactions.transaction_date)
    GROUP BY 1, 2
  ),
  RowNumbers AS (
    SELECT ROW_NUMBER() OVER() AS `row_number`
    FROM Transactions
    UNION ALL
    SELECT 0
  )
SELECT
  RowNumbers.`row_number` AS transactions_count,
  COUNT(Users.user_id) AS visits_count
FROM RowNumbers
LEFT JOIN Users
  ON (RowNumbers.`row_number` = Users.transaction_count)
WHERE RowNumbers.`row_number` <= (
    SELECT MAX(transaction_count) FROM Users
  )
GROUP BY 1
ORDER BY 1;