简体   繁体   中英

psycopg returns empty query result when there is data

I'm trying to get records from the postgressql query below. It does have data but when I tried to retrieve it with psycopg, weirdly it returns empty. Does it work with WITH query or we can only use SELECT instead? Any input would be appreciated, thank you.

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)

It took a whole day for me to realize that the timestamp in the log is different than expected. The query below works (with WITH AS and all), just need to remove the timezone for me since I don't need it. Lesson learnt. Make sure to check the server timezone and if conversion is needed. Hope this helps someone in the future.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM