I have found solution to my problem in one question Merge pandas dataframes where one value is between two others I tried to modify it for my situation but it didn't work. In code given below, I need df to show the beginning and ending for each sold product and category. But it ignores date being between start and end period. as can be seen on screen for sel of Apple on 01/06/2020 period 26/03/2020 - 31/07/2020 should be shown but it shows other. How should I clarify SQL query?
import pandas as pd
import sqlite3
dates_of_discount=pd.DataFrame({"Date_begining":['01/01/2021','01/02/2020','26/03/2020'],
"Date_ending":['31/12/2021', '25/02/2020', '31/07/2020'],
"Category":['Discount', 'Not Discount', "Discount"],
"d_Product":['Apple', 'Peach', "Apple"]})
purchase_dates=pd.DataFrame({"date":(["20/01/2020", "18/02/2020", "01/06/2020"]),
"Qty":[100, 200, 300],
"Price":[3.5,4, 20],
"p_Product":['Apple', 'Peach', "Apple"]})
conn = sqlite3.connect(':memory:')
dates_of_discount.to_sql('disc', conn, index=False)
purchase_dates.to_sql('purch', conn, index=False)
qry = '''
select
purch.date Sold,
purch.p_Product Prod,
purch.Qty,
purch.Price,
Date_begining Period_Start,
Date_ending Period_End,
Category Output
from
purch join disc on
date between Date_begining and Date_ending and
d_Product = p_Product
'''
df = pd.read_sql_query(qry, conn)
df