Discount for repeated for rows - python

User creates one plan to purchase some items on N (five) diff dates.
+--------------+---------+------------+
| plan_date_id | plan_id | ad_date |
+--------------+---------+------------+
| 1 | 1 | 2015-09-13 |
| 2 | 1 | 2015-09-15 |
| 3 | 1 | 2015-09-17 |
| 4 | 1 | 2015-09-21 |
| 5 | 1 | 2015-09-24 |
+--------------+---------+------------+
Week Span: For each product week span will be calculated based on the date on which the product was sold for the first time + 6 to date part.
i.e., for product_ID 10, first purchase was made on 2015-09-13, so
the week span will be from 2015-09-13 to 2015-09-19(2015-09-13 + 6).
Discount logic for a product: (Total No. of Repetition in a Week Span - 1) * 10%.
But maximum discount can be 30%.
+---------------+--------------+-------------+
| plan_product_id | plan_date_id | product_id |
+-----------------+--------------+------------+
| 1 | 1 | 10 |
| 2 | 2 | 5715 |
| 3 | 2 | 10 |
| 4 | 3 | 10 |
| 5 | 3 | 128900 |
| 6 | 4 | 10 |
| 7 | 5 | 10 |
+-----------------+--------------+------------+
So in my example I want discount as follow.
+---------------+--------------+-------------+------------+
| plan_product_id | plan_date_id | product_id | discount |
+-----------------+--------------+------------+------------+
| 1 | 1 | 10 | 0% |
| 2 | 2 | 5715 | 0% |
| 3 | 2 | 10 | 10% |
| 4 | 3 | 10 | 20% |
| 5 | 3 | 128900 | 0% |
| 6 | 4 | 10 | 0% |
| 7 | 5 | 10 | 0% |
+-----------------+--------------+------------+------------+
Please Note there will be 0% discount in plan_product_id 6 and 7
Currently, I am doing discount calculation in python.
First get all required records. Then create a dict with product_id as key,
in value there is another dict holding base date and repeated times in a week. Then loop to all record.
What will be the best way to do it?
Is it possible to do it only from MySQL or Django Orm?
Will looping in MySQL be more performance efficient?

Related

How can i convert 31 colums (refering to the days of month) into a single datetime column?

I have this:
Febuary_Sells
(31 columns refering to the days of month)
Store | Product | 1 | 2 | 3 | 4 | 5 | ... | 31 |
Store 1 | Iphone | 0 | 3 | 1 | 3 | 2 | ... | 0 |
Store 1 | 4k TV | 1 | 4 | 2 | 3 | 0 | ... | 0 |
And i want to have something like this:
Store | Product | Date | Quantity |
Store 1 | Iphone | 01/02/2022 | 0 |
Store 1 | 4k TV | 01/02/2022 | 1 |
Store 1 | Iphone | 02/02/2022 | 3 |
Store 1 | 4k TV | 02/02/2022 | 4 |
I just want to get rid of the 31 columns and transform it into a datetime column (also keep the quantity selled of each item)
I really don't know how can i solve this problem...
For some reason, i can't put images on my question

sqlite3 in Python: How to average the values in a column based on a filter that works on 2 other columns?

Below is a sample data I'm testing on:
---------------------------------------
| id1 | id2 | cnt | avg_score |
---------------------------------------
| 2 | 3 | 3 | 10 |
---------------------------------------
| 3 | 4 | 3 | 11 |
---------------------------------------
| 2 | 5 | 3 | 12 |
---------------------------------------
| 4 | 5 | 3 | 13 |
---------------------------------------
This is a data that details how many times 2 ingredients have been used together and how well the dish has turned out.
I am interested in calculating the average of the avg_score of each ingredient, no matter if they're in column id1 or id2.
The output should be as follows:
--------------------------
| id | avg_avg_score |
--------------------------
| 2 | 11 |
--------------------------
| 3 | 10.5 |
--------------------------
| 4 | 12 |
--------------------------
| 5 | 12.5 |
--------------------------
Ingredient 3 appears in the first and second row, so the avg_avg_score is (10+11)/2.
I'm using sqlite3 in python.

