Commit 6cdd053ccb
Unsigned
Layout: unified · split
projects/sqlite3-analysis/README.md added +26
| @@ -0,0 +1,26 @@ | |||
| 1 | ## Instructions | ||
| 2 | |||
| 3 | ```bash | ||
| 4 | # Open the database: | ||
| 5 | sqlite3 ./chinook.db | ||
| 6 | ``` | ||
| 7 | |||
| 8 | ```sql | ||
| 9 | # Enable some features and export the CSV results of the script | ||
| 10 | .headers on | ||
| 11 | .mode csv | ||
| 12 | .output /home/cmc/git/data-science/projects/sql-dashboard/employee_sales.csv | ||
| 13 | .read employee_sales.sql | ||
| 14 | ``` | ||
| 15 | |||
| 16 | ```bash | ||
| 17 | # Inspect the results | ||
| 18 | cat ./employee_sales.csv | ||
| 19 | ``` | ||
| 20 | |||
| 21 | ```csv | ||
| 22 | EmployeeId,LastName,FirstName,Title,CustomerId,SupportRepId,State,Country,InvoiceId,CustomerId,InvoiceDate,InvoiceId,UnitPrice,Quantity,TotalPrice | ||
| 23 | 3,Peacock,Jane,"Sales Support Agent",1,3,SP,Brazil,98,1,"2010-03-11 00:00:00",98,1.99,1,1.99 | ||
| 24 | 4,Park,Margaret,"Sales Support Agent",4,4,,Norway,2,4,"2009-01-02 00:00:00",2,0.99,1,0.99 | ||
| 25 | 5,Johnson,Steve,"Sales Support Agent",2,5,,Germany,1,2,"2009-01-01 00:00:00",1,0.99,1,0.99 | ||
| 26 | ``` | ||
projects/sqlite3-analysis/chinook.db added
Binary file not shown.
projects/sqlite3-analysis/employee_sales.csv added +4
| @@ -0,0 +1,4 @@ | |||
| 1 | EmployeeId,LastName,FirstName,Title,CustomerId,SupportRepId,State,Country,InvoiceId,CustomerId,InvoiceDate,InvoiceId,UnitPrice,Quantity,TotalPrice | ||
| 2 | 3,Peacock,Jane,"Sales Support Agent",1,3,SP,Brazil,98,1,"2010-03-11 00:00:00",98,1.99,1,1.99 | ||
| 3 | 4,Park,Margaret,"Sales Support Agent",4,4,,Norway,2,4,"2009-01-02 00:00:00",2,0.99,1,0.99 | ||
| 4 | 5,Johnson,Steve,"Sales Support Agent",2,5,,Germany,1,2,"2009-01-01 00:00:00",1,0.99,1,0.99 | ||
projects/sqlite3-analysis/employee_sales.sql added +49
| @@ -0,0 +1,49 @@ | |||
| 1 | -- Data ETL business script in sqlite3 | ||
| 2 | |||
| 3 | -- Create new field(s) for business-requested calculations | ||
| 4 | ALTER TABLE invoice_items | ||
| 5 | ADD COLUMN TotalPrice | ||
| 6 | GENERATED ALWAYS AS (UnitPrice * Quantity); | ||
| 7 | |||
| 8 | -- Extract and summarize data to show sales totals per employee | ||
| 9 | WITH cst AS ( | ||
| 10 | SELECT | ||
| 11 | CustomerId | ||
| 12 | ,SupportRepId | ||
| 13 | ,State | ||
| 14 | ,Country | ||
| 15 | FROM customers | ||
| 16 | ), | ||
| 17 | emp AS ( | ||
| 18 | SELECT | ||
| 19 | EmployeeId | ||
| 20 | ,LastName | ||
| 21 | ,FirstName | ||
| 22 | ,Title | ||
| 23 | FROM employees | ||
| 24 | ), | ||
| 25 | inv AS ( | ||
| 26 | SELECT | ||
| 27 | InvoiceId | ||
| 28 | ,CustomerId | ||
| 29 | ,InvoiceDate | ||
| 30 | FROM invoices | ||
| 31 | ), | ||
| 32 | tot AS ( | ||
| 33 | SELECT | ||
| 34 | InvoiceId | ||
| 35 | ,UnitPrice | ||
| 36 | ,Quantity | ||
| 37 | ,TotalPrice | ||
| 38 | FROM invoice_items | ||
| 39 | ) | ||
| 40 | |||
| 41 | SELECT DISTINCT * FROM emp | ||
| 42 | LEFT JOIN cst ON emp.EmployeeId = cst.SupportRepId | ||
| 43 | LEFT JOIN inv ON cst.CustomerId = inv.CustomerId | ||
| 44 | LEFT JOIN tot ON inv.InvoiceId = tot.InvoiceId | ||
| 45 | WHERE inv.InvoiceDate <= date() | ||
| 46 | GROUP BY EmployeeId; | ||
| 47 | |||
| 48 | -- Drop col used for calculations | ||
| 49 | ALTER TABLE invoice_items DROP TotalPrice; | ||