为了账号安全,请及时绑定邮箱和手机立即绑定

Python - 基于日期过滤 SQL 查询

Python - 基于日期过滤 SQL 查询

12345678_0001 2021-06-10 18:49:33
我正在尝试构建一个 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我认为缺少的。


查看完整回答
反对 回复 2021-06-16
  • 3 回答
  • 0 关注
  • 430 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号