Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

oracle - Problems with grouping

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

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

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...