从数据库获取每日销售额

时间:2014-04-17 19:34:12

标签: mysql sql

在这个伟大的社区的帮助下,我得到了这个解决方案,将每日销售时间间隔为2小时。

SELECT   SUBSTRING(DATE(o.date_purchased) + 
         INTERVAL (HOUR(o.date_purchased) -
         HOUR(o.date_purchased) MOD 2) HOUR , 12) AS time_purchased,
         ROUND(SUM(ot.value), 2) AS sales_today,
         COUNT(*) AS orders_today
FROM     orders AS o
JOIN     orders_total AS ot ON ot.orders_id = o.orders_id
WHERE    ot.class = 'ot_total'
AND      o.date_purchased >= CURRENT_DATE()
AND      o.date_purchased <  CURRENT_DATE() + INTERVAL 1 DAY
GROUP BY DATE(o.date_purchased) +
         INTERVAL (HOUR(o.date_purchased) -
         HOUR(o.date_purchased) MOD 2) HOUR

如何更改此查询以获取每月每日的每日销售额。从第一天开始自动开始,到实际月份的最后一天结束?

1 个答案:

答案 0 :(得分:0)

要获得当月的每日销售额,您可以将查询编写为

SELECT   DATE(o.date_purchased)  AS date_purchased,
         ROUND(SUM(ot.value), 2) AS sales_per_day,
         COUNT(*) AS orders_today
FROM     orders AS o
JOIN     orders_total AS ot ON ot.orders_id = o.orders_id
WHERE    ot.class = 'ot_total'
AND      o.date_purchased >= LAST_DAY(NOW() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
AND      o.date_purchased <= LAST_DAY(NOW())
GROUP BY DATE(o.date_purchased)