CREATE VIEW vwrealtimeuserconv AS (
SELECT ud.conversationid,
ud.media,
ud.queueid,
ud.direction,
ud.conversationstate,
CASE
WHEN ((ud.conversationstate)::text = 'dialing'::text) THEN 'DIAL'::text
WHEN (ud.acwstate = true) THEN 'ACW'::text
WHEN (ud.heldstate = true) THEN 'HELD'::text
WHEN ((ud.conversationstate)::text = 'alerting'::text) THEN 'ALERT'::text
WHEN ((ud.conversationstate)::text = 'contacting'::text) THEN 'CONTACT'::text
ELSE 'TALKING'::text
END AS convstatus,
CASE
WHEN ((ud.conversationstate)::text = 'dialing'::text) THEN datediff('second'::character varying, ud.updated, timezone('utc'::text, now()))
WHEN (ud.acwstate = true) THEN datediff('second'::character varying, ud.acwtime, timezone('utc'::text, now()))
WHEN (ud.heldstate = true) THEN datediff('second'::character varying, ud.heldtime, timezone('utc'::text, now()))
WHEN ((ud.conversationstate)::text = 'alerting'::text) THEN datediff('second'::character varying, ud.updated, timezone('utc'::text, now()))
ELSE datediff('second'::character varying, ud.talktime, timezone('utc'::text, now()))
END AS convstatustime,
CASE
WHEN ((ud.conversationstate)::text = 'dialing'::text) THEN ((datediff('second'::character varying, ud.updated, timezone('utc'::text, now())))::numeric / 86400.00)
WHEN (ud.acwstate = true) THEN ((datediff('second'::character varying, ud.acwtime, timezone('utc'::text, now())))::numeric / 86400.00)
WHEN (ud.heldstate = true) THEN ((datediff('second'::character varying, ud.heldtime, timezone('utc'::text, now())))::numeric / 86400.00)
WHEN ((ud.conversationstate)::text = 'alerting'::text) THEN ((datediff('second'::character varying, ud.updated, timezone('utc'::text, now())))::numeric / 86400.00)
ELSE ((datediff('second'::character varying, ud.talktime, timezone('utc'::text, now())))::numeric / 86400.00)
END AS convstatustimeday,
ud.acwstate,
ud.acwtime,
ud.heldstate,
ud.heldtime,
ud.talktime
FROM userrealtimeconvdata ud
)