![$[R9%()RRI4@(%SH[)C2FM.png](https://cdn.nlark.com/yuque/0/2022/png/25786324/1647155523695-4cda4661-a368-4b30-86ba-e6ecca94f5dc.png#clientId=u85b4d441-37f4-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u943099dc&margin=%5Bobject%20Object%5D&name=%24%5BR9%25%28%29RRI4%40%28%25SH%5B%29C2FM.png&originHeight=447&originWidth=886&originalType=binary&ratio=1&rotation=0&showTitle=false&size=22971&status=done&style=none&taskId=ua7751f27-37f1-4027-9bae-b8d77942926&title=)
这一题需要用到 RECURSIVE 递归查询
WITH RECURSIVE t AS (
SELECT 0 as n
UNION ALL
SELECT n+1 FROM t WHERE n <
(SELECT count(*) FROM Transactions GROUP BY user_id,transaction_date
order by count(*) desc limit 1)
)
SELECT n transactions_count, count(cnt) visits_count FROM t
LEFT JOIN
(
SELECT count(transaction_date) cnt FROM Visits V LEFT JOIN Transactions T
ON (V.user_id,visit_date) = (T.user_id,transaction_date)
GROUP BY V.user_id,visit_date
) A
ON n = cnt GROUP BY n