-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholist.main.query.sql
More file actions
1309 lines (1036 loc) · 45 KB
/
Copy patholist.main.query.sql
File metadata and controls
1309 lines (1036 loc) · 45 KB
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
/** OLIST SORGUSU **/
-- VERİ KÜMESİNDE NULL VE YİNELENEN (DUPLICATE) DEĞERLERİ BULMA
-- 1) Olist veri setindeki her tabloda NULL ve duplicate değer kontrolü
-- Ai) CUSTOMERS TABLOSUNDA NULL DEĞERLER
SELECT 'customer_id' AS column_name, COUNT(*) AS number_of_nulls
FROM olist.olist_customers_dataset
WHERE customer_id IS NULL
UNION ALL
SELECT 'customer_unique_id', COUNT(*)
FROM olist.olist_customers_dataset
where customer_unique_id IS NULL
UNION ALL
SELECT 'customer_zip_code_prefix', COUNT(*)
FROM olist.olist_customers_dataset
WHERE customer_zip_code_prefix IS NULL
UNION ALL
SELECT 'customer_city', COUNT(*)
FROM olist.olist_customers_dataset
WHERE customer_city IS NULL
UNION ALL
SELECT 'customer_state', COUNT(*)
FROM olist.olist_customers_dataset
WHERE customer_state IS NULL;
-- Aii) CUSTOMERS TABLOSUNDA DUPLICATE (YİNELENEN) DEĞERLER
SELECT customer_id, customer_unique_id, customer_zip_code_prefix,
customer_city,
customer_state,
COUNT(*) AS number_of_duplicates
FROM olist.olist_customers_dataset
GROUP BY customer_id, customer_unique_id, customer_zip_code_prefix,
customer_city, customer_state
HAVING COUNT(*) > 1;
/** customers tablosunda duplicate ve null yok **/
-------------------------------------------------------------------------------------
-- Bi) GEOLOCATION TABLOSUNDA NULL DEĞERLER
SELECT 'geolocation_zip_code_prefix' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
where geolocation_zip_code_prefix is null
UNION ALL
SELECT 'geolocation_lat' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
where geolocation_lat is null
UNION ALL
SELECT 'geolocation_lng' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
where geolocation_lng is null
UNION ALL
SELECT 'geolocation_city' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
where geolocation_city is null
UNION ALL
SELECT 'geolocation_state' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
where geolocation_state is null;
-- Bii) GEOLOCATION TABLOSUNDA DUPLICATE DEĞERLER
SELECT geolocation_zip_code_prefix, geolocation_lat, geolocation_lng, geolocation_city, geolocation_state, COUNT(*) AS number_of_dublicates
from `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
group by geolocation_zip_code_prefix, geolocation_lat, geolocation_lng, geolocation_city, geolocation_state
having count(*)>1;
-- Çok sayıda duplicate olduğundan, şunları kaldırabiliriz
-- biii) geolocation duplicate’larını kaldırma ve bunu bir "CTE" (new_geolocation) olarak saklama
WITH new_geolocation AS
(
SELECT DISTINCT geolocation_zip_code_prefix, geolocation_lat,
geolocation_lng, geolocation_city,
geolocation_state
FROM `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
ORDER BY geolocation_zip_code_prefix
)
SELECT *
FROM new_geolocation;
-- Ei) SELLERS TABLOSUNDA NULL DEĞERLER
select 'seller_id' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_sellers_dataset`
where seller_id is null
union all
select 'seller_zip_code_prefix' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_sellers_dataset`
where seller_zip_code_prefix is null
union all
select 'seller_city' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_sellers_dataset`
where seller_city is null
union all
select 'seller_state' as column_name, count(*) as number_of_nulls
from `pacific-legend-474020-b4.olist.olist_sellers_dataset`
where seller_city is null;
-- Eii) SELLERS TABLOSUNDA DUPLICATE DEĞERLER
SELECT seller_id, seller_zip_code_prefix, seller_city, seller_state,
COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.olist_sellers_dataset`
GROUP BY seller_id, seller_zip_code_prefix, seller_city, seller_state
HAVING COUNT(*) > 1;
/** SELLERS TABLOSUNDA null veya duplicate yok **/
----------------------------------------------------------------------------------
-- Fi) PRODUCTS TABLOSUNDA NULL DEĞERLER
SELECT 'product_id' AS column_name, COUNT(*) AS number_of_nulls
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_id IS NULL
UNION ALL
SELECT 'product_category_name', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_category_name IS NULL
UNION ALL
SELECT 'product_name_lenght', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_name_lenght IS NULL
UNION ALL
SELECT 'product_description_lenght', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_description_lenght IS NULL
UNION ALL
SELECT 'product_photos_qty', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_photos_qty IS NULL
UNION ALL
SELECT 'product_weight_g', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_weight_g IS NULL
UNION ALL
SELECT 'product_length_cm', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_length_cm IS NULL
UNION ALL
SELECT 'product_height_cm', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_height_cm IS NULL
UNION ALL
SELECT 'product_width_cm', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
WHERE product_width_cm IS NULL
ORDER BY number_of_nulls DESC;
/** 32951 ürünün 610’unda kategori yok.*/
-- Fii) PRODUCTS TABLOSUNDA DUPLICATE DEĞERLER
SELECT product_id, product_category_name, product_name_lenght,
product_description_lenght, product_photos_qty,
product_weight_g, product_length_cm, product_height_cm,
product_width_cm, COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.olist_products_dataset`
GROUP BY product_id, product_category_name, product_name_lenght,
product_description_lenght, product_photos_qty,
product_weight_g, product_length_cm, product_height_cm,
product_width_cm
HAVING COUNT(*) > 1;
/** Tüm ürünler benzersiz; duplicate ürün yok. **/
--------------------------------------------------------------------------
-- Gi) ORDERS TABLOSUNDA NULL DEĞERLER
SELECT 'order_id' AS column_name, COUNT(*) AS number_of_nulls
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_id IS NULL
UNION ALL
SELECT 'customer_id', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE customer_id IS NULL
UNION ALL
SELECT 'order_status', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_status IS NULL
UNION ALL
SELECT 'order_purchase_timestamp', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_purchase_timestamp IS NULL
UNION ALL
SELECT 'order_approved_at', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_approved_at IS NULL
UNION ALL
SELECT 'order_delivered_carrier_date', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_delivered_carrier_date IS NULL
UNION ALL
SELECT 'order_delivered_customer_date', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_delivered_customer_date IS NULL
UNION ALL
SELECT 'order_estimated_delivery_date', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_estimated_delivery_date IS NULL
ORDER BY number_of_nulls DESC;
/** 3 sütunda anlamlı miktarda NULL var (order_delivered_customer_date, order_delivered_carrier_date, order_approved_at) **/
-- Gii) ORDERS TABLOSUNDA DUPLICATE DEĞERLER
SELECT order_id, customer_id, order_status,
order_purchase_timestamp, order_approved_at,
order_delivered_carrier_date, order_delivered_customer_date,
order_estimated_delivery_date,
COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
GROUP BY order_id, customer_id, order_status,
order_purchase_timestamp, order_approved_at,
order_delivered_carrier_date, order_delivered_customer_date,
order_estimated_delivery_date
HAVING COUNT(*) > 1;
/** Bu tabloda duplicate ORDER yok **/
-------------------------------------------
-- Hi) ORDER_ITEMS TABLOSUNDA NULL DEĞERLER
SELECT 'order_id' AS column_name, COUNT(*) AS number_of_nulls
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE order_id IS NULL
UNION ALL
SELECT 'order_item_id', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE order_item_id IS NULL
UNION ALL
SELECT 'product_id', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE product_id IS NULL
UNION ALL
SELECT 'seller_id', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE seller_id IS NULL
UNION ALL
SELECT 'shipping_limit_date', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE shipping_limit_date IS NULL
UNION ALL
SELECT 'price', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE price IS NULL
UNION ALL
SELECT 'freight_value', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
WHERE freight_value IS NULL
ORDER BY number_of_nulls;
/** order_items tablosunda NULL yok **/
-- Hii) ORDER_ITEMS DUPLICATE’LAR
SELECT order_id, order_item_id, product_id, seller_id,
shipping_limit_date, price, freight_value,
COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.olist_order_items_dataset`
GROUP BY order_id, order_item_id, product_id, seller_id,
shipping_limit_date, price, freight_value
HAVING COUNT(*) > 1
ORDER BY number_of_duplicates;
/** order_items tablosunda duplicate yok **/
---------------------------------------------------------------
-- Ii) ORDER_PAYMENTS TABLOSUNDA NULL DEĞERLER
SELECT 'order_id' AS column_name, COUNT(*) AS number_of_nulls
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset`
WHERE order_id IS NULL
UNION ALL
SELECT 'payment_sequential', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset`
WHERE payment_sequential IS NULL
UNION ALL
SELECT 'payment_type', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset`
WHERE payment_type IS NULL
UNION ALL
SELECT 'payment_installments', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset`
WHERE payment_installments IS NULL
UNION ALL
SELECT 'payment_value', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset`
WHERE payment_value IS NULL
ORDER BY number_of_nulls;
-- Iii) ORDER_PAYMENTS DUPLICATE’LAR
SELECT order_id, payment_sequential, payment_type,
payment_installments, payment_value,
COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset`
GROUP BY order_id, payment_sequential, payment_type,
payment_installments, payment_value
HAVING COUNT(*) > 1
ORDER BY number_of_duplicates;
/** Bu tabloda null veya duplicate yok **/
-------------------------------------------------------------
-- Ji) ORDER REVIEWS TABLOSUNDA NULL DEĞERLER
SELECT 'review_id' AS column_name, COUNT(*) AS number_of_nulls
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE review_id IS NULL
UNION ALL
SELECT 'order_id', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE order_id IS NULL
UNION ALL
SELECT 'review_score', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE review_score IS NULL
UNION ALL
SELECT 'review_comment_title', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE review_comment_title IS NULL
UNION ALL
SELECT 'review_comment_message', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE review_comment_message IS NULL
UNION ALL
SELECT 'review_creation_date', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE review_creation_date IS NULL
UNION ALL
SELECT 'review_answer_timestamp', COUNT(*)
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
WHERE review_answer_timestamp IS NULL
ORDER BY number_of_nulls DESC;
/** Tüm order_id’lerde review_score var; bu ürün doğrulaması için iyi. **/
-- Jii) ORDER REVIEWS TABLOSUNDA DUPLICATE’LAR
SELECT review_id, order_id, review_score,
review_comment_title, review_comment_message,
review_creation_date, review_answer_timestamp,
COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.olist_order_reviews_dataset`
GROUP BY review_id, order_id, review_score,
review_comment_title, review_comment_message,
review_creation_date, review_answer_timestamp
HAVING COUNT(*) > 1
ORDER BY number_of_duplicates;
/** Duplicate review yok; tüm review’lar benzersiz. **/
-- Ki) PRODUCT CATEGORY NAME TRANSLATION TABLOSUNDA NULL DEĞERLER
SELECT 'product_category_name' AS column_name, COUNT(*) AS number_of_nulls
FROM `pacific-legend-474020-b4.olist.product_category_name_translation`
WHERE string_field_0 IS NULL
UNION ALL
SELECT 'product_category_name_english', COUNT(*)
FROM `pacific-legend-474020-b4.olist.product_category_name_translation`
WHERE string_field_1 IS NULL
ORDER BY number_of_nulls DESC;
-- Kii) PRODUCT CATEGORY NAME TRANSLATION TABLOSUNDA DUPLICATE’LAR
SELECT string_field_0,
string_field_1,
COUNT(*) AS number_of_duplicates
FROM `pacific-legend-474020-b4.olist.product_category_name_translation`
GROUP BY string_field_0,
string_field_1
HAVING COUNT(*) > 1
ORDER BY number_of_duplicates;
/** Bu tabloda null veya duplicate yok. **/
-----------------------------------------------------------------
/**VERİ KÜMESİNİ TEMİZLEME**/
/** 2) GEOLOCATION TABLOSUNDAKİ ÖZEL KARAKTERLERİ TEMİZLEME (CITY SÜTUNU)
Ai) GEOLOCATION_CITY SÜTUNUNDA TESPİT EDİLEN ÖZEL KARAKTERLERİ KALDIRMA
Brezilya geolocation_city sütununda kullanılan bazı özel alfabe karakterlerini tespit ettim: á, é, í, ó, ú, â, ê, ô, ã, õ, à ve ç
Bunlar, müşteri ve satıcı şehir sütunlarında tutarlılık için değiştirilmelidir.
--- Özel alfabe karakterlerini değiştirmek için
geolocation_city sütununu düzenlemek üzere geçici bir tablo oluşturdum. **/
WITH new_geolocation_one AS (
SELECT DISTINCT geolocation_zip_code_prefix, geolocation_lat,
geolocation_lng, geolocation_city,
geolocation_state
FROM `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
)
SELECT *
FROM new_geolocation_one
ORDER BY geolocation_zip_code_prefix; /** NOT - TÜM DUPLICATE’LARI KALDIRMAK İÇİN **/
CREATE OR REPLACE TABLE `pacific-legend-474020-b4.olist.real_geolocation_cleaned` AS
WITH unique_geolocation AS (
SELECT DISTINCT
geolocation_zip_code_prefix,
geolocation_lat,
geolocation_lng,
geolocation_city,
geolocation_state
FROM `pacific-legend-474020-b4.olist.olist_geolocation_dataset`
),
real_geolocation_cleaned AS (
SELECT
geolocation_zip_code_prefix,
geolocation_lat,
geolocation_lng,
-- aksanları kaldır + küçük harfe çevir + ' ` - temizle + çoklu boşlukları tek boşluk yap
REGEXP_REPLACE(
REGEXP_REPLACE(
TRANSLATE(LOWER(geolocation_city), 'áéíóúâêôãõàç', 'aeiouaeoaoac'),
'[\'`-]', -- tek tırnak/apostrof, backtick, tire
' '
),
'\\s+',
' '
) AS geolocation_city,
geolocation_state
FROM unique_geolocation
)
SELECT *
FROM real_geolocation_cleaned
ORDER BY geolocation_city;
/** Aii) GEOLOCATION TABLOSUNDAKİ ÖNCEKİ GEÇİCİ TABLODAN SONRA HÂLÂ BAŞKA ÖZEL KARAKTERLER VAR MI GÖRMEK İÇİN **/
SELECT *
FROM `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
WHERE REGEXP_CONTAINS(lower(geolocation_city), r'[^a-z\s]');
/** Aiv) real_geolocation_cleaned TABLOSUNDA, BAZI EYALET KISALTMALARININ GEOLOCATION_CITY SÜTUNUNDA YER ALDIĞINI (BAZI ŞEHİRLERİN İLGİLİ EYALETLERİYLE AYNI ADI TAŞIDIĞINI) DA GÖRDÜM **/
SELECT * FROM
`pacific-legend-474020-b4.olist.real_geolocation_cleaned`
WHERE geolocation_city LIKE '%pr' OR
geolocation_city LIKE '%sp' OR
geolocation_city LIKE '%mg' OR
geolocation_city LIKE '%mt' OR
geolocation_city LIKE '%ms' OR
geolocation_city LIKE '%pr' OR
geolocation_city LIKE '%sc' OR
LENGTH(geolocation_city) < 3
ORDER BY geolocation_city;
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'sao paulo'
WHERE geolocation_city = 'sp';
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'rio de janeiro'
WHERE geolocation_city = 'rj';
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'belo horizonte'
WHERE geolocation_city = 'bh';
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'franca'
WHERE geolocation_zip_code_prefix = 14407;
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'guarulhos'
WHERE geolocation_zip_code_prefix = 7174;
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'lavras'
WHERE geolocation_zip_code_prefix = 37200;
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'limeira do oeste'
WHERE geolocation_zip_code_prefix = 38295;
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = 'arraial dajuda'
WHERE geolocation_state = 'BA' AND
geolocation_city LIKE '%arraial d%';
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = TRIM(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(geolocation_city, 'd oeste', 'doeste'),
'd oeste', 'doeste'),
'd arco', 'darco'),
'd agua', 'dagua'),
'd alianca', 'dalianca'),
'd alho', 'dalho'),
'sao joao do pau d%26apos%3balho', 'sao joao do pau dalho')
)
WHERE geolocation_city LIKE '%d oeste%'
OR geolocation_city LIKE '%d oeste%'
OR geolocation_city LIKE '%d arco%'
OR geolocation_city LIKE '%d agua%'
OR geolocation_city LIKE '%d alianca%'
OR geolocation_city LIKE '%d alho%'
OR geolocation_city LIKE '%sao joao do pau d%26apos%3balho%';
UPDATE `pacific-legend-474020-b4.olist.real_geolocation_cleaned`
SET geolocation_city = TRIM(geolocation_city)
WHERE geolocation_city != TRIM(geolocation_city);
SELECT COUNT(DISTINCT(geolocation_state)) AS total_states
FROM `pacific-legend-474020-b4.olist.real_geolocation_cleaned`;
-- TOPLAM EYALET - 27
SELECT COUNT(DISTINCT(geolocation_city)) AS total_cities
FROM `pacific-legend-474020-b4.olist.real_geolocation_cleaned`;
--TOPLAM ŞEHİR 5907
-- SELLERS TABLOSUNDA ÖZEL KARAKTERLERİ TEMİZLEME (CITY SÜTUNU)
-- A-Z ve boşluk dışında karakter kaldı mı?
SELECT *
FROM `pacific-legend-474020-b4.olist.olist_sellers_dataset`
WHERE REGEXP_CONTAINS(seller_city, r'[^a-z\s]');
-- StandardSQL
-- 1) Temiz tabloyu üret
CREATE OR REPLACE TABLE `pacific-legend-474020-b4.olist.sellers_clean` AS
SELECT
seller_id,
seller_zip_code_prefix,
-- şehir temizleme: küçük harf + aksan kaldır + ' ` - temizle + çoklu boşlukları tekle + kırp
TRIM(
REGEXP_REPLACE(
REGEXP_REPLACE(
TRANSLATE(LOWER(seller_city), 'áéíóúâêôãõàç', 'aeiouaeoaoac'),
'[\'`-]', ' ' -- tek tırnak, backtick, tire → boşluk
),
r'\s+', ' ' -- çoklu boşluk → tek boşluk
)
) AS seller_city,
seller_state
FROM `pacific-legend-474020-b4.olist.olist_sellers_dataset`;
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = 'sao paulo'
WHERE seller_state = 'SP' AND
seller_city IN ('sao paulo sp', 'sp');
SELECT *
FROM `pacific-legend-474020-b4.olist.sellers_clean`
WHERE REGEXP_CONTAINS(seller_city, r'[^a-z\s]');
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = 'rio de janeiro'
WHERE seller_city = '04482255';
-- 'sp', 'sao paulo sp', 'saopaulo' gibi varyasyonları düzelt
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = 'sao paulo'
WHERE seller_state = 'SP'
AND seller_city IN ('sp','sao paulo sp','saopaulo');
-- d + kelime düzeltmeleri (ör: "d oeste" → "doeste")
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = REGEXP_REPLACE(seller_city, r'\bd\s+oeste\b', 'doeste')
WHERE REGEXP_CONTAINS(seller_city, r'\bd\s+oeste\b');
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = REGEXP_REPLACE(seller_city, r'\bd\s+arco\b', 'darco')
WHERE REGEXP_CONTAINS(seller_city, r'\bd\s+arco\b');
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = REGEXP_REPLACE(seller_city, r'\bd\s+agua\b', 'dagua')
WHERE REGEXP_CONTAINS(seller_city, r'\bd\s+agua\b');
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = REGEXP_REPLACE(seller_city, r'\bd\s+alianca\b', 'dalianca')
WHERE REGEXP_CONTAINS(seller_city, r'\bd\s+alianca\b');
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = REGEXP_REPLACE(seller_city, r'\bd\s+alho\b', 'dalho')
WHERE REGEXP_CONTAINS(seller_city, r'\bd\s+alho\b');
-- son temizlik: kalan çift boşlukları tekle + kırp
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = TRIM(REGEXP_REPLACE(seller_city, r'\s+', ' '))
WHERE seller_city != TRIM(REGEXP_REPLACE(seller_city, r'\s+', ' '));
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = TRIM(REGEXP_REPLACE(seller_city, r'\s*[/\\,]\s*.*$', ''))
WHERE REGEXP_CONTAINS(seller_city, r'[/\\,]');
-- sbc → sao bernardo do campo (senin önceki tercihine göre 'sbcampo')
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = 'sbcampo'
WHERE LOWER(seller_city) IN ('sbc', 'sbc/sp', 'sbc sp');
-- Eğer mail yakalarsan (örnek: vendas@creditparts.com.br), PR eyaleti ise maringa yap
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = 'maringa'
WHERE seller_city LIKE '%@%' AND seller_state = 'PR';
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = TRIM(
REGEXP_REPLACE(
REGEXP_REPLACE(
TRANSLATE(LOWER(seller_city), 'áéíóúâêôãõàç', 'aeiouaeoaoac'),
r'[\'`.\-]', ' '
),
r'\s+', ' '
)
)
WHERE TRUE;
SELECT seller_city, seller_state
FROM `pacific-legend-474020-b4.olist.sellers_clean`
WHERE REGEXP_CONTAINS(seller_city, r'[^a-z\s]');
-- StandardSQL
UPDATE `pacific-legend-474020-b4.olist.sellers_clean`
SET seller_city = TRIM(
REGEXP_REPLACE( -- çoklu boşluk → tek
TRANSLATE( -- aksanları sil
REGEXP_REPLACE( -- 3) d + ayraç + (oeste|ajuda) düzelt
REGEXP_REPLACE( -- 2) parantez içini kaldır
LOWER(seller_city), -- 1) küçük harf
'\\s*\\([^)]*\\)\\s*', '' -- (...) → (boş)
),
'\\bd\\W*\\s*oeste\\b', 'doeste' -- d’/d`/d' / d + oeste → doeste
),
'áéíóúâêôãõàç', 'aeiouaeoaoac' -- aksanlar
),
'\\s+', ' ' -- çoklu boşluk → tek
)
)
WHERE
REGEXP_CONTAINS(seller_city, '\\([^)]*\\)') -- parantez varsa
OR REGEXP_CONTAINS(LOWER(seller_city), '\\bd\\W*\\s*oeste\\b')
OR REGEXP_CONTAINS(LOWER(seller_city), '\\bd\\W*\\s*ajuda\\b')
OR REGEXP_CONTAINS(seller_city, '[áéíóúâêôãõàç]'); -- aksanlı harf varsa
/** Biii) SELLERS TABLOSUNDA DA BAZI EYALET KISALTMALARININ SELLER_CITY SÜTUNUNDA YER ALDIĞINI (BAZI ŞEHİRLERİN İLGİLİ EYALETLERLE AYNI ADI TAŞIDIĞINI) FARK ETTİM **/
select count(distinct (seller_state)) as total_states
from `pacific-legend-474020-b4.olist.sellers_clean`;
-- TOPLAM SATICI EYALETİ - 23
SELECT COUNT(DISTINCT(seller_city)) AS total_cities
FROM `pacific-legend-474020-b4.olist.sellers_clean`;
-- TOPLAM SATICI ŞEHRİ -589
--CUSTOMERS TABLOSUNDA ÖZEL KARAKTERLERİ TEMİZLEME (CITY SÜTUNU)
-- a–z ve boşluk dışında karakter kaldı mı?
SELECT customer_city, customer_state
FROM `pacific-legend-474020-b4.olist.olist_customers_dataset`
WHERE REGEXP_CONTAINS(customer_city, r'[^a-z\s]');
-- StandardSQL
CREATE OR REPLACE TABLE `pacific-legend-474020-b4.olist.customers_clean` AS
SELECT
customer_id,
customer_unique_id,
customer_zip_code_prefix,
TRIM(
REGEXP_REPLACE( -- çoklu boşluk → tek
REGEXP_REPLACE( -- ' ` . , - / → boşluk
TRANSLATE(LOWER(customer_city), -- küçük harf + aksanları sil
'áéíóúâêôãõàç',
'aeiouaeoaoac'),
r'[\'`.,/\-]', ' '
),
r'\s+', ' '
)
) AS customer_city,
customer_state
FROM `pacific-legend-474020-b4.olist.olist_customers_dataset`;
SELECT customer_city, customer_state
FROM `pacific-legend-474020-b4.olist.customers_clean`
WHERE REGEXP_CONTAINS(customer_city, r'[^a-z\s]');
UPDATE `pacific-legend-474020-b4.olist.customers_clean`
SET customer_city = 'mutum'
WHERE customer_city = 'quilometro 14 do mutum' AND customer_state = 'ES';
select count(distinct customer_city) as total_cities
from `pacific-legend-474020-b4.olist.customers_clean`;
select count(distinct (customer_state)) as total_states
from `pacific-legend-474020-b4.olist.customers_clean`;
--EN ÇOK SİPARİŞE SAHİP İLK 5 MÜŞTERİ EYALETİ
SELECT c.customer_state, count(*) AS number_of_orders
FROM `pacific-legend-474020-b4.olist.customers_clean` as c
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` as o ON
o.customer_id = c.customer_id
GROUP BY c.customer_state
ORDER BY number_of_orders DESC
limit 5;
-- CEVAP - SP(41746), RJ(12852), MG(11635), RS(5466), PR(5045)
--EN AZ SİPARİŞE SAHİP İLK 5 MÜŞTERİ EYALETİ
SELECT c.customer_state, count(*) AS number_of_orders
FROM `pacific-legend-474020-b4.olist.customers_clean` as c
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` as o ON
o.customer_id = c.customer_id
GROUP BY c.customer_state
ORDER BY number_of_orders asc
limit 5;
-- CEVAP - RR(46), AP(68), AC(81), AM(148), RO(253)
--EN ÇOK SİPARİŞE SAHİP İLK 10 MÜŞTERİ ŞEHRİ
select
c.customer_city,
count(*) as number_of_orders
FROM `pacific-legend-474020-b4.olist.customers_clean` as c
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` as o ON
o.customer_id = c.customer_id
group by c.customer_city
order by number_of_orders desc
limit 10;
--EN AZ SİPARİŞE SAHİP MÜŞTERİ ŞEHİRLERİ
select
c.customer_city,
count(*) as number_of_orders
FROM `pacific-legend-474020-b4.olist.customers_clean` as c
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` as o ON
o.customer_id = c.customer_id
group by c.customer_city
having number_of_orders<500;
--4412 şehrin 4090'nunda müşteri sipariş sayısı 500'ün altında
--SİPARİŞ RAPORU
--Toplam benzersiz sipariş sayısı 99441
SELECT
count(*) as number_of_orders
from `pacific-legend-474020-b4.olist.olist_orders_dataset`;
-- Başarıyla teslim edilen (delivered) benzersiz sipariş sayısı 96478
SELECT COUNT(DISTINCT order_id) AS delivered_unique_orders
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_status = 'delivered';
-- Teslim EDİLMEYEN siparişlerin durum kırılımı
SELECT
order_status,
COUNT(*) AS process_count
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
WHERE order_status != 'delivered'
GROUP BY order_status
ORDER BY process_count DESC;
--GÜNLÜK SİPARİŞ
-- Günlük sipariş adedi (en çok sipariş alınan günler en üstte)
SELECT
DATE(order_purchase_timestamp) AS time_ordered,
COUNT(*) AS no_of_orders_daily
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
GROUP BY time_ordered
ORDER BY no_of_orders_daily DESC;
/** CEVAP - 2017 Kasım’da büyük bir sipariş sıçraması vardı; bunun nedeni insanların Noel için bir ay önceden hazırlık yapması olabilir. En yüksek sipariş 24 Kasım’da (1176). **/
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- # ANALİZ VE İŞ İÇGÖRÜLERİ
-- Olist'in e-ticaret platformu için içgörü üretmek ve büyüme fırsatlarını optimize etmek amacıyla şu soruları yanıtlıyoruz:
-- 1) Toplam gelir nedir? Zaman içindeki değişimi nasıldır?
-- 2) Toplam sipariş sayısı nedir? Aylık/mevsimsel değişim nasıldır?
-- 3) En popüler ürün kategorileri hangileridir? Satış hacimleri nasıl karşılaştırılır?
-- 4) Ortalama sipariş değeri (AOV) nedir? Kategori/ödeme tipine göre nasıl değişir?
-- 5) Kaç aktif satıcı vardır? Zaman içindeki değişimi nasıldır?
-- 6) Satıcı puanlarının dağılımı nedir? Satış performansına etkisi nedir?
-- 7) Tekrar alım yapan müşteri sayısı ve gelir payı nedir?
-- 8) Ürünlerin ortalama müşteri puanı (rating) nedir? Satışa etkisi nedir?
-- 9) Ortalama sipariş iptal oranı nedir? Performansa etkisi nedir?
-- 10) En çok satan ürünler nelerdir? Trendleri zaman içinde nasıl değişir?
-- 11) En sık kullanılan ödeme yöntemleri nelerdir? Kategori/konuma göre nasıl farklılaşır?
-- 13) Hangi kategoriler en yüksek kâr marjına sahiptir? Kârlılık nasıl artırılabilir?
-- 15) Yüksek müşteri yoğunluklu konumlar nelerdir? Bu konumlarda retention nasıl?
-- ## 1) Toplam gelir ve zaman içindeki değişim
-- Toplam gelir (yalnızca teslim edilen siparişler) → 15419773.749999616
SELECT
SUM(oi.price + oi.freight_value) AS total_revenue
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset` o
JOIN `pacific-legend-474020-b4.olist.olist_order_items_dataset` oi USING(order_id)
WHERE o.order_status = 'delivered';
-- Toplam gelir (ödenen tutarların toplamı) → 15862973.819999663
SELECT
SUM(op.payment_value) AS total_revenue_payments
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset` op
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` o
USING (order_id)
WHERE o.order_approved_at IS NOT NULL -- Ödeme onayı var
AND o.order_status <> 'canceled'; -- İptaller hariç
-- Aylık ciro (ödemeye göre)
SELECT
DATE_TRUNC(DATE(o.order_purchase_timestamp), MONTH) AS year_month,
round(SUM(op.payment_value),2) AS total_revenue
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset` as op
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` as o
USING (order_id)
WHERE o.order_approved_at IS NOT NULL
AND o.order_status <> 'canceled'
GROUP BY year_month
ORDER BY year_month;
-- Çeyreklik ciro (ödemeye göre)
SELECT
DATE_TRUNC(DATE(o.order_purchase_timestamp), QUARTER) AS year_quarter,
round(SUM(op.payment_value),2) AS total_revenue
FROM `pacific-legend-474020-b4.olist.olist_order_payments_dataset` op
JOIN `pacific-legend-474020-b4.olist.olist_orders_dataset` o
USING (order_id)
WHERE o.order_approved_at IS NOT NULL
AND o.order_status <> 'canceled'
GROUP BY year_quarter
ORDER BY year_quarter;
-- ## 2) Toplam sipariş sayısı ve dönemsel değişim
select count(distinct order_id) as total_orders
from`pacific-legend-474020-b4.olist.olist_orders_dataset`;
-- Aylık zaman serisi (trend)
select
date_trunc(date(order_purchase_timestamp),MONTH) AS month,
count(order_id) as nb_of_orders
from `pacific-legend-474020-b4.olist.olist_orders_dataset`
group by month
order by month;
-- Ay bazında mevsimsellik (yıllardan bağımsız)
SELECT
EXTRACT(MONTH FROM DATE(order_purchase_timestamp)) AS month_no,
FORMAT_DATE('%B', DATE_TRUNC(DATE(order_purchase_timestamp), MONTH)) AS month_name,
COUNT(*) AS orders
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
GROUP BY month_no, month_name
ORDER BY month_no;
-- Çeyrek (Q1–Q4) kırılımında sipariş trendi
SELECT
DATE_TRUNC(DATE(order_purchase_timestamp), QUARTER) AS quarter_start,
EXTRACT(YEAR FROM DATE(order_purchase_timestamp)) AS yr,
EXTRACT(QUARTER FROM DATE(order_purchase_timestamp)) AS qtr,
COUNT(*) AS orders
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
GROUP BY quarter_start, yr, qtr
ORDER BY quarter_start;
-- Haftalık sipariş trendi (ISO haftası)
SELECT
EXTRACT(YEAR FROM DATE(order_purchase_timestamp)) AS yr,
EXTRACT(ISOWEEK FROM DATE(order_purchase_timestamp)) AS iso_week,
DATE_TRUNC(DATE(order_purchase_timestamp), ISOYEAR) AS iso_year_start,
COUNT(*) AS orders
FROM `pacific-legend-474020-b4.olist.olist_orders_dataset`
GROUP BY yr, iso_week, iso_year_start
ORDER BY yr, iso_week;
-- ## 3) Popüler kategoriler ve satış hacimleri
-- Kategori bazında sipariş sayısı + (payments) toplam ciro
SELECT
COALESCE(pc.string_field_1, 'unknown_category') AS category,
COUNT( o.order_id) AS num_of_orders,
SUM(pa.payment_value) AS total_revenue