Untitled
raw download clone
SQL
views 94
,
size 650 b
with suppliers_without_orgs AS (
	select s.id as supplier_id, 
	       s.emails
	from supplier_user su
	left join supplier s on s.id  = su.supplier_id
	left join supplier_org org on org.supplier_id = s.id
	where org.id is null and s.default_contract_id is null
	group by s.id
),

t_users as (select swo.supplier_id, 
			       s.id as new_supplier 
			from suppliers_without_orgs swo
join supplier s on s.emails = swo.emails
join supplier_org org on org.supplier_id = s.id
join supplier_contract sc on sc.org_id = org.id
where 
s.emails <> '' and swo.supplier_id <> s.id
and sc.data->>'market_type' = 'v3')

select * from t_users;
close fullscreen
Login or Register to edit or fork this paste. It's free.