如何获得这样的输出,我可以在公司名称行中删除重复项?
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
查询:
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;
产生这些结果:
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
答案 0 :(得分:0)
我没有经过测试,但它必须工作:
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;