How can I put this into one query, the key point is that when grouping artist by name I need to consider all rows in artist, but I don't want to keep rows in artist_nodup table if it already is in mbartist_discogsartist table. (I'm using Postgres 9.3)
INSERT INTO artist_nodup
SELECT
min(a1.id) as id,
a1.name
FROM artist  a1
GROUP BY a1.name
HAVING COUNT(*)=1
;
DELETE
FROM artist_nodup T1
WHERE exists
( select 1
   from mbartist_discogsartist T2
  where t1.id = t2.discogs_id
)
;
 
     
     
    