[vox-tech] MySQL question: making stats out of orders
Chanoch (Ken) Bloom
kbloom at gmail.com
Tue Dec 1 22:37:19 PST 2009
On Tue, 2009-12-01 at 22:33 -0800, Bill Kendrick wrote:
> On Wed, Dec 02, 2009 at 12:20:25AM -0600, Chanoch (Ken) Bloom wrote:
> > INSERT into orderstats(productid, date, orders)
> > SELECT
> > orderitem.productid, orders.timestamp, SUM(orderitem.qty)
> > FROM orderitem
> > JOIN orders ON orders.id = orderitem.orderid
> > WHERE orders.timestamp >= { 90 days ago }
> > group by orderitem.productid, orders.timestamp
> >
> > assuming orders.timestamp has one-day granularity
>
> Timestamp is actually a timestamp (down the second).
Then you have to throw in a conversion function that truncates it to the
date in order for the `group by` to work correctly. I'm not sure what
function that is, but you can play around with the functions listed at
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
More information about the vox-tech
mailing list