Skip to content

Instantly share code, notes, and snippets.

@lloiacono
Created August 16, 2016 20:54
Show Gist options
  • Save lloiacono/1e4a3c758ed5c3a7fd2879717501dde4 to your computer and use it in GitHub Desktop.
Save lloiacono/1e4a3c758ed5c3a7fd2879717501dde4 to your computer and use it in GitHub Desktop.
MySQL queries to calculate AVG and Amount of orders at current hour
#AVG orders at current hour
select AVG(orders_count) as avg_orders_per_hour FROM (
select count(entity_id) as orders_count
from sales_flat_order sfo
where
HOUR(created_at) >= HOUR(now())
AND
HOUR(created_at) <= HOUR(now())
group by DATE(created_at) order by NULL
) avg;
#Number of orders at current hour
select count(entity_id)
from sales_flat_order
where created_at >= DATE_FORMAT(NOW(), '%Y-%m-%d %H:00');
#TZ convertion if needed
select DATE_FORMAT(CONVERT_TZ(now(), @@session.time_zone, 'Europe/Berlin'), '%Y-%m-%d %H:00');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment