Get Started Free

14. Longest streak of order days Solved

AdvancedExpertgaps and islandsROW_NUMBERdate arithmetic

Find the longest run of consecutive calendar days on which at least one order was placed. Return one row: start_day, end_day and days. If two runs are equally long, return the earlier one.

Your answer returns 1 row with the columns start_day, end_day, days. Order matters.

The data

The store from the DuckDB playground, running in your browser with DuckDB. Prices are in EUR; the data covers 2024.

customers20 rows
idINTEGER
primary key
nameVARCHAR
countryVARCHAR
currencyVARCHAR
EUR, GBP, USD or JPY
signed_upDATE
tagsVARCHAR[]
a list: vip, newsletter, b2b, returns-often
products10 rows
idINTEGER
primary key
nameVARCHAR
categoryVARCHAR
priceDECIMAL(10,2)
EUR
specsSTRUCT
weight_g INTEGER, color VARCHAR, sizes VARCHAR[]
orders300 rows
idINTEGER
primary key
customer_idINTEGER
→ customers.id
placed_atTIMESTAMP
2024
statusENUM
placed, paid, shipped, delivered, returned
currencyVARCHAR
the customer's currency
order_items576 rows
order_idINTEGER
→ orders.id
product_idINTEGER
→ products.id
quantityINTEGER
unit_priceDECIMAL(10,2)
EUR
web_events20,000 rows
event_idBIGINT
tsTIMESTAMP
session_idVARCHAR
customer_idINTEGER
NULL for anonymous visitors
event_typeVARCHAR
page_view, search, add_to_cart, checkout
pageSTRUCT
path, referrer
deviceSTRUCT
browser, mobile BOOLEAN, screen STRUCT(w, h)
product_idsINTEGER[]
load_msINTEGER
fx_rates720 rows
currencyVARCHAR
USD, GBP, JPY
valid_fromTIMESTAMP
per_eurDOUBLE
units of currency per 1 EUR

Hints

Common traps
  • Several orders on the same day: deduplicate days first.

Solution

Run in the editor with Ctrl/⌘ + Enter. Check with Shift + Ctrl/⌘ + Enter.

"Check answer" runs your SQL on a fresh copy of the data, then on 2 hidden test datasets with more customers and orders, and compares each result with the expected one. The result of your last statement is your answer.

Starting…