-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_finalize_and_export.sql
More file actions
47 lines (42 loc) · 1.12 KB
/
03_finalize_and_export.sql
File metadata and controls
47 lines (42 loc) · 1.12 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
-- Purpose: Rearrange columns for readability and export the cleaned dataset.
USE uk_db;
-- 1. Create a rearranged, final table
DROP TABLE IF EXISTS online_retail_final;
CREATE TABLE online_retail_final AS
SELECT
InvoiceNo,
InvoiceDate,
InvoiceYearMonth,
CustomerID,
Country,
StockCode,
Description,
Quantity,
UnitPrice,
TotalSpent
FROM online_retail;
-- 2. Swap tables
DROP TABLE online_retail;
RENAME TABLE online_retail_final TO online_retail;
-- 3. Export cleaned data with headers
-- Note: MySQL's SELECT ... INTO OUTFILE won't export column names.
-- We prepend a header row manually via UNION ALL.
(SELECT 'InvoiceNo','InvoiceDate','InvoiceYearMonth','CustomerID','Country',
'StockCode','Description','Quantity','UnitPrice','TotalSpent')
UNION ALL
SELECT
InvoiceNo,
DATE_FORMAT(InvoiceDate, '%Y-%m-%d %H:%i:%s'),
InvoiceYearMonth,
CustomerID,
Country,
StockCode,
Description,
Quantity,
UnitPrice,
TotalSpent
FROM online_retail
INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/online_retail_cleaned.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';