Skip to content

Instantly share code, notes, and snippets.

@AcousticRand
Created March 14, 2017 22:53
Show Gist options
  • Save AcousticRand/5b78f28fa2deae36e1bafdf721c1a288 to your computer and use it in GitHub Desktop.
Save AcousticRand/5b78f28fa2deae36e1bafdf721c1a288 to your computer and use it in GitHub Desktop.
Calculate order velocity in any order database (orders per second, orders per minute, orders per hour, orders per day)
SET @end_time = utc_timestamp(), @start_time = '2017-03-14 15:00:00'; -- specific start time
-- SET @end_time = utc_timestamp(), @start_time = date_sub(@end_time, INTERVAL '02:30' HOUR_MINUTE); -- interval start time last 2.5 hours
SELECT
COUNT(id) AS OrdersPer
-- , DATE_FORMAT(CONVERT_TZ(OrderDate, 'UTC', 'EST'), "%Y-%m-%d %H:%i:%s") AS time_buckets -- Use this one for ORDERS PER SECOND
, DATE_FORMAT(CONVERT_TZ(OrderDate, 'UTC', 'EST'), "%Y-%m-%d %H:%i") AS time_buckets -- Use this one for ORDERS PER MINUTE
-- , DATE_FORMAT(CONVERT_TZ(OrderDate, 'UTC', 'EST'), "%Y-%m-%d %H") AS time_buckets -- Use this one for ORDERS PER HOUR
-- , DATE_FORMAT(CONVERT_TZ(OrderDate, 'UTC', 'EST'), "%Y-%m-%d") AS time_buckets -- Use this one for ORDERS PER DAY
-- , now() -- if you would like to see the timezone of now in the DB server's TZ (likely UTC) for comparison
FROM order_header
WHERE OrderDate BETWEEN @start_time AND @end_time -- (or use between OR an absolute time)
GROUP BY time_buckets -- WITH ROLLUP; -- WANT TOTALS?
ORDER BY OrdersPer DESC -- by highest OrdersPer
-- LIMIT 40; -- only show me top 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment