我需要为每个“ Tecnico”计算“ Estado de solicitud”,对于每个“ Tecnico”,我要拥有多少“ Abiertos”或“ En espera”
这就是我所拥有的
SELECT ti.FIRST_NAME AS "Técnico", std.STATUSNAME AS "Estado de solicitud"
FROM WorkOrder wo
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 StatusDefinition std ON wos.STATUSID=std.STATUSID
WHERE ( std.STATUSID != 3 ) AND (std.STATUSID != 4)
答案 0 :(得分:2)
您可以通过条件聚合来做到这一点:
SELECT ti.FIRST_NAME AS "Técnico",
COUNT(*) FILTER (WHERE std.STATUSNAME = 'Abierto') as abiertos,
COUNT(*) FILTER (WHERE std.STATUSNAME = 'En Espera') as en_esperas
FROM WorkOrder wo 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
StatusDefinition std
ON wos.STATUSID = std.STATUSID
WHERE std.STATUSID NOT IN (3, 4)
GROUP BY ti.FIRST_NAME