← 返回题库
初级

生成库存预警报表(汇总报表统计)

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    import sqlite3
    import pandas as pd
    
    conn = sqlite3.connect(':memory:')
    
    base_url = 'https://liangdaima.com/static/data/crm/state_08/'
    
    tables = ['suppliers', 'customers', 'products', 
              'stock_in', 'stock_in_items', 
              'stock_out', 'stock_out_items']
    
    for table in tables:
        try:
            url = base_url + table + '.csv'
            df = pd.read_csv(url)
            df.to_sql(table, conn, index=False)
        except Exception as e:
            pass
    
    result = conn.execute('SELECT * FROM (SELECT p.物料编码, p.物料名称, COALESCE(i.入库总量, 0) - COALESCE(o.出库总量, 0) AS 库存 FROM products p LEFT JOIN (SELECT 物料编码, SUM(数量) AS 入库总量 FROM stock_in_items GROUP BY 物料编码) i ON p.物料编码 = i.物料编码 LEFT JOIN (SELECT 物料编码, SUM(数量) AS 出库总量 FROM stock_out_items GROUP BY 物料编码) o ON p.物料编码 = o.物料编码) WHERE 库存 < 5').fetchall()
    conn.close()
    return result

示例

输入
print(solve())
期望输出
[('XM004', '小米13 Pro', 2)]
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码