commit 46c52acc6f176a30e7b4beb9c8bb1e0003003137 Author: RobViren Date: Fri Mar 18 10:00:54 2022 -0500 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e4c958 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data.db diff --git a/convert.py b/convert.py new file mode 100644 index 0000000..f9222b0 --- /dev/null +++ b/convert.py @@ -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) \ No newline at end of file diff --git a/data.xlsx b/data.xlsx new file mode 100644 index 0000000..6f59ace Binary files /dev/null and b/data.xlsx differ diff --git a/get_top.py b/get_top.py new file mode 100644 index 0000000..401af30 --- /dev/null +++ b/get_top.py @@ -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) \ No newline at end of file