Commit 6cdd053ccb

6cdd053ccbafe5c479ae379448a85569bccdf19d

parent: 836841dba8

Unsigned

cmc <hello@cleberg.net> · 2023-06-16 21:43 UTC

add basic sqlite3 analysis script

Layout: unified · split

projects/sqlite3-analysis/README.md added +26
@@ -0,0 +1,26 @@
1## Instructions
2
3```bash
4# Open the database:
5sqlite3 ./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
18cat ./employee_sales.csv
19```
20
21```csv
22EmployeeId,LastName,FirstName,Title,CustomerId,SupportRepId,State,Country,InvoiceId,CustomerId,InvoiceDate,InvoiceId,UnitPrice,Quantity,TotalPrice
233,Peacock,Jane,"Sales Support Agent",1,3,SP,Brazil,98,1,"2010-03-11 00:00:00",98,1.99,1,1.99
244,Park,Margaret,"Sales Support Agent",4,4,,Norway,2,4,"2009-01-02 00:00:00",2,0.99,1,0.99
255,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 @@
1EmployeeId,LastName,FirstName,Title,CustomerId,SupportRepId,State,Country,InvoiceId,CustomerId,InvoiceDate,InvoiceId,UnitPrice,Quantity,TotalPrice
23,Peacock,Jane,"Sales Support Agent",1,3,SP,Brazil,98,1,"2010-03-11 00:00:00",98,1.99,1,1.99
34,Park,Margaret,"Sales Support Agent",4,4,,Norway,2,4,"2009-01-02 00:00:00",2,0.99,1,0.99
45,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
4ALTER TABLE invoice_items
5ADD COLUMN TotalPrice
6GENERATED ALWAYS AS (UnitPrice * Quantity);
7
8-- Extract and summarize data to show sales totals per employee
9WITH cst AS (
10 SELECT
11 CustomerId
12 ,SupportRepId
13 ,State
14 ,Country
15 FROM customers
16),
17emp AS (
18 SELECT
19 EmployeeId
20 ,LastName
21 ,FirstName
22 ,Title
23 FROM employees
24),
25inv AS (
26 SELECT
27 InvoiceId
28 ,CustomerId
29 ,InvoiceDate
30 FROM invoices
31),
32tot AS (
33 SELECT
34 InvoiceId
35 ,UnitPrice
36 ,Quantity
37 ,TotalPrice
38 FROM invoice_items
39)
40
41SELECT DISTINCT * FROM emp
42LEFT JOIN cst ON emp.EmployeeId = cst.SupportRepId
43LEFT JOIN inv ON cst.CustomerId = inv.CustomerId
44LEFT JOIN tot ON inv.InvoiceId = tot.InvoiceId
45WHERE inv.InvoiceDate <= date()
46GROUP BY EmployeeId;
47
48-- Drop col used for calculations
49ALTER TABLE invoice_items DROP TotalPrice;