psycopg 有数据时返回空查询结果

问题描述 投票:0回答:2

我正在尝试从下面的 postgressql 查询中获取记录。它确实有数据,但当我尝试用 psycopg 检索它时,奇怪的是它返回空。它可以与WITH查询一起使用还是我们只能使用SELECT来代替?任何意见将不胜感激,谢谢。

query = \
        f""" WITH a AS (
            SELECT "public"."table_a"."code", 
            "public"."table_a"."address", sum("public"."table_a"."amount") AS 
            "sum" FROM "public"."table_a" WHERE "public"."table_a"."address" <> 'KL' GROUP BY 
            "public"."table_a"."code", "public"."table_a"."address"), 
            c as (
                WITH b AS ( 
                SELECT address, code, count(*) as "total" FROM table_b where created_date between 
                ((current_date + TIME '14:00:00.000+08:00') - interval '7 days') and ((
                current_date + TIME '23:59:00.000+08:00') - interval '7 days') group by 
                address, code order by address, code) 
                select b.address, table_c.unit_code, table_c.name, sum(b.total * 
                table_c.amount) as "total" from table_c join b on table_c.code = b.code 
                group by table_c.unit_code, table_c.name, b.address 
                order by table_c.unit_code, b.address
                )
            SELECT a.address, a.code, (a.sum - c.total)::int as output
            FROM a join c on a.code = c.unit_code
            AND a.address = c.address
            ORDER BY a.address, a.code 
"""

conn = psycopg2.connect(**params)
        cur = conn.cursor()
        cur.execute(query)
rows = cur.fetchall()
for row in rows:
            print(row)
python postgresql psycopg2
2个回答
1
投票

我花了一整天才意识到日志中的时间戳与预期不同。下面的查询有效(使用WITH AS和所有),只需要为我删除时区,因为我不需要它。学习到教训了。请务必检查服务器时区以及是否需要转换。希望这对将来的人有帮助。

WITH a AS (
            SELECT "public"."table_a"."code", 
            "public"."table_a"."address", sum("public"."table_a"."amount") AS 
            "sum" FROM "public"."table_a" WHERE "public"."table_a"."address" <> 'KL' GROUP BY 
            "public"."table_a"."code", "public"."table_a"."address"), 
            c as (
                WITH b AS ( 
                SELECT address, code, count(*) as "total" FROM table_b where created_date between 
                ((current_date + TIME '14:00:00.000') - interval '7 days') and ((
                current_date + TIME '23:59:00.000') - interval '7 days') group by 
                address, code order by address, code) 
                select b.address, table_c.unit_code, table_c.name, sum(b.total * 
                table_c.amount) as "total" from table_c join b on table_c.code = b.code 
                group by table_c.unit_code, table_c.name, b.address 
                order by table_c.unit_code, b.address
                )
            SELECT a.address, a.code, (a.sum - c.total)::int as output
            FROM a join c on a.code = c.unit_code
            AND a.address = c.address
            ORDER BY a.address, a.code 

0
投票

我最近遇到了一个令人困惑的问题,想分享我的经验以造福社区。我在本地计算机上制作了一个查询,其中涉及与 PostgreSQL 表中的created_at 列进行时间戳比较。该查询在我的 DBMS 客户端中执行时完美运行,但在我的 Python 代码中使用 psycopg2 执行时抛出错误。

令我惊讶的是,解决方案非常简单。最初看起来不错的时间戳比较导致了差异。当用 psycopg2 执行时,它不知何故失败了。然而,我能够通过使用 PostgreSQL 的clock_timestamp() 函数和时间间隔来转换比较来解决这个问题。

这是最初导致问题的代码片段:


WHERE created_at > '2023-08-17 12:00:00' -- Example timestamp

这是与 DBMS 客户端和 psycopg2 一致工作的改进版本:


WHERE created_at > clock_timestamp() - interval '10 minutes'

这里的关键教训是,即使看似正确的查询有时在不同的上下文中(例如在服务器中)也会有不同的行为。通过使用 PostgreSQL 的内置函数和表达式,我们可以确保不同工具和接口之间的行为一致。

有关 PostgreSQL 日期时间函数的更多信息,可以参考官方文档:PostgreSQL Datetime Functions

© www.soinside.com 2019 - 2024. All rights reserved.