-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2_count_and_revenue_of_orders.sql
75 lines (72 loc) · 1.33 KB
/
m2_count_and_revenue_of_orders.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Count and revenue of orders by: ALL customers
(optionally specify date range)
*/
SELECT
count(*) as total_orders,
SUM(so.`total_paid`) as revenue
FROM
`sales_order` so
WHERE
-- so.`created_at` >= '2020-01-01'
-- AND
-- so.`created_at` <= '2020-12-31'
-- AND
so.`status` != 'closed'
;
/*
Count and revenue of orders by: customers
(optionally specify date range)
*/
SELECT
count(*) as total_orders,
SUM(so.`total_paid`) as revenue
FROM
`sales_order` so
WHERE
-- so.`created_at` >= '2020-01-01'
-- AND
-- so.`created_at` <= '2020-12-31'
-- AND
so.`customer_id` IS NOT NULL
AND
so.`status` != 'closed'
;
/*
Count and revenue of orders by: non-customers
(optionally specify date range)
*/
SELECT
count(*) as total_orders,
SUM(so.`total_paid`) as revenue
FROM
`sales_order` so
WHERE
-- so.`created_at` >= '2020-01-01'
-- AND
-- so.`created_at` <= '2020-12-31'
-- AND
so.`customer_id` IS NULL
AND
so.`status` != 'closed'
;
/*
Count and revenue of orders by: returning customers (more than 1 order)
(optionally specify date range)
*/
SELECT
count(*) as total_orders,
SUM(so.`total_paid`) as revenue
FROM
`sales_order` so
WHERE
-- so.`created_at` >= '2020-01-01'
-- AND
-- so.`created_at` <= '2020-12-31'
-- AND
so.`customer_id` IS NOT NULL
AND
so.`status` != 'closed'
AND
(SELECT count(*) FROM `sales_order` so2 WHERE so2.`customer_id` = so.`customer_id`) > 1
;