-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base_info_adder.py
98 lines (87 loc) · 3.93 KB
/
Base_info_adder.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
87
88
89
90
91
92
93
94
95
96
97
98
import sqlite3 as sql
from Excel_reader1 import ListRow1
from Excel_reader1 import ListRow2
print("Інформацію додано в базу данних")
with sql.connect("Base_zvit1.db") as db:
curs = db.cursor()
curs.execute("DROP TABLE IF EXISTS customers")
curs.execute("""CREATE TABLE IF NOT EXISTS 'customers' (
'id' INTEGER NOT NULL UNIQUE,
'lastName',
'firstName',
'fathersName',
'phone_number',
'e_mail',
'city',
'company',
'doc_numb',
PRIMARY KEY('id' AUTOINCREMENT)
);""")
db.commit()
curs.execute("DROP TABLE IF EXISTS sales")
curs.execute("""CREATE TABLE IF NOT EXISTS 'sales' (
'id' INTEGER NOT NULL UNIQUE,
'customer',
'sale_sum',
'sale_date',
'doc_numb' TEXT,
'manager',
'company',
PRIMARY KEY('id' AUTOINCREMENT)
);""")
db.commit()
curs.execute("""CREATE VIEW IF NOT EXISTS tmp1 AS SELECT DISTINCT manager FROM sales""")
curs.executemany("""INSERT INTO customers (lastName,
firstName,
fathersName,
phone_number,
e_mail,
city,
company,
doc_numb)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", ListRow1)
db.commit()
curs.executemany("""INSERT INTO sales (customer,
sale_sum,
sale_date,
doc_numb,
manager,
company)
VALUES (?, ?, ?, ?, ?, ?)""", ListRow2)
rows = curs.execute("""SELECT manager, SUM(sale_sum) as SUMA FROM sales GROUP BY manager ORDER BY SUMA DESC""")
Zvitr_1 = []
while True:
next_row = curs.fetchone()
if next_row:
ZvitCell = []
Zvitr_1.append(list(next_row))
else:
break
print("Данні для звыту під назвою 'Сума продаж ВЗ за весь час' успішно сформовані")
# for i in Zvitr_1:
# print(i)
rows2 = curs.execute("""SELECT DISTINCT tmp1.manager, IFNULL(sum(s.sale_sum) OVER (PARTITION BY tmp1.manager), 0) AS Sales_suma FROM tmp1 LEFT JOIN (SELECT Sale_sum, manager FROM sales WHERE strftime('%Y', sale_date) = '2020') AS s ON s.manager = tmp1.manager ORDER BY Sales_suma DESC""")
Zvitr_2 = []
while True:
next_row2 = curs.fetchone()
if next_row2:
ZvitCell2 = []
Zvitr_2.append(list(next_row2))
else:
break
print("Данні для звыту під назвою 'Сума продаж ВЗ за 2020 рік' успішно сформовані")
# for i in Zvitr_2:
# print(i)
rows3 = curs.execute("""SELECT city, SUM(sale_sum) as Sales_suma FROM sales s JOIN customers c ON s.company = c.company GROUP BY City ORDER BY Sales_suma DESC""")
Zvitr_3 = []
while True:
next_row3 = curs.fetchone()
if next_row3:
ZvitCell3 = []
Zvitr_3.append(list(next_row3))
else:
break
print("Данні для звыту під назвою 'Рейтинг міст по сумі збуту за весь час' успішно сформовані")
for i in Zvitr_3:
print(i)
db.commit()