Query to show total number of conversations, incoming and outgoing conversations (MSSQL & PGSQL)

Query to show total number of conversations, incoming and outgoing conversations (MSSQL & PGSQL)

Tested in build PGSQL (14300) and MSSQL (14306)


Total Number of Conversations:

SELECT wo.WORKORDERID AS "Request ID", wo.TITLE AS "Subject", pd.PRIORITYNAME AS "Priority", aau.FIRST_NAME AS "Requester", ad.ORG_NAME AS "Account", std.STATUSNAME AS "Status", ti.FIRST_NAME AS "Technician", ((select count(c.conversationid) from conversation c where c.workorderid=wo.workorderid) + (select count(n.notificationid) from notify_workorder n  LEFT JOIN notification no on n.notificationid=no.notificationid where no.senderid!='1' AND n.workorderid=wo.workorderid))  "Total number of conversations" FROM WorkOrder wo LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID LEFT JOIN SDUser td ON wos.OWNERID=td.USERID LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID LEFT JOIN PriorityDefinition pd ON wos.PRIORITYID=pd.PRIORITYID LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID LEFT JOIN AccountSiteMapping asm ON wo.SITEID=asm.SITEID LEFT JOIN AccountDefinition ad ON asm.ACCOUNTID=ad.ORG_ID

Number of Incoming Conversations:

select wo.workorderid "Request ID", COUNT(conv.conversationid) "Number of Incoming Conversations" FROM workorder wo LEFT JOIN conversation conv ON wo.workorderid=conv.workorderid LEFT JOIN conversationdescription convd ON conv.conversationid=convd.conversationid LEFT JOIN sduser sdu ON conv.requesterid=sdu.userid LEFT JOIN aaauser aaau ON sdu.userid=aaau.user_id WHERE conv.workorderid=wo.workorderid group by wo.workorderid 
order by 1

Number of Outgoing Conversations: (Excluding system notifications):

select wo.workorderid "Request ID", COUNT(n.Notificationid) "Number of Outgoing Conversations" from workorder wo left join notify_workorder nw on wo.workorderid=nw.workorderid LEFT JOIN notification n on nw.notificationid=n.notificationid left join aaauser nau on n.senderid=nau.user_id where nau.first_name!='system' GROUP BY wo.workorderid
                  New to ADManager Plus?

                    New to ADSelfService Plus?