I need to tune this statement to return the id if row exists or insert row if not exists based on 2 parameters:
WITH d(t, e) AS ( VALUES (1, current_timestamp)),
t AS (SELECT id FROM attendance, d WHERE start = e and user_id = t),
i AS (INSERT INTO attendance (user_id, start)
SELECT t, e FROM d WHERE t NOT IN (SELECT id FROM t))
SELECT t,e FROM d WHERE t IN (SELECT id FROM t);
1
and current_timestamp
needs to be parametric. I will use this from nodejs. I have just used pgsql function because when I supplement timestamp like '2016-08-18T14:51:42.333Z'
it is saying that I need to convert it.
Current state is that with current_timestamp
function, it will insert the new row but not return it's id.
This is what I have constructed with help of this thread.
Thanks for solutions.