CREATE VIEW vwcalldetail AS (
SELECT di.conversationid,
di.originaldirection,
di.mediatype,
di.ani,
di.dnis,
di.purpose,
di.sessiondirection,
di.conversationstartdate,
di.userid,
di.queueid,
qd.name AS queuename,
di.conversationenddate,
datediff('second'::character varying, di.conversationstartdate, di.conversationenddate) AS callduration,
sum(datediff('second'::character varying, di.segmentstartdate, di.segmentenddate)) AS talkduration,
((datediff('second'::character varying, di.conversationstartdate, di.conversationenddate))::numeric / 86400.00) AS calldurationday,
((sum(datediff('second'::character varying, di.segmentstartdate, di.segmentenddate)))::numeric / 86400.00) AS talkdurationday,
ud.name,
di.segmenttype
FROM ((detailedinteractiondata di
LEFT JOIN userdetails ud ON (((ud.id)::text = (di.userid)::text)))
LEFT JOIN queuedetails qd ON (((qd.id)::text = (di.queueid)::text)))
WHERE ((NOT (di.userid IS NULL)) AND ((di.purpose)::text = ANY ((ARRAY['agent'::character varying, 'user'::character varying, 'voicemail'::character varying])::text[])) AND ((di.segmenttype)::text = ANY ((ARRAY['Interact'::character varying, 'contacting'::character varying, 'dialing'::character varying])::text[])))
GROUP BY di.conversationid, di.originaldirection, di.mediatype, di.ani, di.dnis, di.purpose, di.sessiondirection, di.conversationstartdate, di.userid, di.queueid, qd.name, di.conversationenddate, ud.name, di.segmenttype
)