-
Notifications
You must be signed in to change notification settings - Fork 2
/
olist_sql.sql
1390 lines (1062 loc) · 41.1 KB
/
olist_sql.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CREATE TABLE order_items (
order_id varchar NOT NULL,
order_item_id varchar (50),
product_id varchar (50),
seller_id varchar (50),
shipping_limit_date timestamp,
price float,
freight_value float)
SELECT * FROM order_items;
COPY order_items (order_id, order_item_id, product_id, seller_id, shipping_limit_date, price,freight_value)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_order_items_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM order_items;
CREATE TABLE order_payments (
order_id varchar(50),
payment_sequential int,
payment_type varchar (50),
payment_installments int,
payment_value float)
SELECT * FROM order_payments
COPY order_payments (order_id, payment_sequential, payment_type, payment_installments, payment_value)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_order_payments_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM order_payments;
CREATE TABLE customers (
customer_id varchar (50) NOT NULL PRIMARY KEY,
customer_unique_id varchar(50),
customer_zip_code int,
customer_city varchar (50),
customer_state varchar (50)
)
SELECT * FROM customers;
COPY customers (customer_id, customer_unique_id, customer_zip_code, customer_city, customer_state)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_customers_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM customers;
DROP TABLE geolocation
CREATE TABLE geolocation
(geolocation_zip_code_prefix int,
geolocation_lat float,
geolocation_Ing float,
geolocation_city varchar (50),
geolocation_state varchar (10)
)
SELECT * FROM geolocation;
COPY geolocation (geolocation_zip_code_prefix, geolocation_lat, geolocation_Ing, geolocation_city, geolocation_state)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_geolocation_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM geolocation;
CREATE TABLE reviews
(review_id varchar (50) NOT NULL,
order_id varchar (50),
review_score int,
review_comment_title varchar (50),
review_comment_message varchar (500),
review_creation_date timestamp,
review_answer_timestamp timestamp)
SELECT * FROM reviews;
COPY reviews (review_id, order_id, review_score, review_comment_title, review_comment_message, review_creation_date, review_answer_timestamp)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_order_reviews_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM reviews;
CREATE TABLE orders (
order_id VARCHAR(100) PRIMARY KEY,
customer_id VARCHAR(100),
order_status VARCHAR(50),
order_purchase_timestamp TIMESTAMP,
order_approved_at TIMESTAMP,
order_delivered_carrier_date TIMESTAMP,
order_delivered_customer_date TIMESTAMP,
order_estimated_delivery_date TIMESTAMP
)
SELECT * FROM orders;
COPY orders (order_id, customer_id, order_status, order_purchase_timestamp, order_approved_at, order_delivered_carrier_date, order_delivered_customer_date,order_estimated_delivery_date)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_orders_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM orders;
CREATE TABLE products (
product_id varchar(50) NOT NULL PRIMARY KEY,
product_category_name VARCHAR(70),
product_name_length int ,
product_description_length int,
product_photos_qty int,
product_weight_g int ,
product_length_cm int ,
product_height_cm int ,
product_width_cm int )
SELECT * FROM products;
COPY products (product_id, product_category_name, product_name_length, product_description_length, product_photos_qty, product_weight_g, product_length_cm, product_height_cm, product_width_cm)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_products_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM products;
CREATE TABLE sellers
(seller_id varchar(50) NOT NULL PRIMARY KEY,
seller_zip_code_prefix int,
seller_city varchar (100) ,
seller_state varchar(15)
)
SELECT * FROM sellers
COPY sellers (seller_id, seller_zip_code_prefix, seller_city, seller_state)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\olist_sellers_dataset.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM sellers
CREATE TABLE product_category_name_translation (
product_category_name varchar(50),
product_category_name_english varchar(70)
)
SELECT * FROM product_category_name_translation
COPY product_category_name_translation (product_category_name, product_category_name_english)
FROM 'C:\Program Files\PostgreSQL\11\data\olist\product_category_name_translation.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM product_category_name_translation
/*
Generate date for fi
*/
-- first_6_months
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id
-- Master Joint, only missing geolocation
WITH seven_table_merge AS (
SELECT *
FROM product_category_name_translation
INNER JOIN products USING(product_category_name)
INNER JOIN order_items USING(product_id)
INNER JOIN reviews USING(order_id)
INNER JOIN orders o USING (order_id)
INNER JOIN order_payments USING(order_id)
INNER JOIN orders o2 USING(order_id)
INNER JOIN customers C ON C.customer_id = o.customer_id),
first_6_months AS (
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order,
COUNT(order_id) AS order_count
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id)
SELECT *
FROM sellers
INNER JOIN seven_table_merge USING(seller_id)
INNER JOIN first_6_months USING(customer_unique_id);
--
SELECT DISTINCT order_status FROM orders;
/*
order_status
--------------
shipped
unavailable
invoiced
created
approved
processing
delivered
canceled
(8 rows)
*/
/*
The following queries check if there if the city and state fields in the customer records directly match the ones based off zip code in the geolocation.
They do not match.
So, both records will be kept for the Python upload.
However, any correlation analysis between sellers and customers will be based off the generated fields
*/
SELECT COUNT(*) FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix WHERE c.customer_city <> g.geolocation_city;
/*
count
--------
954042
(1 row)
*/
SELECT COUNT(*) FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix WHERE c.customer_city = g.geolocation_city;
/*
count
----------
14129413
(1 row)
*/
olist=# SELECT COUNT(*) FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix WHERE c.customer_state <>g.geolocation_state;
/* count
-------
74
(1 row)
*/
olist=# SELECT COUNT(*) FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix WHERE c.customer_state = g.geolocation_state;
/* count
----------
15083381
(1 row)
*/
-- To confirm than an order count based on the orders table is accurate (no dupes)
olist=# SELECT COUNT(Order_id) FROM orders;
/* count
-------
99441
(1 row)
*/
olist=# SELECT COUNT(DISTINCT order_id) FROM orders;
/* count
-------
99441
(1 row)
*/
-- Basic Info
-- Total Customers = 96096
olist=# SELECT COUNT(DISTINCT customer_unique_id) FROM customers INNER JOIN orders USING(customer_id);
/*
count
-------
96096
(1 row)
*/
* Total Orders = 99441
olist=# SELECT COUNT(DISTINCT order_id) FROM orders;
/*
count
-------
99441
(1 row)
*/
-- Total Sellers = 3095
olist=# SELECT COUNT(DISTINCT seller_id) FROM sellers INNER JOIN order_items USING(seller_id);
/* count
-------
3095
(1 row)
*/
-- First 6 Months
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id)
SELECT *
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp < six_months_from_first_order;
-- First 6 month grouped by customer
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id),
first_6_months_activity AS (
SELECT *
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp < six_months_from_first_order)
SELECT
customer_unique_id,
MIN(order_purchase_timestamp) AS date_first_order,
EXTRACT(YEAR FROM MIN(order_purchase_timestamp)) AS year_first_order,
EXTRACT(MONTH FROM MIN(order_purchase_timestamp)) AS month_first_order,
COUNT(DISTINCT order_id) AS total_orders_first_6_months,
SUM(payment_value) AS total_paid_first_6_months
FROM first_6_months_activity
GROUP BY customer_unique_id
ORDER BY customer_unique_id;
-- What value should I use for revenue? Does calculated total match payment value columns
olist=# SELECT SUM(price) FROM orders INNER JOIN order_items USING(order_id) INNER JOIN products USING(product_id);
/* sum
------------------
13591643.7000074
(1 row)
*/
olist=# SELECT SUM(freight_value) FROM orders INNER JOIN order_items USING(order_id) INNER JOIN products USING(product_id);
/* sum
------------------
2251909.53999995
(1 row)
*/
olist=# SELECT SUM(payment_value) FROM order_payments;
/*
sum
------------------
16008872.1199988
(1 row)
*/
-- Do Customer that make one order in 6 months, end up making orders later or remain inactive?
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id),
first_6_months_activity AS (
SELECT *
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp < six_months_from_first_order),
totals_by_customer AS (
SELECT *
FROM customers
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
)
SELECT
f.customer_unique_id,
MIN(f.order_purchase_timestamp) AS date_first_order,
COUNT(DISTINCT t.order_id) AS total_orders,
COUNT(DISTINCT f.order_id) AS total_orders_first_6_months,
COUNT(DISTINCT t.order_id) - COUNT(DISTINCT f.order_id) AS Count_difference,
SUM(t.payment_value) AS total_paid,
SUM(f.payment_value) AS total_paid_first_6_months,
SUM(t.payment_value) - SUM(f.payment_value) AS Rev_Difference
FROM first_6_months_activity f INNER JOIN totals_by_customer t USING(customer_unique_id)
GROUP BY f.customer_unique_id
HAVING COUNT(DISTINCT f.order_id) = 1 AND (COUNT(DISTINCT t.order_id) - COUNT(DISTINCT f.order_id)) <> 0
ORDER BY Count_difference DESC;
-- COUNT the number of customers who make 1 order in the first 6 months and LATER make another order.
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id),
first_6_months_activity AS (
SELECT *
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp < six_months_from_first_order),
totals_by_customer AS (
SELECT *
FROM customers
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
),
reworked_customer_aggregates AS (
SELECT
f.customer_unique_id,
MIN(f.order_purchase_timestamp) AS date_first_order,
COUNT(DISTINCT t.order_id) AS total_orders,
COUNT(DISTINCT f.order_id) AS total_orders_first_6_months,
COUNT(DISTINCT t.order_id) - COUNT(DISTINCT f.order_id) AS Count_difference,
SUM(t.payment_value) AS total_paid,
SUM(f.payment_value) AS total_paid_first_6_months,
SUM(t.payment_value) - SUM(f.payment_value) AS Rev_Difference
FROM first_6_months_activity f INNER JOIN totals_by_customer t USING(customer_unique_id)
GROUP BY f.customer_unique_id
HAVING COUNT(DISTINCT f.order_id) = 1 AND (COUNT(DISTINCT t.order_id) - COUNT(DISTINCT f.order_id)) <> 0
ORDER BY Count_difference DESC)
SELECT COUNT (*)
FROM reworked_customer_aggregates;
/*
count
-------
502
(1 row)
*/
SELECT COUNT(DISTINCT customer_unique_id) FROM customers INNER JOIN orders USING(customer_id);
/*
count
-------
96096
(1 row)
*/
-- Only 502 FROM 96,096 total customers made 1 order within 6 months and later made another. Only 0.5%. Not sigficant.
-- Min/Max Dates = First and last orders in dataset
SELECT MAX(order_purchase_timestamp) FROM orders;
/*
max
---------------------
2018-10-17 17:30:18
(1 row)
*/
SELECT MIN(order_purchase_timestamp) FROM orders;
/* min
---------------------
2016-09-04 21:15:19
(1 row)
*/
-- How many customers have a first order within the last 6 months?
WITH brand_new_customers AS(
SELECT *, (SELECT MAX(order_purchase_timestamp) FROM orders) - interval '182.5 day' AS Last_6_months_in_data
FROM customers
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp > ((SELECT MAX(order_purchase_timestamp) FROM orders) - interval '182.5 day')
)
SELECT COUNT (DISTINCT customer_unique_id) FROM brand_new_customers;
/*
-------
28427
(1 row)
*/
WITH min_date AS
(SELECT customer_unique_id, MIN(order_purchase_timestamp)
FROM customers
INNER JOIN orders USING(customer_id)
GROUP BY customer_unique_id)
SELECT COUNT(DISTINCT m.customer_unique_id)
FROM min_date m
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE min > CAST('2018-04-17' AS timestamp);
/*
count
-------
28112
(1 row)
*/
-- Are 30% of customers brand new! Should they be dropped?
-- How many orders are cancelled? Should these be removed?
olist=# SELECT DISTINCT order_status FROM orders;
/*
order_status
--------------
shipped
unavailable
invoiced
created
approved
processing
delivered
canceled
(8 rows)
*/
olist=# SELECT COUNT(DISTINCT order_id) FROM orders WHERE order_status = 'canceled';
/*
count
-------
625
(1 row)
*/
olist=# SELECT COUNT(DISTINCT order_id) FROM orders;
/*
count
-------
99441
(1 row)
*/
-- Only 0.06% of orders are canceled.
-- But, are they adding to the payments total? YES
SELECT SUM(payment_value) FROM orders INNER JOIN order_payments USING(order_id) WHERE order_status = 'canceled';
/* sum
----------
143255.6
(1 row)
*/
-- Because they add to payment totals, these will be removed
-- Create base df (df01) with basic customer aggregates (excludes orders cancelled and not within first 6 months)
CREATE VIEW df01 AS
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id),
first_6_months_activity AS (
SELECT *
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp < six_months_from_first_order AND order_status <> 'canceled')
SELECT
customer_unique_id,
MIN(order_purchase_timestamp) AS date_first_order,
EXTRACT(YEAR FROM MIN(order_purchase_timestamp)) AS year_first_order,
EXTRACT(MONTH FROM MIN(order_purchase_timestamp)) AS month_first_order,
COUNT(DISTINCT order_id) AS total_orders_first_6_months,
SUM(payment_value) AS total_paid_first_6_months
FROM first_6_months_activity
GROUP BY customer_unique_id
ORDER BY customer_unique_id;
-- Start creating pieces to add to df. Now customer demographic
SELECT DISTINCT customer_unique_id, customer_zip_code, geolocation_city AS customer_geo_city, geolocation_state AS customer_geo_state
FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix
ORDER BY customer_unique_id;
\COPY (SELECT DISTINCT customer_unique_id, customer_zip_code, geolocation_city AS customer_geo_city, geolocation_state AS customer_geo_state FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix) TO 'C:/Users/rache/DATA/Olist/GITHUB_OLIST/customer_demographics.csv' CSV HEADER;
SELECT COUNT(DISTINCT customer_id)
FROM customers c INNER JOIN geolocation g ON c.customer_zip_code = g.geolocation_zip_code_prefix;
-- Does the count matches the rows in the Python upload?
-- No, is zip code data in the geolocation table distinct? - NO
SELECT COUNT(DISTINCT geolocation_zip_code_prefix) FROM geolocation;
/*
count
-------
19015
(1 row)
*/
SELECT COUNT(geolocation_state) FROM geolocation;
/*
count
---------
1000163
(1 row)
*/
SELECT COUNT(DISTINCT geolocation_state) FROM geolocation;
/*
count
-------
27
(1 row)
*/
-- The name of the city variates BUT the state doesn't so we will only add state data.
-- Now, add geolocation
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id),
first_6_months_activity AS
(
SELECT *
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
INNER JOIN order_payments USING(order_id)
WHERE order_purchase_timestamp < six_months_from_first_order AND order_status <> 'canceled'),
df1 AS
(
SELECT
customer_unique_id,
customer_zip_code,
MIN(order_purchase_timestamp) AS date_first_order,
EXTRACT(YEAR FROM MIN(order_purchase_timestamp)) AS year_first_order,
EXTRACT(MONTH FROM MIN(order_purchase_timestamp)) AS month_first_order,
COUNT(DISTINCT order_id) AS total_orders_first_6_months,
SUM(payment_value) AS total_paid_first_6_months
FROM first_6_months_activity
GROUP BY customer_unique_id, customer_zip_code
ORDER BY customer_unique_id)
SELECT df1.customer_unique_id, df1.customer_zip_code AS customer_geo_zip, g.geolocation_state AS customer_geo_state, g.geolocation_lat AS customer_geo_lat, g.geolocation_ing AS customer_geo_lng
FROM df1 INNER JOIN (SELECT DISTINCT geolocation_state, geolocation_zip_code_prefix, geolocation_lat, geolocation_ing FROM geolocation) AS g
ON df1.customer_zip_code = g.geolocation_zip_code_prefix;
-- clean geo table with unique zip_code
CREATE VIEW geo_clean AS
SELECT
DISTINCT ON
(geolocation_zip_code_prefix) geolocation_zip_code_prefix,
geolocation_state,
geolocation_lat,
geolocation_ing
FROM geolocation;
/*
-- add customer geo location
CREATE VIEW ds3 AS
WITH customer_plus_geo AS(
SELECT DISTINCT c.customer_unique_id,
gc.geolocation_zip_code_prefix AS customer_geo_zip,
gc.geolocation_state AS customer_geo_state,
gc.geolocation_lat AS customer_geo_lat,
gc.geolocation_ing AS customer_geo_lng
FROM customers c LEFT JOIN geo_clean gc ON c.customer_zip_code = gc.geolocation_zip_code_prefix)
SELECT COUNT(customer_unique_id) FROM customer_plus_geo
SELECT DISTINCT * FROM df2 LEFT JOIN customer_plus_geo USING(customer_unique_id);
-- list - orders first 6 months
CREATE VIEW orders_6m AS
WITH first_6_months_dates AS(
SELECT c.customer_unique_id,
MIN(o.order_purchase_timestamp) as first_order_date,
MIN(o.order_purchase_timestamp) + interval '182.5 day' AS six_months_from_first_order
FROM Orders o INNER JOIN Customers c USING(customer_id)
GROUP BY c.customer_unique_id
ORDER BY c.customer_unique_id)
SELECT DISTINCT order_id
FROM first_6_months_dates
INNER JOIN customers USING(customer_unique_id)
INNER JOIN orders USING(customer_id)
WHERE order_purchase_timestamp < six_months_from_first_order AND order_status <> 'canceled';
*/
-- add number of orders unavailable
CREATE VIEW df02 AS
WITH orders_status_bol_total AS(
SELECT customer_unique_id, order_id, order_status, CASE WHEN order_status = 'unavailable' THEN TRUE ELSE NULL END AS order_unavailable_bol
FROM customers INNER JOIN orders USING(customer_id)
),
customers_unavailable_count AS (
SELECT customer_unique_id, COUNT(order_unavailable_bol) AS order_count_unavailable
FROM orders_status_bol_total INNER JOIN orders_6m USING(order_id)
GROUP BY customer_unique_id
ORDER BY 2)
SELECT * FROM df01 LEFT JOIN customers_unavailable_count USING(customer_unique_id);
-- add payment approval time
CREATE VIEW df03 AS
WITH orders_plus_payment_approval_wait AS(
SELECT C.customer_unique_id, O.order_id, O.order_purchase_timestamp, o.order_approved_at, (o.order_approved_at - o.order_purchase_timestamp) as wait_for_payment_approval
FROM orders o INNER JOIN customers c USING(customer_id)
),
customers_plus_payment_approval_wait AS (
SELECT customer_unique_id, AVG(wait_for_payment_approval) AS avg_payment_processing_time
FROM orders_plus_payment_approval_wait INNER JOIN orders_6m USING(order_id)
WHERE wait_for_payment_approval >= '0 microsecond'
GROUP BY customer_unique_id)
SELECT * FROM df02 LEFT JOIN customers_plus_payment_approval_wait USING(customer_unique_id);
-- now, add lag between order date and seller delivery to carrier (avg_seller processing time)
CREATE VIEW df04 AS
WITH orders_plus_seller_processing_time AS(
SELECT C.customer_unique_id, O.order_id, O.order_purchase_timestamp, o.order_approved_at, (o.order_delivered_carrier_date- o.order_purchase_timestamp) as seller_processing_time
FROM orders_6m o6 INNER JOIN orders o USING(order_id) INNER JOIN customers c USING(customer_id)
),
customers_plus_seller_processing_time AS (
SELECT customer_unique_id, AVG(seller_processing_time) AS avg_seller_processing_time
FROM orders_plus_seller_processing_time
WHERE seller_processing_time >= '0 microsecond'
GROUP BY customer_unique_id)
SELECT * FROM df03 LEFT JOIN customers_plus_seller_processing_time USING(customer_unique_id);
-- add transit_time
CREATE VIEW df05 AS
WITH orders_plus_transit_time AS(
SELECT C.customer_unique_id, O.order_id, (o.order_delivered_customer_date-o.order_delivered_carrier_date) as transit_time
FROM orders_6m o6 INNER JOIN orders o USING(order_id) INNER JOIN customers c USING(customer_id)
),
customers_plus_transit_time AS (
SELECT customer_unique_id, AVG(transit_time) AS avg_transit_time
FROM orders_plus_transit_time
WHERE transit_time >= '0 microsecond'
GROUP BY customer_unique_id)
SELECT * FROM df04 LEFT JOIN customers_plus_transit_time USING(customer_unique_id);
-- add order_lead_time for customer
CREATE VIEW df06 AS
WITH orders_plus_lead_time AS(
SELECT C.customer_unique_id, O.order_id, (o.order_delivered_customer_date-o.order_purchase_timestamp) as lead_time
FROM orders_6m o6 INNER JOIN orders o USING(order_id) INNER JOIN customers c USING(customer_id)
),
customers_plus_avg_lead_time AS (
SELECT customer_unique_id, AVG(lead_time) AS avg_lead_time
FROM orders_plus_lead_time
WHERE lead_time >= '0 microsecond'
GROUP BY customer_unique_id)
SELECT * FROM df05 LEFT JOIN customers_plus_avg_lead_time USING(customer_unique_id);
-- add number of items per order
CREATE VIEW df07 AS
WITH items_per_order AS (
SELECT customer_unique_id, order_id, COUNT(order_item_id) AS item_count
FROM customers INNER JOIN orders USING(customer_id) INNER JOIN order_items USING(order_id)
GROUP BY customer_unique_id, order_id),
avg_item_count_per_order AS (
SELECT customer_unique_id, AVG(item_count) AS avg_item_count_per_order
FROM items_per_order INNER JOIN orders_6m USING(order_id)
GROUP BY customer_unique_id)
SELECT * FROM df06 LEFT JOIN avg_item_count_per_order USING (customer_unique_id);
-- add number of products per order (distinct product)
CREATE VIEW df08 AS
WITH product_count_per_order AS (
SELECT customer_unique_id, order_id, COUNT(DISTINCT product_id) AS product_count
FROM customers INNER JOIN orders USING(customer_id) INNER JOIN order_items USING(order_id)
GROUP BY customer_unique_id, order_id),
avg_product_count_per_order AS (
SELECT customer_unique_id, AVG(product_count) AS avg_product_count_per_order
FROM product_count_per_order INNER JOIN orders_6m USING(order_id)
GROUP BY customer_unique_id)
SELECT * FROM df07 LEFT JOIN avg_product_count_per_order USING (customer_unique_id);
-- change layoyt
\x on
-- Do customers missing their shipping deadline correlate with LTV?
CREATE VIEW df09 AS
WITH orders_shipped_past_deadline AS (
SELECT *
FROM customers INNER JOIN orders USING(customer_id) INNER JOIN order_items USING(order_id)
WHERE shipping_limit_date > order_delivered_carrier_date),
order_shipped_late AS(
SELECT customer_unique_id, COUNT(DISTINCT order_id) as orders_shipped_late
FROM orders_shipped_past_deadline INNER JOIN orders_6m USING(order_id)
GROUP BY customer_unique_id)
SELECT * FROM df08 LEFT JOIN order_shipped_late USING(customer_unique_id);
-- number of items grouped by product, and order number
CREATE VIEW df010 AS
WITH units_per_product_by_customer AS(
SELECT customer_unique_id, product_id, COUNT(product_id) AS units_per_product
FROM customers INNER JOIN orders USING(customer_id) INNER JOIN order_items USING(order_id) INNER JOIN orders_6m USING(order_id)
GROUP BY customer_unique_id, product_id),
avg_quantity_by_product AS (
SELECT customer_unique_id, AVG(units_per_product) AS avg_quantity_by_product
FROM units_per_product_by_customer
GROUP BY customer_unique_id)
SELECT * FROM df09 LEFT JOIN avg_quantity_by_product USING(customer_unique_id);
-- average price per product
CREATE VIEW df011 AS
WITH price_per_unit_by_customer AS
(
SELECT customer_unique_id, AVG(price) AS average_price_per_unit
FROM customers INNER JOIN orders USING(customer_id) INNER JOIN order_items USING(order_id) INNER JOIN orders_6m USING(order_id)
GROUP BY customer_unique_id
)
SELECT * FROM df010 LEFT JOIN price_per_unit_by_customer USING(customer_unique_id);
-- average freight cost per order, freight value column is grouped by products within an order.
-- If order has 2 units of product X and each have freight cost Y. Y is the freight price for the 2 units of the same product. Gets repeated in each line, not split.
CREATE VIEW df012 AS
WITH freight_costs_corrected AS (
SELECT DISTINCT customer_unique_id, order_id, product_id, freight_value
FROM customers INNER JOIN orders USING(customer_id) INNER JOIN order_items USING(order_id) INNER JOIN orders_6m USING(order_id)
),
customer_order_aggregates AS (
SELECT customer_unique_id, order_id, SUM(freight_value) AS order_freight_cost
FROM freight_costs_corrected
GROUP BY customer_unique_id, order_id),
customer_aggregates AS (
SELECT customer_unique_id, AVG(order_freight_cost) AS avg_freight_cost_per_order
FROM customer_order_aggregates
GROUP BY customer_unique_id)
SELECT * FROM df011 LEFT JOIN customer_aggregates USING (customer_unique_id);
-- NOTE - How many orders were left out of analysis as they fall outside of first 6 months?
-- Less than 2%. OK. 98.76% of orders provided fall in customer's first 6 months.
olist=# SELECT COUNT(DISTINCT order_id) FROM orders_6m;
/*
count
-------
98207
(1 row)
*/
olist=# SELECT COUNT(DISTINCT order_id) FROM orders;
/*
count
-------
99441
(1 row)
*/
-- how many product categories were purchased from? - 71
SELECT COUNT(DISTINCT product_category_name_english)
FROM product_category_name_translation
INNER JOIN products USING(product_category_name)
INNER JOIN order_items USING(product_id)
INNER JOIN orders_6m USING(order_id);\
-- Total order_count
/*
order_count_total
-------------------
98207
(1 row)
*/
-- order_count_by_category
-- SO far, in Excel did proportion of order_count to total_orders
-- The top 7 categories were were present in over 50% of the orders (51%) with bed_bad_table is almost 10% of orders.
SELECT product_category_name_english, COUNT(DISTINCT order_id) AS order_count_by_category
FROM product_category_name_translation
INNER JOIN products USING(product_category_name)
INNER JOIN order_items USING(product_id)
INNER JOIN orders_6m USING(order_id)
INNER JOIN order_payments USING(order_id)
GROUP BY product_category_name_english
ORDER BY 2 DESC
-- NEXT: Boolean, did customer purchase from top 8 categories
-- PROBLEM - How to make it even across customers. Will customers that have 1 order be less likely to have diversity.
-- How to boil down info at customer level.
-- number of customers grouped by category
SELECT product_category_name_english, COUNT(DISTINCT customer_unique_id) AS customer_count_by_category
FROM product_category_name_translation
INNER JOIN products USING(product_category_name)
INNER JOIN order_items USING(product_id)
INNER JOIN orders USING(order_id)
INNER JOIN customers USING(customer_id)
WHERE order_id IN (SELECT * FROM orders_6m)
GROUP BY product_category_name_english
ORDER BY 2 DESC
-- total customers
olist=# select count(distinct customer_unique_id) from customers inner join orders using(customer_id) inner join orders_6m using(order_id);
/*
count