Complex Postgres Query

Postgres Complex Query to get column values having corresponding column joined with another table having a specific value

Ok i finally constructed the right query as follows:

UPDATE app.affiliations
SET current = true
where current = false
and user_id in (select r.user_id
from app.affiliations as a join app.relationships as r
on r.target_user_id = a.user_id
group by r.user_id
having false = ALL(array_agg(a.current))
)

Complex SQL query works in Postgresql pgAdmin 4 but not Python

Per Parameters:

import psycopg2
con = psycopg2.connect(dbname="test", host='localhost', user='postgres')
cur = con.cursor()
number = 1

#mogrify returns an adapted query string. Used here to show that the
#query is correctly built. Substitute execute for actual usage.
cur.mogrify("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s and product.prodtempindex = %s",[number, 2])

'select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = 1 and product.prodtempindex = 2'

How to query complex range in postgreSql

SELECT * FROM TABLE_NAME WHERE 
(dateFrom <= Req.from AND dateTo >= Req.from)OR
(dateFrom <= Req.to AND dateTo >= Req.to) OR
(dateFrom >= Req.from AND dateTo <= Req.to)

I didn't find better then this



Related Topics



Leave a reply



Submit