-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1. Data Cleaning.sql
More file actions
2087 lines (1792 loc) · 66.5 KB
/
1. Data Cleaning.sql
File metadata and controls
2087 lines (1792 loc) · 66.5 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
---/ sqlite - data cleaning
---/ sqlite - create tabel data raw dan clean
---| Cek isi tabel
SELECT * FROM raw_superstore_orders;
SELECT * FROM clean_superstore_orders;
---/ TAHAP 0 — PERSIAPAN
---| Import file csv to databases
sqlite3 "D:\Salsa\OneDrive\SQL_Files\PORTOFOLIO\Project1\DB1_Project1.db"
.databases
.tables
.mode csv
--- The folder path (D:/......./Superstore_Orders_q1.csv) depends on where you save them
.import "D:/Salsa/OneDrive/SQL_Files/PORTOFOLIO/Project1/Superstore_Orders_q1.csv" csv1
.import "D:/Salsa/OneDrive/SQL_Files/PORTOFOLIO/Project1/Superstore_Orders_q2.csv" csv2
.import "D:/Salsa/OneDrive/SQL_Files/PORTOFOLIO/Project1/raw_state_city.csv" raw_state_city
.schema csv1
.schema csv2
.schema zip_code_csv
---| Cek isi csv1 dan csv2
SELECT * FROM csv1 UNION ALL
SELECT * FROM csv2;
---/ CREATE TABLE untuk raw data
---| All attributes in lowercase no space
CREATE Table IF NOT EXISTS raw_superstore_orders (
row_id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id TEXT,
order_date DATE,
ship_date DATE,
ship_mode TEXT,
customer_id TEXT,
country TEXT,
state TEXT,
city TEXT,
postal_code TEXT,
region TEXT,
product_id TEXT,
category TEXT,
sub_category TEXT,
product_name TEXT,
sales DECIMAL(10,2),
quantity INTEGER,
discount DECIMAL(4,2),
profit DECIMAL(10,2) );
---/ INSERT DATA ke tabel raw data
---| Rewrite all attributes (instead of select all) karena mau mengubah penamaan (ex: Row ID to row_id)
---| Kolom country, state, city di data import tidak berurutan, jadi perlu diurutkan peletakannya
WITH csv_combined AS (
SELECT
"Row ID",
"Order ID",
DATE(substr("Order Date", 7, 4) || '-' || substr("Order Date", 1, 2) || '-' || substr("Order Date", 4, 2)) AS "Order Date",
DATE(substr("Ship Date", 7, 4) || '-' || substr("Ship Date", 1, 2) || '-' || substr("Ship Date", 4, 2)) AS "Ship Date",
"Ship Mode",
"Customer ID",
"Country",
"State",
"City",
printf('%05d', "Postal Code"),
"Region",
"Product ID",
"Category",
"Sub-Category",
"Product Name",
round("Sales", 2),
"Quantity",
"Discount",
round("Profit",2) FROM csv1
UNION ALL
SELECT
"Row ID",
"Order ID",
DATE(substr("Order Date", 7, 4) || '-' || substr("Order Date", 1, 2) || '-' || substr("Order Date", 4, 2)) AS "Order Date",
DATE(substr("Ship Date", 7, 4) || '-' || substr("Ship Date", 1, 2) || '-' || substr("Ship Date", 4, 2)) AS "Ship Date",
"Ship Mode",
"Customer ID",
"Country",
"State",
"City",
printf('%05d', "Postal Code"),
"Region",
"Product ID",
"Category",
"Sub-Category",
"Product Name",
round("Sales", 2),
"Quantity",
"Discount",
round("Profit",2) FROM csv2)
INSERT INTO raw_superstore_orders (
row_id,
order_id,
order_date,
ship_date,
ship_mode,
customer_id,
country,
state,
city,
postal_code,
region,
product_id,
category,
sub_category,
product_name,
sales,
quantity,
discount,
profit)
SELECT * FROM csv_combined;
---/ CREATE TABLE backup tabel raw data (BACKUP_raw_superstore_orders)
---| All attributes in lowercase no space (format sama kaya raw_superstore_orders)
CREATE Table IF NOT EXISTS BACKUP_raw_superstore_orders (
row_id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id TEXT,
order_date DATE,
ship_date DATE,
ship_mode TEXT,
customer_id TEXT,
country TEXT,
state TEXT,
city TEXT,
postal_code TEXT,
region TEXT,
product_id TEXT,
category TEXT,
sub_category TEXT,
product_name TEXT,
sales DECIMAL(10,2),
quantity INTEGER,
discount DECIMAL(4,2),
profit DECIMAL(10,2) );
---/ INSERT DATA ke BACKUP_raw_superstore_orders
INSERT INTO BACKUP_raw_superstore_orders
SELECT * FROM raw_superstore_orders;
---/ CREATE TABLE external_state_city
CREATE Table if NOT exists external_state_city (
state TEXT,
city TEXT,
UNIQUE (state, city) );
---/ INSERT INTO TABLE external_state_city
WITH
insert_state_city AS (
SELECT
substr("State;City", 1, instr("State;City",';') - 1) AS state,
substr("State;City", instr("State;City",';') + 1) AS city
FROM raw_state_city
ORDER BY state, city)
INSERT INTO external_state_city (State, city)
SELECT state, city FROM insert_state_city;
---| CEK DATA PROBLEMATIC (NULL, blank '', double space, strips)
---- tabel raw data
WITH cek_issues AS (
SELECT
'row_id' AS attributes,
SUM(row_id IS NULL) AS "NULL",
SUM(trim(row_id) = '') AS "BLANK",
SUM(trim(row_id) LIKE '% %') AS "SPACES",
SUM(row_id LIKE '%--%') AS "STRIPS"
FROM raw_superstore_orders
UNION ALL
SELECT
'order_id',
SUM(order_id IS NULL),
SUM(trim(order_id) = ''),
SUM(trim(order_id) LIKE '% %'),
SUM(order_id LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'order_date',
SUM(order_date IS NULL),
SUM(trim(order_date) = ''),
SUM(trim(order_date) LIKE '% %'),
SUM(order_date LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'ship_date',
SUM(ship_date IS NULL),
SUM(trim(ship_date) = ''),
SUM(trim(ship_date) LIKE '% %'),
SUM(ship_date LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'ship_mode',
SUM(ship_mode IS NULL),
SUM(trim(ship_mode) = ''),
SUM(trim(ship_mode) LIKE '% %'),
SUM(ship_mode LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'customer_id',
SUM(customer_id IS NULL),
SUM(trim(customer_id) = ''),
SUM(trim(customer_id) LIKE '% %'),
SUM(customer_id LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'country',
SUM(country IS NULL),
SUM(trim(country) = ''),
SUM(trim(country) LIKE '% %'),
SUM(country LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'state',
SUM(state IS NULL),
SUM(trim(state) = ''),
SUM(trim(state) LIKE '% %'),
SUM(state LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'city',
SUM(city IS NULL),
SUM(trim(city) = ''),
SUM(trim(city) LIKE '% %'),
SUM(city LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'postal_code',
SUM(postal_code IS NULL),
SUM(trim(postal_code) = ''),
SUM(trim(postal_code) LIKE '% %'),
SUM(postal_code LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'region',
SUM(region IS NULL),
SUM(trim(region) = ''),
SUM(trim(region) LIKE '% %'),
SUM(region LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'product_id',
SUM(product_id IS NULL),
SUM(trim(product_id) = ''),
SUM(trim(product_id) LIKE '% %'),
SUM(product_id LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'category',
SUM(category IS NULL),
SUM(trim(category) = ''),
SUM(trim(category) LIKE '% %'),
SUM(category LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'sub_category',
SUM(sub_category IS NULL),
SUM(trim(sub_category) = ''),
SUM(trim(sub_category) LIKE '% %'),
SUM(sub_category LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'product_name',
SUM(product_name IS NULL),
SUM(trim(product_name) = ''),
SUM(trim(product_name) LIKE '% %'),
SUM(product_name LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'sales',
SUM(sales IS NULL),
SUM(trim(sales) = ''),
SUM(trim(sales) LIKE '% %'),
SUM(sales LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'quantity',
SUM(quantity IS NULL),
SUM(trim(quantity) = ''),
SUM(trim(quantity) LIKE '% %'),
SUM(quantity LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'discount',
SUM(discount IS NULL),
SUM(trim(discount) = ''),
SUM(trim(discount) LIKE '% %'),
SUM(discount LIKE '%--%')
FROM raw_superstore_orders
UNION ALL
SELECT
'profit',
SUM(profit IS NULL),
SUM(trim(profit) = ''),
SUM(trim(profit) LIKE '% %'),
SUM(profit LIKE '%--%')
FROM raw_superstore_orders)
SELECT * FROM cek_issues
ORDER BY "NULL" DESC, "BLANK" DESC, "SPACES" DESC, "STRIPS" DESC;
---/ Problem data yang terdeteksi:
-- 1. BLANK → discount, region
-- SOLUTION → UPDATE ke 'BLANK'
-- 2. STRIPS → region
-- SOLUTION → UPDATE ke 'Unknown'
-- 3. SPACES → product_name
-- SOLUTION → TRIM column
---/ INPUT KE LOG TABLE
---| 1. INSERT PROBLEMATIC DATA
--- source_table : raw_superstore_orders
--- reason : BLANK
--- attribute : discount
--- solution : flag as BLANK
INSERT INTO audit_log
(row_id, attribute, reason, solution, source_table, total_rows)
SELECT
row_id,
'discount',
'BLANK',
'flag as BLANK',
'raw_superstore_orders',
(SELECT count(row_id) FROM raw_superstore_orders)
FROM raw_superstore_orders
GROUP BY row_id
HAVING discount = ''
ORDER BY row_id;
---| 2. INSERT PROBLEMATIC DATA
--- source_table : raw_superstore_orders
--- reason : BLANK
--- attribute : region
--- solution : flag as Unknown
INSERT INTO audit_log
(row_id, attribute, reason, solution, source_table, total_rows)
SELECT
row_id,
'region',
'BLANK',
'flag as Unknown',
'raw_superstore_orders',
(SELECT count(row_id) FROM raw_superstore_orders)
FROM raw_superstore_orders
GROUP BY row_id
HAVING region = ''
ORDER BY row_id;
---| 3. INSERT PROBLEMATIC DATA
--- source_table : raw_superstore_orders
--- reason : STRIPS
--- attribute : region
--- solution : flag as Unknown
INSERT INTO audit_log
(row_id, attribute, reason, solution, source_table, total_rows)
SELECT
row_id,
'region',
'STRIPS',
'flag as Unknown',
'raw_superstore_orders',
(SELECT count(row_id) FROM raw_superstore_orders)
FROM raw_superstore_orders
GROUP BY row_id
HAVING region LIKE '%--%'
ORDER BY row_id;
---| 4. INSERT PROBLEMATIC DATA
--- source_table : raw_superstore_orders
--- reason : SPACES
--- attribute : product_name
--- solution : replace spaces to a space
INSERT INTO audit_log
(row_id, attribute, reason, solution, source_table, total_rows)
SELECT
row_id,
'product_name',
'SPACES',
'replace spaces to a space',
'raw_superstore_orders',
(SELECT count(row_id) FROM raw_superstore_orders)
FROM raw_superstore_orders
GROUP BY row_id
HAVING product_name LIKE '% %'
ORDER BY row_id;
---| UPDATE DATA PROBLEMATIC (NULL, blank '', double space, strips)
---- tabel raw data
UPDATE raw_superstore_orders
SET
discount = CASE
WHEN discount = '' THEN 'BLANK'
ELSE discount END,
region = CASE
WHEN region = '' OR region LIKE '%--%' THEN 'Unknown'
ELSE region END,
product_name = CASE
WHEN product_name LIKE '% %' THEN REPLACE(product_name, ' ', ' ')
ELSE product_name END;
---/ CREATE TABLE untuk cleaning data
---- With more specific and proper data types for each attributes
CREATE Table IF NOT EXISTS clean_superstore_orders (
row_id
INTEGER
PRIMARY KEY AUTOINCREMENT,
order_id
TEXT
NOT NULL
CHECK (length(order_id) = 14),
order_date
DATE -- untuk making sure format date YYYY-MM-DD
CHECK (order_date GLOB'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'),
ship_date
DATE -- untuk making sure format date YYYY-MM-DD
CHECK (ship_date GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'),
ship_mode
TEXT
NOT NULL,
customer_id
TEXT
NOT NULL
CHECK (length(customer_id) = 8),
country
TEXT
NOT NULL,
state
TEXT
NOT NULL,
city
TEXT
NOT NULL,
postal_code
TEXT
NOT NULL,
region
TEXT
NOT NULL,
product_id
TEXT
NOT NULL
CHECK (length(product_id) = 15),
category
TEXT
NOT NULL,
sub_category
TEXT
NOT NULL,
product_name
TEXT
NOT NULL,
sales
DECIMAL(10, 2)
NOT NULL
CHECK (sales >= 0),
quantity INTEGER
NOT NULL
CHECK (quantity >= 0),
discount DECIMAL(3, 2)
NOT NULL,
profit DECIMAL(10, 2)
NOT NULL);
---/ INSERT DATA ke tabel clean data
INSERT INTO clean_superstore_orders
SELECT * FROM raw_superstore_orders;
---/ Cek schema dan isi tabel clean data
.schema clean_superstore_orders;
SELECT * FROM clean_superstore_orders;
---/ TRIM all fields
UPDATE clean_superstore_orders
SET
row_id = trim(row_id),
order_id = trim(order_id),
order_date = trim(order_date),
ship_date = trim(ship_date),
ship_mode = trim(ship_mode),
customer_id = trim(customer_id),
country = trim(country),
state = trim(state),
city = trim(city),
postal_code = trim(postal_code),
region = trim(region),
product_id = trim(product_id),
category = trim(category),
sub_category = trim(sub_category),
product_name = trim(product_name),
sales = trim(sales),
quantity = trim(quantity),
discount = trim(discount),
profit = trim(profit);
---| CEK DATA PROBLEMATIC (NULL, blank '', double space, strips)
---- tabel data clean
WITH cek_issues AS (
SELECT
'row_id' AS attribute,
SUM(row_id IS NULL) AS issue_null,
SUM(trim(row_id) = '') AS issue_blank,
SUM(trim(row_id) LIKE '% %') AS issue_spaces,
SUM(row_id LIKE '%--%') AS issue_strips
FROM clean_superstore_orders
UNION ALL
SELECT
'order_id',
SUM(order_id IS NULL),
SUM(trim(order_id) = ''),
SUM(trim(order_id) LIKE '% %'),
SUM(order_id LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'order_date',
SUM(order_date IS NULL),
SUM(trim(order_date) = ''),
SUM(trim(order_date) LIKE '% %'),
SUM(order_date LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'ship_date',
SUM(ship_date IS NULL),
SUM(trim(ship_date) = ''),
SUM(trim(ship_date) LIKE '% %'),
SUM(ship_date LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'ship_mode',
SUM(ship_mode IS NULL),
SUM(trim(ship_mode) = ''),
SUM(trim(ship_mode) LIKE '% %'),
SUM(ship_mode LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'customer_id',
SUM(customer_id IS NULL),
SUM(trim(customer_id) = ''),
SUM(trim(customer_id) LIKE '% %'),
SUM(customer_id LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'country',
SUM(country IS NULL),
SUM(trim(country) = ''),
SUM(trim(country) LIKE '% %'),
SUM(country LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'state',
SUM(state IS NULL),
SUM(trim(state) = ''),
SUM(trim(state) LIKE '% %'),
SUM(state LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'city',
SUM(city IS NULL),
SUM(trim(city) = ''),
SUM(trim(city) LIKE '% %'),
SUM(city LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'postal_code',
SUM(postal_code IS NULL),
SUM(trim(postal_code) = ''),
SUM(trim(postal_code) LIKE '% %'),
SUM(postal_code LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'region',
SUM(region IS NULL),
SUM(trim(region) = ''),
SUM(trim(region) LIKE '% %'),
SUM(region LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'product_id',
SUM(product_id IS NULL),
SUM(trim(product_id) = ''),
SUM(trim(product_id) LIKE '% %'),
SUM(product_id LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'category',
SUM(category IS NULL),
SUM(trim(category) = ''),
SUM(trim(category) LIKE '% %'),
SUM(category LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'sub_category',
SUM(sub_category IS NULL),
SUM(trim(sub_category) = ''),
SUM(trim(sub_category) LIKE '% %'),
SUM(sub_category LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'product_name',
SUM(product_name IS NULL),
SUM(trim(product_name) = ''),
SUM(trim(product_name) LIKE '% %'),
SUM(product_name LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'sales',
SUM(sales IS NULL),
SUM(trim(sales) = ''),
SUM(trim(sales) LIKE '% %'),
SUM(sales LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'quantity',
SUM(quantity IS NULL),
SUM(trim(quantity) = ''),
SUM(trim(quantity) LIKE '% %'),
SUM(quantity LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'discount',
SUM(discount IS NULL),
SUM(trim(discount) = ''),
SUM(trim(discount) LIKE '% %'),
SUM(discount LIKE '%--%')
FROM clean_superstore_orders
UNION ALL
SELECT
'profit',
SUM(profit IS NULL),
SUM(trim(profit) = ''),
SUM(trim(profit) LIKE '% %'),
SUM(profit LIKE '%--%')
FROM clean_superstore_orders)
SELECT * FROM cek_issues
ORDER BY issue_null DESC, issue_blank DESC, issue_spaces DESC, issue_null DESC;
---| 16. INSERT PROBLEMATIC DATA
--- source_table : clean_superstore_orders
--- reason : SPACES
--- attribute : product_name
--- solution : replace spaces to a space
INSERT INTO audit_log
(row_id, attribute, reason, solution, source_table, total_rows)
SELECT
row_id,
'product_name',
'SPACES',
'replace spaces to a space',
'clean_superstore_orders',
(SELECT count(row_id) FROM clean_superstore_orders)
FROM clean_superstore_orders
GROUP BY row_id
HAVING product_name LIKE '% %'
ORDER BY row_id;
---| UPDATE DATA PROBLEMATIC (NULL, blank '', double space, strips)
---- tabel clean data
UPDATE clean_superstore_orders
SET product_name =
CASE WHEN product_name LIKE '% %' THEN REPLACE(product_name, ' ', ' ')
ELSE product_name END;
---/ CEK DATA PROBLEMATIC (duplikat based on all rows, kecuali row_id)
---- tabel data clean
---| Menghitung row duplikat (ada duplikat → total rows ≠ unique rows)
WITH
unique_records AS (
SELECT * FROM clean_superstore_orders
GROUP BY
order_id, order_date, ship_date, ship_mode, customer_id, country, state, city, postal_code, region, product_id, category, sub_category, product_name, sales, quantity, discount, profit),
count_unique_records AS (
SELECT COUNT(*) AS rows_unique
FROM unique_records),
count_all_records AS (
SELECT COUNT(*) AS rows_total
FROM clean_superstore_orders)
SELECT
rows_total,
rows_unique,
(rows_total - rows_unique) AS "total rows duplicate"
FROM count_unique_records, count_all_records;
---| Menampilkan row_id data yang duplikat (based on all rows, kecuali row_id)
WITH group_data_duplicate AS (
SELECT
group_concat(row_id) AS rows_duplicate,
count(row_id) AS total_duplicate,
clean_superstore_orders.*
FROM clean_superstore_orders
GROUP BY
order_id, order_date, ship_date, ship_mode, customer_id, country, state, city, postal_code, region, product_id, category, sub_category, product_name, sales, quantity, discount, profit
HAVING total_duplicate > 1)
SELECT
c.row_id,
d.total_duplicate,
d.rows_duplicate
FROM group_data_duplicate d
JOIN clean_superstore_orders c
ON
d.order_id = c.order_id AND
d.order_date = c.order_date AND
d.ship_date = c.ship_date AND
d.ship_mode = c.ship_mode AND
d.customer_id = c.customer_id AND
d.country = c.country AND
d.state = c.state AND
d.city = c.city AND
d.postal_code = c.postal_code AND
d.region = c.region AND
d.product_id = c.product_id AND
d.category = c.category AND
d.sub_category = c.sub_category AND
d.product_name = c.product_name AND
d.sales = c.sales AND
d.quantity = c.quantity AND
d.discount = c.discount AND
d.profit = c.profit
GROUP BY d.rows_duplicate
ORDER BY c.row_id ASC;
---/ Problem data yang terdeteksi:
-- 1. Ada 32 rows data yang duplikat
---/ INPUT KE LOG TABLE
---| 5. INSERT PROBLEMATIC DATA
--- source_table : clean_superstore_orders
--- reason : DUPLICATE DATA
--- attribute : ALL
--- solution : remove duplicate data
WITH group_unique_records AS (
SELECT * FROM clean_superstore_orders
GROUP BY
order_id, order_date, ship_date, ship_mode, customer_id, country, state, city, postal_code, region, product_id, category, sub_category, product_name, sales, quantity, discount, profit)
INSERT INTO audit_log
(row_id, attribute, reason, solution, source_table, total_rows)
SELECT
row_id, 'ALL', 'DUPLICATE DATA', 'remove duplicate data', 'clean_superstore_orders', (SELECT count(row_id) FROM clean_superstore_orders)
FROM clean_superstore_orders
GROUP BY row_id
HAVING row_id NOT IN (
SELECT row_id FROM group_unique_records);
---| UPDATE DATA PROBLEMATIC (remove duplicate data)
---- tabel data clean
WITH
unique_records AS (
SELECT * FROM clean_superstore_orders
GROUP BY
order_id, order_date, ship_date, ship_mode, customer_id, country, state, city, postal_code, region, product_id, category, sub_category, product_name, sales, quantity, discount, profit)
DELETE FROM clean_superstore_orders
WHERE row_id NOT IN (
SELECT row_id FROM unique_records);
---/ DATA FORMATTING PER KOLOM
---- FOKUS: cek dan fixing data per kolom agar tidak ada perbedaan format data (
---- misal:
---- 1. data order_id punya ukuran fix 16 karakter
---- 2. data category ada broken name (Tech, technologies, Furni, OfficeSupply)
---- 2. etc
----------------------------------------------------------------------------------------------
-------------------------------------- row_id ------------------------------------------------
----------------------------------------------------------------------------------------------
-- cek jumlah total dan selisih row_id berdasarkan length(row_id)
WITH RECURSIVE
full_numbers AS (
SELECT 1 AS nomor UNION ALL
SELECT nomor + 1
FROM full_numbers
WHERE nomor <= ( SELECT (max(row_id) -1) FROM clean_superstore_orders) ),
numbers AS (
SELECT DISTINCT
(length(nomor)) AS LEN_nomor,
count(nomor) AS jumlah_nomor
FROM full_numbers
GROUP BY LEN_nomor ),
row_id_table AS (
SELECT DISTINCT
(length(row_id)) AS LEN_db,
count(row_id) AS jumlah_db
FROM clean_superstore_orders
GROUP BY LEN_db )
SELECT
LEN_nomor AS LEN,
jumlah_nomor,
jumlah_db,
(jumlah_nomor - jumlah_db) AS selisih,
(SELECT sum(jumlah_nomor) FROM numbers ) AS total_nomor,
(SELECT sum(jumlah_db) FROM row_id_table ) AS total_db,
( (SELECT sum(jumlah_nomor) FROM numbers) - (SELECT sum(jumlah_db) FROM row_id_table) ) AS total_selisih
FROM numbers
JOIN row_id_table
ON LEN_nomor = LEN_db;
-- Cek uniqueness of row_id dan cek row_id yang hilang dan kenapa?
WITH RECURSIVE
full_numbers AS (
SELECT 1 AS nomor UNION ALL
SELECT nomor + 1
FROM full_numbers
WHERE nomor <= ( SELECT (max(row_id) -1) FROM clean_superstore_orders) ),
deleted_row_id AS (
SELECT nomor, row_id
FROM full_numbers
FULL OUTER JOIN clean_superstore_orders
ON nomor = row_id
WHERE nomor IS NULL OR row_id IS NULL)
SELECT
d.nomor,
d.row_id,
a.reason
FROM deleted_row_id d
JOIN audit_log a
ON d.nomor = a.row_id
GROUP BY d.nomor;
----------------------------------------------------------------------------------------------
-------------------------------------- order_id ----------------------------------------------
----------------------------------------------------------------------------------------------
SELECT DISTINCT order_id FROM clean_superstore_orders;
-- cek jumlah total dan selisih order_id berdasarkan length(order_id)
WITH
CEK_order_id AS (
SELECT
length('XX-NNNN-MMMMMM') AS LEN_real,
length(order_id) AS LEN_order_id,
count(order_id) AS jumlah
FROM clean_superstore_orders
GROUP BY LEN_order_id)
SELECT * FROM CEK_order_id;
-- cek konsistensi format order_id per huruf/kata
SELECT DISTINCT substr(order_id, 1, 2) AS country_code
FROM clean_superstore_orders; --cek kode negara
SELECT DISTINCT substr(order_id, 3, 1) AS strips1
FROM clean_superstore_orders; --cek strip pemisah 1
SELECT DISTINCT substr(order_id, 4, 4) AS year_code
FROM clean_superstore_orders; --cek kode tahun
SELECT DISTINCT substr(order_id, 8, 1) AS strips2
FROM clean_superstore_orders; --cek strip pemisah 2
SELECT DISTINCT substr(order_id, 9, 6) AS number_code
FROM clean_superstore_orders; --cek kode nomor
----------------------------------------------------------------------------------------------
-------------------------------------- order_date --------------------------------------------
----------------------------------------------------------------------------------------------
SELECT DISTINCT order_date FROM clean_superstore_orders;
-- cek jumlah total dan selisih order_date berdasarkan length(order_date)
WITH
CEK_order_date AS (
SELECT
length('YYYY-MM-DD') AS LEN_real,
length(order_date) AS LEN_order_date,
count(order_date) AS jumlah
FROM clean_superstore_orders
GROUP BY
LEN_order_date
)
SELECT *
FROM CEK_order_date;
-- cek konsistensi format order_date per huruf/kata
SELECT DISTINCT substr(order_date, 1, 4) AS year_code
FROM clean_superstore_orders
ORDER BY year_code; --cek kode tahun
SELECT DISTINCT substr(order_date, 5, 1) AS strips1
FROM clean_superstore_orders; --cek strip pemisah 1
SELECT DISTINCT substr(order_date, 6, 2) AS month_code
FROM clean_superstore_orders
ORDER BY month_code; --cek kode bulan
SELECT DISTINCT substr(order_date, 8, 1) AS strips2
FROM clean_superstore_orders; --cek strip pemisah 2
SELECT DISTINCT substr(order_date, 9, 2) AS day_code
FROM clean_superstore_orders
ORDER BY day_code; --cek kode hari
----------------------------------------------------------------------------------------------
-------------------------------------- ship_date ---------------------------------------------
----------------------------------------------------------------------------------------------
SELECT DISTINCT ship_date FROM clean_superstore_orders;
-- cek jumlah total dan selisih ship_date berdasarkan length(ship_date)
WITH
CEK_ship_date AS (
SELECT
length('YYYY-MM-DD') AS LEN_real,
length(ship_date) AS LEN_ship_date,
count(ship_date) AS jumlah
FROM clean_superstore_orders
GROUP BY LEN_ship_date )
SELECT * FROM CEK_ship_date;
-- cek konsistensi format ship_date per huruf/kata
SELECT DISTINCT substr(ship_date, 1, 4) AS year_code
FROM clean_superstore_orders