Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do i get an output like this where i can suppress duplicates in the company name row?..

Example output:

  Ref Company name        Total calls Last name         Calls No call
----- ------------------- ----------- ------------ ---------- -------
  135 Dasher Services              12 Clarke                5
                                      Wright                4
                                      Turner                3
  142 Diaphonic Shipping           12 Hill                  5
                                      Johnson               5
                                      Butler                2
                                      Jackson               0 *******
  107 Juan and Co.                 12 White                 4
                                      Turner                3
                                      Clarke                3
                                      Roberts               2

Query:

SELECT 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;

produces these results:

       REF COMPANY_NAME                   TOTAL_CALLS LAST_NAME                 CALLS
---------- ------------------------------ ----------- -------------------- ----------
       100 Haunt Services                           4 Anderson                      4 
       100 Haunt Services                           4 Penrice                       4 
       100 Haunt Services                           3 Best                          3 
       100 Haunt Services                           2 Williams                      2 
       100 Haunt Services                           2 Lewis                         2 
       101 Genus Ltd.                               4 Phillips                      4 
       101 Genus Ltd.                               3 Parker                        3 
       101 Genus Ltd.                               2 Davies                        2 
       101 Genus Ltd.                               2 Johnson                       2 
       102 Corps Ltd.                               4 Gritten                       4 
       102 Corps Ltd.                               2 Scott                         2 
       103 Train Services                           7 Lawson                        7 
       103 Train Services                           2 Roberts                       2 

SQLFiddle here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
153 views
Welcome To Ask or Share your Answers For Others

1 Answer

I hadn't tested, but it has to work:

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...