diff --git a/.prettierrc b/.prettierrc index dece4466002ba..90ec8297e71b4 100644 --- a/.prettierrc +++ b/.prettierrc @@ -29,7 +29,8 @@ "solution/1500-1599/1555.Bank Account Summary/Solution.sql", "solution/1600-1699/1667.Fix Names in a Table/Solution.sql", "solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql", - "solution/2600-2699/2686.Immediate Food Delivery III/Solution.sql" + "solution/2600-2699/2686.Immediate Food Delivery III/Solution.sql", + "solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql" ], "options": { "parser": "bigquery" diff --git a/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README.md b/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README.md index 1a2757ba6178c..b6985dc7f1a63 100644 --- a/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README.md +++ b/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README.md @@ -71,7 +71,29 @@ In total, the highest number of consecutive transactions is 3, achieved by custo ```sql - +# Write your MySQL query statement below +WITH + s AS ( + SELECT + customer_id, + date_sub( + transaction_date, + INTERVAL row_number() OVER ( + PARTITION BY customer_id + ORDER BY transaction_date + ) DAY + ) AS transaction_date + FROM Transactions + ), + t AS ( + SELECT customer_id, transaction_date, count(1) AS cnt + FROM s + GROUP BY 1, 2 + ) +SELECT customer_id +FROM t +WHERE cnt = (SELECT max(cnt) FROM t) +ORDER BY customer_id; ``` diff --git a/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README_EN.md b/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README_EN.md index cd7b3fec3f4b8..0aa47586d3908 100644 --- a/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README_EN.md +++ b/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/README_EN.md @@ -65,7 +65,29 @@ In total, the highest number of consecutive transactions is 3, achieved by custo ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + s AS ( + SELECT + customer_id, + date_sub( + transaction_date, + INTERVAL row_number() OVER ( + PARTITION BY customer_id + ORDER BY transaction_date + ) DAY + ) AS transaction_date + FROM Transactions + ), + t AS ( + SELECT customer_id, transaction_date, count(1) AS cnt + FROM s + GROUP BY 1, 2 + ) +SELECT customer_id +FROM t +WHERE cnt = (SELECT max(cnt) FROM t) +ORDER BY customer_id; ``` diff --git a/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql b/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql new file mode 100644 index 0000000000000..f213a7dfb1845 --- /dev/null +++ b/solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql @@ -0,0 +1,23 @@ +# Write your MySQL query statement below +WITH + s AS ( + SELECT + customer_id, + date_sub( + transaction_date, + INTERVAL row_number() OVER ( + PARTITION BY customer_id + ORDER BY transaction_date + ) DAY + ) AS transaction_date + FROM Transactions + ), + t AS ( + SELECT customer_id, transaction_date, count(1) AS cnt + FROM s + GROUP BY 1, 2 + ) +SELECT customer_id +FROM t +WHERE cnt = (SELECT max(cnt) FROM t) +ORDER BY customer_id;