明确查找新订单和重复订单的编号(计数)和价格

时间:2016-01-05 09:13:07

标签: mysql sql

我的订单表的架构如下所示:

Orders(Order_ID varchar, Order_NO int, User_ID varchar, Price float, Payment_Status int, Paid_At timestamp)

付款完成时将付款状态视为1,不付款时将其视为零。

预期结果:

Date         New Orders   Repeat Orders  Total of New  Total of Repeat
2015-09-01      4               9              500        1600
2015-09-02      3               5              311         222

我为新订单尝试了这个查询,但它只提供了只下一个订单的客户的订单

SELECT date(paid_at) , COUNT(order_no) AS Total_Orders , SUM(price) AS daily_total FROM 
(SELECT * FROM jobs AS D WHERE user_id IN 
    ( SELECT user_id FROM( 
        (SELECT user_id,count(order_no) FROM Orders AS B WHERE user_id IN 
            (SELECT user_id FROM Orders AS A WHERE payment_status = '1' 
                ) GROUP BY user_id HAVING COUNT(order_no) = 1 
            ) AS C
        )
    )   
) 
AS E GROUP BY date(paid_at) 

正如您可能已经观察到我是SQL的新手,这里的一些帮助将帮助我改进。

0 个答案:

没有答案