Top
10
Time range
Last 7 Days
-- It is a particularly resource-intensive analytical query.
-- The events table generates an average of 1.8 million records per day.
WITH top_sellers AS (
SELECT
product_id,
SUM(price) AS sold
FROM events
WHERE event_type = 'purchase'
AND event_time >= '${start_time}'
AND event_time <= '${end_time}'
GROUP BY product_id
ORDER BY sold DESC
LIMIT ${top_n}
)
SELECT
products.product_id,
products.product_name,
top_sellers.sold
FROM top_sellers
JOIN products ON top_sellers.product_id = products.product_id
ORDER BY sold DESC
MySQL RDS
your results will display here
TiDB Cloud Serverless
your results will display here
Copyright © PingCAP 2024.