-
Notifications
You must be signed in to change notification settings - Fork 1
/
SQL.py
35 lines (32 loc) · 1015 Bytes
/
SQL.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
#############################
########## SQL + pandas ##########
from sqlalchemy import create_engine, text
import pandas as pd
# Create a connection using SQLAlchemy
connection_string = (
"mssql+pyodbc://@A01-DBAAS.local,12345/ESM_DBA_ABC?"
"driver=ODBC+Driver+17+for+SQL+Server&"
"trusted_connection=yes"
)
engine = create_engine(connection_string)
sql_query = """
WITH h AS (
SELECT [id]
,[entity]
,[country]
,[z]
,[report_date]
,[market_value_eur]
FROM [T1].[S1].[H2]
WHERE (entity = 'ABC' AND report_date > '2010-01-01' )
AND (country LIKE '%France%')
)
SELECT *
FROM h
LEFT JOIN (SELECT id, col1, col2 FROM [T1].[S1].[H3]) as o
ON h.[id] = o.[id];
"""
df = pd.read_sql(sql_query, engine)
print(df.head())
gdf = df.groupby(['entity','report_date']).agg({'market_value_eur':'sum'}).reset_index()
gdf["report_date"] = pd.to_datetime(gdf["report_date"], format='%Y-%m-%d')