簡體   English   中英

psycopg 有數據時返回空查詢結果

[英]psycopg returns empty query result when there is data

我正在嘗試從下面的 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)

我花了一整天的時間才意識到日志中的時間戳與預期的不同。 下面的查詢有效(使用 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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM