2013-04-17 62 views
5

以下是我需要回答的提示:SQL嵌套聚合查詢 - 如何獲取每位員工的客戶數量?

列出每位員工的客戶數量。包括僱員的名字和編號適當的 客戶,列出最先顧客最多的僱員。

以下是我的員工表:

create table EMPLOYEES 
    (EmpID char(4)   unique Not null, 
    Ename varchar(10), 
    Job  varchar(9), 
    MGR  char(4), 
    Hiredate date, 
    Salary decimal(7,2), 
    Comm  decimal(7,2), 
    DeptNo char(2)   not null, 
     Primary key(EmpID), 
     Foreign key(DeptNo) REFERENCES DEPARTMENTS(DeptNo)); 


insert into EMPLOYEES values (7839,'King','President',null,'17-Nov-11',5000,null,10); 
insert into EMPLOYEES values (7698,'Blake','Manager',7839,'01-May-11',2850,null,30); 
insert into EMPLOYEES values (7782,'Clark','Manager',7839,'02-Jun-11',2450,null,10); 
insert into EMPLOYEES values (7566,'Jones','Manager',7839,'02-Apr-11',2975,null,20); 
insert into EMPLOYEES values (7654,'Martin','Salesman',7698,'28-Feb-12',1250,1400,30); 
insert into EMPLOYEES values (7499,'Allen','Salesman',7698,'20-Feb-11',1600,300,30); 
insert into EMPLOYEES values (7844,'Turner','Salesman',7698,'08-Sep-11',1500,0,30); 
insert into EMPLOYEES values (7900,'James','Clerk',7698,'22-Feb-12',950,null,30); 
insert into EMPLOYEES values (7521,'Ward','Salesman',7698,'22-Feb-12',1250,500,30); 
insert into EMPLOYEES values (7902,'Ford','Analyst',7566,'03-Dec-11',3000,null,20); 
insert into EMPLOYEES values (7369,'Smith','Clerk',7902,'17-Dec-10',800,null,20); 
insert into EMPLOYEES values (7788,'Scott','Analyst',7566,'09-Dec-12',3000,null,20); 
insert into EMPLOYEES values (7876,'Adams','Clerk',7788,'12-Jan-10',1100,null,20); 
insert into EMPLOYEES values (7934,'Miller','Clerk',7782,'23-Jan-12',1300,null,10); 

以下是我的客戶表:

create table CUSTOMERS 
    (CustID  char(6)  unique Not null, 
    Name  varchar(45), 
    Address  varchar(40), 
    City  varchar(30), 
    State  varchar(2), 
    Zip   varchar(9), 
    AreaCode char(3), 
    Phone  varchar (9), 
    RepID  char(4)  not null, 
    CreditLimit decimal(9,2), 
    Primary key(CustID), 
     Foreign key(RepID) References EMPLOYEES(EmpID)); 


insert into CUSTOMERS values (100,'Jocksports','345 Viewridge','Belmont','CA','96711',415,'598-6609',7844,5000); 
insert into CUSTOMERS values (101,'TKB Sport Shop','490 Boli Rd.','Redwood City','CA','94061',415,'368-1223',7521,10000); 
insert into CUSTOMERS values (102,'Vollyrite','9722 Hamilton','Burlingame','CA','95133',415,'644-3341',7654,7000); 
insert into CUSTOMERS values (103,'Just Tennis','Hillview Mall','Burlingame','CA','97544',415,'677-9312',7521,3000); 
insert into CUSTOMERS values (104,'Every Mountain','574 Surry Rd.','Cupertino','CA','93301',408,'996-2323',7499,10000); 
insert into CUSTOMERS values (105,'K + T Sports','3476 El Paseo','Santa Clara','CA','91003',408,'376-9966',7844,5000); 
insert into CUSTOMERS values (106,'Shape Up','908 Sequoia','Palo Alto','CA','94301',415,'364-9777',7521,6000); 
insert into CUSTOMERS values (107,'Womens Sports','Valco Village','Sunnyvale','CA','93301',408,'967-4398',7499,10000); 
insert into CUSTOMERS values (108,'North Woods Fitness Supply Center','98 Lone Pine Way','Hibbing','MN','55649',612,'566-9123',7844,8000); 

以下是我的查詢:

select ename, empId 
from EMPLOYEES 
where EmpID in 
(select count(repid) as NumberOfCustomers 
from CUSTOMERS 
group by RepID); 

爲什麼我的查詢不加工?

我知道我想將EMPLOYEES中的empid與CUSTOMERS中的repID進行匹配,然後COUNT中的代表ID出現在CUSTOMERS中多少次。我需要EMPLOYEES表的唯一原因是輸出Ename。林困惑的syntaxt我需要使用,因爲我需要輸出REPID的計數CUSTOMERS表

+2

+1張貼的腳本。所有人都喜歡Jeff Oris! – Quassnoi

+0

@Quassnoi:除了發佈明顯的作業:/ – mellamokb

回答

1

如果你正在使用SQL Server 2005以上也可以使用Common Table Expression

;WITH CTE 
AS 
(
    SELECT RepID, COUNT(*) AS CNT 
    FROM CUSTOMERS 
    GROUP BY REPID 
) 

SELECT E.Ename, E.EmpID, ISNULL(C.CNT, 0) 
FROM EMPLOYEES E 
LEFT OUTER JOIN CTE C ON C.RepID = E.EmpID 

DEMO

0

你內心的查詢只有一個count(repid),這是一個數,而不是你的外部查詢需要where EmpID in (xxx)實際僱員。不知道why're你想在你的內心查詢計數,因爲你最終結果失去它,但這應該工作:

select ename, empId 
from EMPLOYEES 
where EmpID in (
    select repID 
    from CUSTOMERS); 
+1

我收到以下錯誤消息:子查詢未與EXISTS一起引入時,只能在選擇列表中指定一個表達式。 –

+0

嘗試更新的版本。您不需要通過按repID計算客戶來進行分組,而不會在最終查詢中顯示。你也可以做一個內部連接,但我想你想要內部查詢的原因。 – mjuarez

+0

這不適合我。你認爲我應該在某個地方使用EXISTS嗎? –

1
SELECT * 
FROM employees e 
CROSS APPLY 
     (
     SELECT COUNT(*) 
     FROM customers c 
     WHERE c.repId = e.empId 
     ) cc (cnt) 
ORDER BY 
     cnt DESC 

見到這對SQLFiddle

同與子查詢:

SELECT *, 
     (
     SELECT COUNT(*) 
     FROM customers c 
     WHERE c.repId = e.empId 
     ) cnt 
FROM employees e 
ORDER BY 
     cnt DESC 
+0

我不允許使用CROSS APPLY –

+0

@JeffOrris:我不明白,誰可以提問?這是作業嗎? – Quassnoi

+0

是的。我很難與此,因爲我的數據庫類被取消,我自己學習這一點。我的數據庫書沒有很好的例子。 –

0

這是怎麼回事?還是我誤解

SELECT ename, count(*) as NumCustomers 
FROM Customers INNER JOIN 
Employees ON customers.repid = employees.empid GROUP BY repid,ename 
ORDER BY numCustomers DESC 
+0

我無法使用INNER JOIN,必須是嵌套查詢以最大限度地提高效率 –

+0

由於您需要員工姓名,將其加入僱員表並不總是更高效嗎?由於您需要保留客戶表中的計數以及與此相對應的員工姓名,因此如果不加入他們,我將如何做到這一點空白。 – Scotch

+0

我被告知在課堂上,而不是一個連接,嵌套查詢總是使用較少的資源 –