WITH gtime AS (
  SELECT MAX("time") AS time FROM "glucose-processed" LIMIT 1
),
stime AS (
SELECT MIN("glucose-import"."time") AS time FROM "glucose-import" LIMIT 1
)
INSERT INTO "glucose-processed"
SELECT gv."time",
    absvalue,
    delta1st,
    delta2nd,
    smoothval
FROM "glucose-view" AS gv
LEFT JOIN gtime ON 1=1
LEFT JOIN stime ON 1=1
WHERE gv."time" >= COALESCE(gtime.time, stime.time) AND delta1st IS NOT NULL AND delta2nd IS NOT NULL
ORDER BY gv."time" ASC
ON CONFLICT ("time") DO NOTHING;
