-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path# 1565. Unique Orders and Customers Per Month.sql
82 lines (52 loc) · 1.56 KB
/
# 1565. Unique Orders and Customers Per Month.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
# 1565. Unique Orders and Customers Per Month
# find the number of unique orders and the number of unique customers with invoices > $20 for each different month
SELECT DATE_FORMAT(order_date, "%Y-%m") AS month,
COUNT(DISTINCT order_id) AS order_count,
COUNT(DISTINCT customer_id) AS customer_count
FROM Orders
WHERE invoice > 20
GROUP BY DATE_FORMAT(order_date, "%Y-%m")
;
select
SUBSTRING(order_date, 1, 7) as month,
count(distinct order_id) as order_count,
count(distinct customer_id) as customer_count
from
Orders
where invoice > 20
group by
SUBSTRING(order_date, 1, 7)
SELECT left(order_date, 7) AS month,
COUNT(DISTINCT order_id) AS order_count,
COUNT(DISTINCT customer_id) AS customer_count
FROM Orders
WHERE invoice > 20
GROUP BY month
SELECT month, COUNT(order_id) as order_count, COUNT(DISTINCT customer_id) as customer_count
FROM
(
SELECT SUBSTRING(order_date, 1, 7) as month, order_id, customer_id, invoice
FROM Orders
WHERE invoice > 20
) t
GROUP BY month
SELECT LEFT(order_date, 7) month, COUNT(DISTINCT order_id) order_count,
COUNT(DISTINCT customer_id) customer_count
FROM orders
WHERE invoice > 20
GROUP BY 1
SELECT month,
COUNT(*) AS order_count,
COUNT(DISTINCT customer_id) AS customer_count
from
(select LEFT(order_date, 7) AS month,
customer_id
from Orders
where invoice > 20) AS tb1
group by month
SELECT DATE_FORMAT(order_date,'%Y-%m') AS month,
COUNT(order_id) AS order_count,
COUNT(DISTINCT customer_id) AS customer_count
FROM Orders
WHERE Invoice > 20
GROUP BY DATE_FORMAT(order_date,'%Y-%m')