Oracle
 sql >> Base de données >  >> RDS >> Oracle

Problèmes de regroupement

Je n'avais pas testé, mais ça doit marcher :

SELECT case when lag(CUSTOMERS.COMPANY_NAME, 1, null) over 
                 (partition by CUSTOMERS.COMPANY_NAME 
                  order by COMPANY_NAME, COUNT(CALLS.CALL_REF) DESC) is null 
            then CUSTOMERS.COMPANY_NAME else null end cmp_name,
       CUSTOMERS.CUSTOMER_ID AS REF,
       CUSTOMERS.COMPANY_NAME,
       COUNT (CALLS.CALL_REF) AS TOTAL_CALLS,
       CALLERS.CALLER_LAST_NAME AS LAST_NAME,
       COUNT(CALLS.CALL_REF)AS CALLS
  FROM CUSTOMERS
  INNER JOIN CALLERS
    ON COMPANY_ID=CUSTOMER_ID
  LEFT JOIN CALLS
    ON CALLERS.CONTACT_ID=CALLS.CONTACT_ID
  GROUP BY CUSTOMERS.CUSTOMER_ID,
           CUSTOMERS.COMPANY_NAME,
           CALLERS.CALLER_LAST_NAME
  ORDER BY REF, COMPANY_NAME, CALLS DESC;