-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecuteSQLQueriesSQLite.py
87 lines (79 loc) · 1.72 KB
/
ExecuteSQLQueriesSQLite.py
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# Define a function to execute SQL queries and return results as pandas DataFrame
def query_data(query, connection = sqlite3.connect('wwi.db')):
return pd.read_sql_query(query, connection)
wwi = sqlite3.connect('wwi.db')
# Create SQL queries for all the data used in the project
result = query_data("""
SELECT
c.CUSTOMERNAME,
strftime('%Y', i.INVOICEDATE) AS TRANSACTIONYEAR,
strftime('%m', i.INVOICEDATE) AS TRANSACTIONMONTH,
SUM(l.LINEPROFIT) AS PROFITPERMONTH,
SUM(l.TAXAMOUNT) AS TAXPERMONTH
FROM
INVOICELINES l
JOIN
INVOICES i
ON
l.INVOICEID = i.INVOICEID
JOIN
CUSTOMERS c
ON
i.CUSTOMERID = c.CUSTOMERID
GROUP BY
strftime('%Y', i.INVOICEDATE),
strftime('%m', i.INVOICEDATE),
c.CUSTOMERNAME
ORDER BY
strftime('%Y', i.INVOICEDATE),
strftime('%m', i.INVOICEDATE),
c.CUSTOMERNAME
;
""", wwi)
employees = query_data("""
SELECT
p.FULLNAME,
strftime('%Y', o.ORDERDATE) AS ORDER_YEAR,
strftime('%m', o.ORDERDATE) AS ORDER_MONTH,
SUM(l.QUANTITY) AS Total_Quantity
FROM
ORDERLINES l
JOIN
ORDERS o
ON
l.ORDERID = o.ORDERID
JOIN
PEOPLE p
ON
o.SALESPERSONPERSONID = p.PERSONID
GROUP BY
p.FULLNAME,
strftime('%Y', o.ORDERDATE),
strftime('%m', o.ORDERDATE)
;
""", wwi)
invoices = query_data("""
SELECT
p.FULLNAME,
strftime('%Y', o.INVOICEDATE) AS ORDER_YEAR,
strftime('%m', o.INVOICEDATE) AS ORDER_MONTH,
SUM(l.UNITPRICE) AS InvoiceTotal
FROM
INVOICELINES l
JOIN
INVOICES o
ON
l.INVOICEID = o.INVOICEID
JOIN
PEOPLE p
ON
o.SALESPERSONPERSONID = p.PERSONID
GROUP BY
p.FULLNAME,
strftime('%Y', o.INVOICEDATE),
strftime('%m', o.INVOICEDATE)
;
""", wwi)