客户将拥有很长的采购产品历史,我需要为每个客户导入根据购买日期购买的最新商品。
我有一个代码来汇总每个客户的最后购买日期,但是我不知道如何带来最新的产品。 信息来自的SQL表具有以下列 client_ID结束日期产品
ProductCol = [
'end_date'
'ID',
'hts_file'
]
ProductColNames = {
'ID' : 'customerID'
}
ProductImportFilter = {
'ID' : IDMaster
}
Product_date_col = 'end_date'
Product_from_date = ProductFromDate
ProductServer = 'Prod01'
ProductDatabase = 'Sales'
ProductTable = 'TableA'
Product = pdsql.mssql.rd_sql(
server = ProductServer,
database = ProductDatabase,
table = ProductTable,
col_names = ProductCol,
where_in = ProductImportFilter,
date_col = Product_date_col,
from_date= Product_from_date
)
# Format data
Product.rename(columns=ProductColNames, inplace=True)
Product['customerID'] = Product['customerID'] .str.strip().str.upper()
# Filter to latest Product
Product = Product.groupby(
['customerID'], as_index=False
).agg({'end_date' : 'max'})
Product.rename(columns =
{'end_date' :'LatestProduct',
},inplace=True)
预期的最终结果是在“产品”列上显示最新的产品