-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
68 lines (61 loc) · 2.29 KB
/
database.py
File metadata and controls
68 lines (61 loc) · 2.29 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
class Database:
def __init__(self, database_url):
try:
self.engine = create_engine(database_url)
self.connection = self.engine.connect()
print("[INFO] Database bağlantısı başarılı.")
except SQLAlchemyError as e:
print(f"[ERROR] Database bağlantı hatası: {str(e)}")
self.connection = None
def collect_data(self):
if self.connection is None:
raise Exception("Database bağlantısı başarısız. Veri çekilemiyor.")
query2 = """
SELECT
p.product_id,
AVG(od.unit_price) AS avg_unit_price,
COUNT(od.order_id) AS total_sales_count,
AVG(od.quantity) AS avg_quantity_per_order,
COUNT(DISTINCT o.customer_id) AS unique_customer_count
FROM
products p
INNER JOIN order_details od ON p.product_id = od.product_id
INNER JOIN orders o ON od.order_id = o.order_id
GROUP BY
p.product_id
"""
query3 = """
SELECT
suppliers.supplier_id,
suppliers.company_name,
products.product_id,
order_details.quantity,
order_details.unit_price,
order_details.order_id
FROM suppliers
JOIN products ON suppliers.supplier_id = products.supplier_id
JOIN order_details ON products.product_id = order_details.product_id
"""
query4 = """
SELECT
c.country,
COUNT(DISTINCT o.order_id) AS total_orders,
AVG(od.unit_price * od.quantity) AS avg_order_value,
AVG(order_item_count.item_count) AS avg_items_per_order
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN (
SELECT order_id, COUNT(*) AS item_count
FROM order_details
GROUP BY order_id
) AS order_item_count ON o.order_id = order_item_count.order_id
JOIN order_details od ON o.order_id = od.order_id
GROUP BY c.country
"""
df2 = pd.read_sql_query(query2, self.connection)
df3 = pd.read_sql_query(query3, self.connection)
df4 = pd.read_sql_query(query4, self.connection)
return df2, df3, df4