0

I need to count "Estado de solicitud" for each "Tecnico" i mean, for each "Tecnico" how many "Abiertos" or "En espera" I have

This is what I have

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)

This is the result for the query

And something like this is that I need

1 Answer 1

2

You can do this with conditional aggregation:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.