UNION doet gewoon niet wat je beschrijft. Deze zoekopdracht moet:
CREATE TABLE AS
SELECT date, location_code, product_code, quantity
FROM transactions_kitchen k
UNION ALL
SELECT h.date, h.location_code, h.product_code, h.quantity
FROM transactions_admin h
LEFT JOIN transactions_kitchen k USING (location_code, date)
WHERE k.location_code IS NULL;
LEFT JOIN / IS NULL om rijen uit te sluiten van de tweede tabel voor dezelfde locatie en datum. Zie:
- Selecteer rijen die niet aanwezig zijn in een andere tabel
Gebruik CREATE TABLE AS in plaats van SELECT INTO . De handleiding:
CREATE TABLE ASis functioneel vergelijkbaar metSELECT INTO.CREATE TABLE ASis de aanbevolen syntaxis, aangezien deze vorm vanSELECT INTOis niet beschikbaar in ECPG of PL/pgSQL, omdat ze deINTOinterpreteren clausule anders. VerderCREATE TABLE ASbiedt een superset van de functionaliteit vanSELECT INTO.
Of, als de doeltabel al bestaat:
INSERT INTO transactions_combined (<list names of target column here!>)
SELECT ...
Terzijde:ik zou date niet gebruiken als kolomnaam. Het is een gereserveerd woord in elke SQL-standaard en een functie- en gegevenstypenaam in Postgres.