postgresql 9.2 - Confused with sql query -
i have following tables:
customer(login varchar, town varchar) orders (ordno int , login varchar) >>login fk customer combination (id int, ordno int ,product_id int) >>ordno fk orders
i need show products have been sold in all cities.
example:
insert customer (log1 , ny) (log2, ny) (log3, london) insert orders (1,log1) (2,log1) (3,log3) insert combination (1,1,1) (2,2,2) (3,3,1)
- product 1 sold in ny
- product 2 sold in ny
- product 1 sold in london
if available cities ny , london, product must result of query product 1
select a.productid combination inner join orders b on a.ordno = b.ordno inner join customer c on b.login = c.login group a.productid having count(distinct a.id) = (select count(distinct town) customer)