我需要帮助。我有如下的MySQL表结构。
订单不是每日订单,而是总订单。日期是时间戳
我想查看最近3天按类别划分的销售趋势。我想找到哪个类别的产品趋势
id, product_id, cat_id, orders, date
5, 2345, 15, 120, 2018-06-18 00:00:00
6, 2345, 15, 123, 2018-06-19 00:00:00
1, 2345, 15, 137, 2018-06-22 00:00:00
2, 2345, 15, 140, 2018-06-23 00:00:00
7, 456, 55, 127, 2018-06-18 00:00:00
8, 456, 55, 136, 2018-06-19 00:00:00
3, 456, 55, 152, 2018-06-22 00:00:00
4, 456, 55, 176, 2018-06-23 00:00:00
谢谢
最佳答案
您需要将日期转换为日期,将product_id和cat_id分组,并根据我所假设的趋向词按订单总数降序排列:
select product_id, cat_id, count(orders) as total_orders
from mytable
where cast(date as date) >= cast(sysdate()-3 as date)
group by product_id, cat_id
order by count(orders) desc;
product_id cat_id total_orders
2345 15 1
456 55 1
SQL Fiddle Demo
关于mysql - 如何从运行中的总销售额中检索每日销售额,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51000063/