diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21d0b89 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..b5b3c43 --- /dev/null +++ b/main.py @@ -0,0 +1,63 @@ +import os +import pandas as pd +from sqlalchemy import create_engine, text + + +def get_engine(): + user = os.getenv("MYSQL_USER", "root") + password = os.getenv("MYSQL_PASSWORD","Kalamarhs1997") + host = os.getenv("MYSQL_HOST", "127.0.0.1") + port = os.getenv("MYSQL_PORT", "3306") + db = os.getenv("MYSQL_DB", "sakila") + + if not password: + raise ValueError("MYSQL_PASSWORD not set") + + url = f"mysql+pymysql://{user}:{password}@{host}:{port}/{db}" + return create_engine(url) + + +def rentals_month(engine, month, year): + query = text(""" + SELECT rental_id, rental_date, inventory_id, customer_id, return_date, staff_id + FROM rental + WHERE YEAR(rental_date) = :year + AND MONTH(rental_date) = :month + """) + return pd.read_sql(query, engine, params={"year": year, "month": month}) + + +def rental_count_month(df, month, year): + col = f"rentals_{month:02d}_{year}" + return df.groupby("customer_id").size().reset_index(name=col) + + +def compare_rentals(df1, df2): + col1 = [c for c in df1.columns if c.startswith("rentals_")][0] + col2 = [c for c in df2.columns if c.startswith("rentals_")][0] + + merged = df1.merge(df2, on="customer_id", how="inner") + merged["difference"] = merged[col2] - merged[col1] + + return merged.sort_values("difference", ascending=False) + + +def main(): + engine = get_engine() + + may = rentals_month(engine, 5, 2005) + june = rentals_month(engine, 6, 2005) + + may_counts = rental_count_month(may, 5, 2005) + june_counts = rental_count_month(june, 6, 2005) + + comparison = compare_rentals(may_counts, june_counts) + + print(comparison.head(20)) + + comparison.to_csv("may_vs_june_rentals.csv", index=False) + print("Saved to may_vs_june_rentals.csv") + + +if __name__ == "__main__": + main() diff --git a/may_vs_june_rentals.csv b/may_vs_june_rentals.csv new file mode 100644 index 0000000..1465247 --- /dev/null +++ b/may_vs_june_rentals.csv @@ -0,0 +1,513 @@ +customer_id,rentals_05_2005,rentals_06_2005,difference +454,1,10,9 +457,1,9,8 +213,1,9,8 +295,1,9,8 +27,1,8,7 +234,1,8,7 +260,1,8,7 +380,1,8,7 +561,2,9,7 +406,1,7,6 +29,1,7,6 +86,1,7,6 +434,1,7,6 +431,1,7,6 +459,1,7,6 +66,1,7,6 +450,1,7,6 +456,1,7,6 +338,1,7,6 +187,1,7,6 +277,1,7,6 +200,1,7,6 +526,3,9,6 +171,1,7,6 +267,3,9,6 +570,1,7,6 +149,1,7,6 +322,2,7,5 +62,2,7,5 +236,3,8,5 +146,2,7,5 +98,1,6,5 +116,1,6,5 +299,2,7,5 +358,1,6,5 +107,2,7,5 +343,2,7,5 +310,1,6,5 +111,1,6,5 +122,2,7,5 +327,1,6,5 +1,2,7,5 +491,2,7,5 +576,2,7,5 +26,2,7,5 +514,1,6,5 +510,3,8,5 +550,1,6,5 +363,1,5,4 +362,1,5,4 +35,2,6,4 +25,2,6,4 +352,1,5,4 +364,1,5,4 +515,2,6,4 +339,1,5,4 +113,2,6,4 +383,2,6,4 +155,1,5,4 +466,1,5,4 +386,1,5,4 +571,2,6,4 +328,1,5,4 +516,2,6,4 +120,2,6,4 +517,1,5,4 +123,1,5,4 +10,1,5,4 +316,3,7,4 +12,2,6,4 +308,1,5,4 +130,2,6,4 +148,1,5,4 +144,2,6,4 +293,3,7,4 +186,2,6,4 +290,1,5,4 +574,1,5,4 +193,2,6,4 +46,3,7,4 +452,3,7,4 +479,2,6,4 +45,1,5,4 +196,4,8,4 +444,2,6,4 +590,1,5,4 +230,2,6,4 +191,2,6,4 +430,2,6,4 +428,1,5,4 +76,2,6,4 +427,1,5,4 +426,1,5,4 +208,1,5,4 +405,2,6,4 +215,2,6,4 +85,2,6,4 +216,1,5,4 +403,2,6,4 +221,1,5,4 +471,1,5,4 +189,1,5,4 +451,3,7,4 +275,1,4,3 +543,2,5,3 +190,2,5,3 +278,1,4,3 +203,1,4,3 +273,2,5,3 +194,2,5,3 +262,1,4,3 +588,2,5,3 +254,2,5,3 +253,3,6,3 +242,3,6,3 +241,2,5,3 +279,2,5,3 +573,1,4,3 +237,2,5,3 +330,2,5,3 +292,1,4,3 +438,3,6,3 +393,2,5,3 +497,1,4,3 +404,1,4,3 +493,1,4,3 +422,1,4,3 +492,1,4,3 +439,3,6,3 +388,2,5,3 +446,3,6,3 +448,2,5,3 +481,1,4,3 +477,1,4,3 +467,1,4,3 +463,1,4,3 +390,2,5,3 +382,2,5,3 +537,1,4,3 +319,3,6,3 +297,2,5,3 +298,1,4,3 +534,2,5,3 +305,1,4,3 +533,3,6,3 +318,1,4,3 +522,1,4,3 +498,3,6,3 +465,1,4,3 +333,1,4,3 +349,1,4,3 +365,2,5,3 +378,1,4,3 +499,1,4,3 +384,4,7,3 +599,1,4,3 +108,2,5,3 +24,2,5,3 +75,2,5,3 +125,1,4,3 +57,2,5,3 +52,1,4,3 +82,2,5,3 +84,3,6,3 +166,1,4,3 +87,2,5,3 +137,1,4,3 +42,1,4,3 +40,1,4,3 +91,1,4,3 +74,1,4,3 +143,2,5,3 +94,2,5,3 +183,1,4,3 +100,1,4,3 +181,1,4,3 +151,2,5,3 +95,1,4,3 +173,2,5,3 +176,5,8,3 +28,2,5,3 +147,2,4,2 +55,3,5,2 +447,2,4,2 +495,2,4,2 +309,2,4,2 +455,2,4,2 +307,4,6,2 +500,3,5,2 +489,2,4,2 +476,2,4,2 +505,2,4,2 +67,2,4,2 +133,2,4,2 +141,1,3,2 +483,1,3,2 +470,1,3,2 +472,4,6,2 +88,1,3,2 +416,3,5,2 +317,1,3,2 +424,1,3,2 +368,4,6,2 +381,3,5,2 +103,2,4,2 +104,2,4,2 +361,2,4,2 +360,2,4,2 +387,4,6,2 +356,1,3,2 +389,1,3,2 +89,3,5,2 +353,1,3,2 +351,1,3,2 +350,2,4,2 +396,1,3,2 +398,1,3,2 +345,2,4,2 +402,1,3,2 +112,2,4,2 +118,1,3,2 +325,1,3,2 +270,2,4,2 +420,1,3,2 +81,1,3,2 +511,2,4,2 +280,1,3,2 +513,1,3,2 +243,1,3,2 +538,2,4,2 +544,2,4,2 +546,2,4,2 +162,3,5,2 +553,1,3,2 +554,2,4,2 +556,3,5,2 +228,2,4,2 +227,2,4,2 +566,2,4,2 +11,1,3,2 +572,1,3,2 +8,1,3,2 +577,1,3,2 +578,1,3,2 +582,1,3,2 +587,2,4,2 +5,3,5,2 +204,4,6,2 +589,3,5,2 +201,3,5,2 +199,1,3,2 +3,2,4,2 +593,2,4,2 +174,2,4,2 +594,4,6,2 +182,2,4,2 +158,2,4,2 +164,1,3,2 +529,1,3,2 +265,1,3,2 +21,3,5,2 +156,2,4,2 +523,2,4,2 +23,3,5,2 +261,4,6,2 +530,1,3,2 +256,5,7,2 +266,2,4,2 +519,1,3,2 +520,1,3,2 +518,1,2,1 +581,2,3,1 +486,3,4,1 +484,3,4,1 +6,3,4,1 +397,1,2,1 +485,1,2,1 +20,3,4,1 +374,3,4,1 +92,2,3,1 +385,2,3,1 +37,2,3,1 +32,3,4,1 +595,1,2,1 +597,2,3,1 +99,1,2,1 +379,2,3,1 +507,2,3,1 +49,4,5,1 +562,3,4,1 +565,2,3,1 +33,1,2,1 +461,1,2,1 +462,2,3,1 +18,3,4,1 +54,3,4,1 +539,2,3,1 +442,3,4,1 +441,1,2,1 +71,2,3,1 +532,1,2,1 +468,5,6,1 +552,1,2,1 +421,2,3,1 +536,2,3,1 +473,3,4,1 +474,1,2,1 +415,1,2,1 +414,2,3,1 +524,2,3,1 +411,2,3,1 +409,1,2,1 +407,1,2,1 +557,2,3,1 +83,3,4,1 +560,1,2,1 +376,1,2,1 +303,3,4,1 +286,1,2,1 +114,3,4,1 +334,2,3,1 +223,1,2,1 +331,2,3,1 +165,1,2,1 +326,3,4,1 +324,1,2,1 +238,2,3,1 +121,1,2,1 +244,2,3,1 +249,4,5,1 +252,2,3,1 +259,3,4,1 +276,2,3,1 +145,1,2,1 +282,3,4,1 +127,2,3,1 +373,1,2,1 +287,3,4,1 +138,2,3,1 +294,1,2,1 +135,2,3,1 +219,1,2,1 +304,3,4,1 +217,2,3,1 +210,1,2,1 +372,2,3,1 +371,6,7,1 +180,1,2,1 +366,1,2,1 +359,3,4,1 +344,2,3,1 +357,3,4,1 +355,1,2,1 +354,3,4,1 +209,3,4,1 +348,2,3,1 +211,1,2,1 +346,2,3,1 +47,3,3,0 +494,1,1,0 +172,3,3,0 +284,3,3,0 +197,8,8,0 +192,1,1,0 +291,3,3,0 +7,5,5,0 +586,2,2,0 +563,2,2,0 +136,1,1,0 +167,3,3,0 +134,4,4,0 +301,3,3,0 +464,2,2,0 +281,2,2,0 +2,1,1,0 +580,1,1,0 +501,2,2,0 +17,3,3,0 +542,2,2,0 +541,2,2,0 +240,3,3,0 +169,2,2,0 +246,5,5,0 +248,2,2,0 +16,4,4,0 +525,1,1,0 +235,2,2,0 +232,2,2,0 +154,3,3,0 +263,2,2,0 +271,1,1,0 +509,2,2,0 +548,3,3,0 +185,3,3,0 +69,2,2,0 +391,5,5,0 +115,3,3,0 +314,2,2,0 +117,2,2,0 +443,1,1,0 +73,2,2,0 +413,2,2,0 +119,3,3,0 +419,1,1,0 +65,2,2,0 +449,3,3,0 +323,3,3,0 +437,3,3,0 +70,1,1,0 +408,3,3,0 +106,2,2,0 +59,3,3,0 +58,3,3,0 +128,2,2,0 +425,1,1,0 +306,2,2,0 +56,4,4,0 +435,2,2,0 +77,5,5,0 +432,3,3,0 +321,3,2,-1 +545,2,1,-1 +79,3,2,-1 +531,2,1,-1 +429,5,4,-1 +239,6,5,-1 +302,2,1,-1 +225,2,1,-1 +184,3,2,-1 +367,2,1,-1 +179,3,2,-1 +102,3,2,-1 +93,3,2,-1 +170,3,2,-1 +105,4,3,-1 +584,3,2,-1 +394,2,1,-1 +564,2,1,-1 +399,3,2,-1 +168,2,1,-1 +400,4,3,-1 +9,3,2,-1 +110,2,1,-1 +337,3,2,-1 +220,2,1,-1 +315,2,1,-1 +375,4,3,-1 +36,2,1,-1 +482,3,2,-1 +142,3,2,-1 +496,2,1,-1 +503,4,3,-1 +504,3,2,-1 +43,2,1,-1 +44,5,4,-1 +490,2,1,-1 +64,3,2,-1 +150,3,2,-1 +508,2,1,-1 +313,4,3,-1 +272,4,3,-1 +60,3,2,-1 +300,3,2,-1 +53,6,5,-1 +157,2,1,-1 +257,3,2,-1 +131,3,2,-1 +51,3,2,-1 +50,5,4,-1 +152,2,1,-1 +296,4,3,-1 +48,3,2,-1 +480,3,2,-1 +502,4,3,-1 +311,5,3,-2 +109,7,5,-2 +575,5,3,-2 +312,4,2,-2 +214,5,3,-2 +22,3,1,-2 +159,4,2,-2 +417,3,1,-2 +247,3,1,-2 +72,3,1,-2 +535,4,2,-2 +436,3,1,-2 +506,7,5,-2 +547,3,1,-2 +126,3,1,-2 +231,3,1,-2 +569,5,2,-3 +412,4,1,-3 +369,4,1,-3 +528,5,2,-3 +460,4,1,-3 +222,5,2,-3 +469,5,2,-3 +19,6,3,-3 +245,6,3,-3 +269,6,3,-3 +551,5,2,-3 +124,4,1,-3 +549,4,1,-3 +288,4,1,-3 +251,6,3,-3 +161,6,2,-4 +14,5,1,-4 +198,5,1,-4 +274,6,2,-4 +250,5,1,-4 +596,6,2,-4 +207,6,1,-5