How to get the column values of a Dataframe into another dataframe as a new column after matching the values in columns that both dataframes have?

I'm trying to create a new column in a DataFrame and storing it with values stored in a different dataframe by first comparing the values of columns that both dataframes have. For example:
df1 >>>
| name | team | week | dates | interceptions | pass_yds | rating |
| ---- | ---- | -----| ---------- | ------------- | --------- | -------- |
| maho | KC | 1 | 2020-09-10 | 0 | 300 | 105 |
| went | PHI | 1 | 2020-09-13 | 2 | 225 | 74 |
| lock | DEN | 1 | 2020-09-14 | 0 | 150 | 89 |
| dris | DEN | 2 | 2020-09-20 | 1 | 220 | 95 |
| went | PHI | 2 | 2020-09-20 | 2 | 250 | 64 |
| maho | KC | 2 | 2020-09-21 | 1 | 245 | 101 |
df2 >>>
| name | team | week | catches | rec_yds | rec_tds |
| ---- | ---- | -----| ------- | ------- | ------- |
| ertz | PHI | 1 | 5 | 58 | 1 |
| fant | DEN | 2 | 6 | 79 | 0 |
| kelc | KC | 2 | 8 | 105 | 1 |
| fant | DEN | 1 | 3 | 29 | 0 |
| kelc | KC | 1 | 6 | 71 | 1 |
| ertz | PHI | 2 | 7 | 91 | 2 |
| goed | PHI | 2 | 2 | 15 | 0 |
I want to create a dates column in df2 with the values of the dates stored in the dates column in df1 after matching the teams and the weeks columns. After the matching, df2 in this example should look something like this:
df2 >>>
| name | team | week | catches | rec_yds | rec_tds | dates |
| ---- | ---- | -----| ------- | ------- | ------- | ---------- |
| ertz | PHI | 1 | 5 | 58 | 1 | 2020-09-13 |
| fant | DEN | 2 | 6 | 79 | 0 | 2020-09-20 |
| kelc | KC | 2 | 8 | 105 | 1 | 2020-09-20 |
| fant | DEN | 1 | 3 | 29 | 0 | 2020-09-14 |
| kelc | KC | 1 | 6 | 71 | 1 | 2020-09-10 |
| ertz | PHI | 2 | 7 | 91 | 2 | 2020-09-20 |
| goed | PHI | 2 | 2 | 15 | 0 | 2020-09-20 |
I'm looking for an optimal solution. I've already tried nested for loops and comparing the week and team columns from both dataframes together but that hasn't worked. At this point I'm all out of ideas. Please help!
Disclaimer: The actual DataFrames I'm working with are a lot larger. They have a lot more rows, columns, and values (i.e. a lot more teams in the team columns, a lot more dates in the dates columns, and a lot more weeks in the week columns)

How to create a table resulting from joining of two or more table with this structure?

