This commit is contained in:
2022-03-18 10:00:54 -05:00
commit 46c52acc6f
4 changed files with 125 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
data.db

49
convert.py Normal file
View File

@@ -0,0 +1,49 @@
import pandas as pd
import numpy as np
import sqlite3
import time
start = time.time()
data = pd.read_excel("data.xlsx")
df = pd.DataFrame()
years = []
months = []
level2s = []
level3s = []
skus = []
quantities = []
revenues = []
costs = []
descriptions = []
for index,row in data.iterrows():
if type(row["FiscalYearMonth"]) == str:
years.append(int(row["FiscalYearMonth"][0:4]))
months.append(int(row["FiscalYearMonth"][5:]))
level2s.append(row["Level2"])
level3s.append(row["Level3"])
skus.append(row["MaterialEntered"])
quantities.append(int(row["Quantity"]))
revenues.append(row["SalesRevenue"])
costs.append(row["CostOfGoodsSold"])
descriptions.append(row["ProductDescription"])
df["year"] = years
df["month"] = months
df["level2"] = level2s
df["level3"] = level3s
df["sku"] = skus
df["quantity"] = quantities
df["revenue"] = revenues
df["cost"] = costs
df["description"] = descriptions
conn = sqlite3.connect("data.db")
df.to_sql("data", conn, if_exists='replace')
end = time.time()
print(end-start)

BIN
data.xlsx Normal file

Binary file not shown.

75
get_top.py Normal file
View File

@@ -0,0 +1,75 @@
get_top = """
SELECT
description,
sku,
level2,
level3,
rev,
qty,
tren
from
(
SELECT
*,
ROW_NUMBER () OVER (
PARTITION BY level2
ORDER BY
SUM(revenue) DESC
) rownum,
SUM(revenue) OVER (PARTITION BY level2) total,
ROUND(SUM(revenue), 2) as rev,
SUM(quantity) as qty,
tren
FROM
(
select
*,
cast(past as float) / 3 as past,
round(
cast(present as float) /(cast(past as float) / 3.0),
2
) as tren
from
(
select
*,
sum(quantity) FILTER (
WHERE
year = {0}
and month between {1}
and {2}
) over (PARTITION BY description) as past,
sum(quantity) FILTER (
WHERE
year = {3}
and month = {4}
) over (PARTITION BY description) as present
from
data
)
)
where
year = {3}
and month = {4}
GROUP BY
description
ORDER BY
total DESC
)
WHERE
rownum <= 10
and revenue > 0
order by
(sum(revenue) over (
partition by level2
)) desc;
""".format(2022,1,2,2022,3)
import sqlite3
conn = sqlite3.connect("data.db")
res = conn.execute(get_top)
print(res.description)
for row in res.fetchall():
print(row)