postgresql - SQL Query - excluding rows where one value of a column with multiple values matches -
i have set of tables follows. (this simplified, several of these tables have other columns, i've tried pare down)
website id | _____________________________________ 10001 10002 10003 domain names name | website_id _____________________________________ surfinglikewoah.com | 10001 surfinglikewoah.net | 10001 ihatesurfing.com | 10002 surfinghatesme.net | 10003 promotion redemptions promo id | website_id _____________________________________ 0001 | 10001 0002 | 10001 0003 | 10001 0002 | 10002 promo codes promo id | code _____________________________________ 0001 | test 0002 | super-discount 0003 | double-discount
and i'm trying filter out accounts have promotion "test" - however, because these websites associated multiple promotion redemptions, wrong rows still appear in results. note: i'll filtering based on additional criteria, again columns may have multiple values per row, or may null.
here's query far:
select distinct ws.id, array(select distinct dn.name domainname dn ws.id = dn.website_id), array(select p.code promotionredemption pr, promotion p p.id = pr.promotion_id , pr.site_id = ws.id) website ws left join domainname dn on dn.website_id = ws.id left join promotionredemption pr on pr.site_id = ws.id left join promotion p on p.id = pr.promotion_id p.code != 'test' order ws.id asc
this query should not returning surfinglikewoah.com result - does, because result has 2 other promotion redemptions not match 'test'.
results: 10001 | {surfinglikewoah.com, surfinglikewoah.net} | {test, super-discount, double-discount} 10002 | {ihatesurfing.com} | {super-discount} 10003 | {surfinghatesme.net} | {}
i feel i'm missing obvious!
help appreciated.
what want find 1 record record not exist. last 2 words of last sentence key not exists. find lot of code uses outer joins think better served using exists clause.
the following query may not need, should close enough can adjust fix exact needs.
select ws.id array(select distinct dn.name domainname dn ws.id = dn.website_id), array(select p.code promotionredemption pr, promotion p p.id = pr.promotion_id , pr.site_id = ws.id) website ws not exists ( select 1 promotion p left join promotionredemption pr on p.id = pr.promotion_id pr.site_id = ws.id , p.code = 'test' ) order ws.id asc
Comments
Post a Comment