Lets say I have two tables with the following structure and same values-
+-----------+-----------+---------+-------+--------+---------+--------+---------+
| TEACHER | STUDENT | CLASS | SEC | HB_a | VHB_b | HG_c | VHG_d |
|-----------+-----------+---------+-------+--------+---------+--------+---------|
| 1 | - | - | - | 1 | 1 | 1 | 1 |
| - | 1 | 10 | D | 1 | 1 | 1 | 1 |
| - | 1 | 9 | D | 1 | 1 | 1 | 1 |
+-----------+-----------+---------+-------+--------+---------+--------+---------+
CLASS can go from 6-12 and SEC from A-Z,
*There's nothing in STUDENT, CLASS, SEC while there's some value in TEACHER and Vice-versa .
Now i want to create a table joining two tables with exact structure and data given above... I.e, I want the result to be something like below-
+-----------+-----------+---------+-------+--------+---------+--------+---------+
| TEACHER | STUDENT | CLASS | SEC | HB_a | VHB_b | HG_c | VHG_d |
|-----------+-----------+---------+-------+--------+---------+--------+---------|
| 2 | - | - | - | 2 | 2 | 2 | 2 |
| - | 2 | 10 | D | 2 | 2 | 2 | 2 |
| - | 2 | 9 | D | 2 | 2 | 2 | 2 |
+-----------+-----------+---------+-------+--------+---------+--------+---------+
I tried something like this but it doesn't work well, the output isn't what I want-
__tbl_sy = f"""
CREATE TABLE <tbl>
AS SELECT CLASS, SEC, SUM(TEACHER), SUM(STUDENT), SUM(HB_a), SUM(VHB_b), SUM(HG_c), SUM(VHG_d)
FROM <tbl1>
UNION
SELECT CLASS, SEC, SUM(TEACHER), SUM(STUDENT), SUM(HB_a), SUM(VHB_b), SUM(HG_c), SUM(VHG_d)
FROM <tbl2>
GROUP BY CLASS, SEC
"""
Cursor.execute(__tbl_sy)
For the sample data that you posted this will work:
select
sum(teacher) teacher, sum(student) student,
class, sec,
sum(hb_a) hb_a, sum(vhb_b) vhb_b, sum(hg_c) hg_c, sum(vhg_d) vhg_d
from (
select * from tbl1
union all
select * from tbl2
)
group by class, sec
See the demo.
Results:
| teacher | student | CLASS | SEC | hb_a | vhb_b | hg_c | vhg_d |
| ------- | ------- | ----- | --- | ---- | ----- | ---- | ----- |
| 2 | | | | 2 | 2 | 2 | 2 |
| | 2 | 10 | D | 2 | 2 | 2 | 2 |
| | 2 | 9 | D | 2 | 2 | 2 | 2 |

sqlalchemy how to divide 2 columns from different table

I have 2 tables named as company_info and company_income:
company_info :
| id | company_name | staff_num | year |
|----|--------------|-----------|------|
| 0 | A | 10 | 2010 |
| 1 | A | 10 | 2011 |
| 2 | A | 20 | 2012 |
| 3 | B | 20 | 2010 |
| 4 | B | 5 | 2011 |
company_income :
| id | company_name | income | year |
|----|--------------|--------|------|
| 0 | A | 10 | 2010 |
| 1 | A | 20 | 2011 |
| 2 | A | 30 | 2012 |
| 3 | B | 20 | 2010 |
| 4 | B | 15 | 2011 |
Now I want to calculate average staff income of each company, the result looks like this:
result :
| id | company_name | avg_income | year |
|----|--------------|------------|------|
| 0 | A | 1 | 2010 |
| 1 | A | 2 | 2011 |
| 2 | A | 1.5 | 2012 |
| 3 | B | 1 | 2010 |
| 4 | B | 3 | 2011 |
how to get this result using python SQLalchemy ? The database of the table is MySQL.
Join the tables and do a standard sum. You'd want to either set yourself up a view in MySQL with this query or create straight in your program.
SELECT
a.CompanyName,
a.year,
(a.staff_num / b.income) as avg_income
FROM
company_info as a
LEFT JOIN
company_income as b
ON
a.company_name = b.company_name
AND
a.year = b.year
You'd want a few wheres as well (such as where staff_num is not null or not equal to 0 and same as income. Also if you can have multiple values for the same company / year in both columns then you'll want to do a SUM of the values in the column, then group by companyname and year)
Try this:
SELECT
info.company_name,
(inc.income / info.staff_num) as avg,
info.year
FROM
company_info info JOIN company_income inc
ON
info.company_name = inc.company_name
AND
info.year = inc.year

Categories