如何从运行中的总销售额中检索每日销售额

时间:2018-06-23 10:05:49

标签: mysql sql

我需要帮助。我有如下的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

谢谢

2 个答案:

答案 0 :(得分:0)

您需要按日期强制转换,将product_id和cat_id分组,并根据我认为的趋势一词按订单总数进行降序排列:

[temp.arg.type]

SQL Fiddle Demo

答案 1 :(得分:0)

我解决了这个问题。谢谢

select a.product_id,a.cox-b.az sales from (
SELECT product_id,
        max(orders) AS cox
    FROM
        products
    WHERE
        date > ( CURDATE() - INTERVAL 3 DAY ) group by product_id order by cox desc) a,

(SELECT product_id,
        orders AS az
    FROM
        products
    WHERE
        date > ( CURDATE() - INTERVAL 3 DAY ) group by product_id) b 


       where a.product_id=b.product_id 
        group by a.product_id order by sales desc