我正在尝试构建一个 SQL 查询,它将根据系统日期进行过滤(查询过去 7 天内完成的所有销售):import datetimeimport pandas as pdimport psycopg2con = p.connect(db_details)cur = con.cursor()df = pd.read_sql("""select store_name,count(*) from sales where created_at between datetime.datetime.now() - (datetime.today() - timedelta(7))""",con=con)我收到一个错误psycopg2.NotSupportedError: cross-database references are not implemented: datetime.datetime.now
3 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
实际上,您不一定需要 Python 中的任何参数或计算。只需使用相应的 SQL 语句,它应该如下所示:
select store_name,count(*)
from sales
where created_at >= now()::date - 7
group by store_name
编辑:我还添加了一个group by我认为缺少的。
添加回答
举报
0/150
提交
取消
