Iām doing a join between two tables in PostgreSQL, one has a primary key constraint on incidentnumber (opentickets) while the other table does not have the restraint and can have duplicate incidentnumbers (incommingtickets). The problem comes when trying to filter out duplicates. The query,
SELECT incommingtickets.*
FROM incommingtickets
LEFT JOIN opentickets
ON incommingtickets.incidentnumber = opentickets.incidentnumber
WHERE opentickets.incidentnumber IS NULL
AND incommingtickets.status NOT IN ('Closed','Cancelled', '')
works until it hits a duplicate, the I get the violates primary key message. If I add a distinct clause like,
SELECT DISTINCT ON (incommingtickets.incidentnumber) incommingtickets.*
FROM incommingtickets
LEFT JOIN opentickets
ON incommingtickets.incidentnumber = opentickets.incidentnumber
WHERE opentickets.incidentnumber IS NULL
AND incommingtickets.status NOT IN ('Closed','Cancelled', '')
I get an error,
pg_query(): Query failed: ERROR: missing FROM-clause entry for table "incommingtickets" LINE 30: WHERE opentickets.incidentnumber = incommingtickets.incident...