sql server 2008 - SQL Nested Aggregate Query- How do I get the number of customers per employee? -


the following prompt need answer:

list number of customers each employee. include employee's name , number of customers labeled appropriately, listing employee customers first.

the following employees table:

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 employees values (7839,'king','president',null,'17-nov-11',5000,null,10); insert employees values (7698,'blake','manager',7839,'01-may-11',2850,null,30); insert employees values (7782,'clark','manager',7839,'02-jun-11',2450,null,10); insert employees values (7566,'jones','manager',7839,'02-apr-11',2975,null,20); insert employees values (7654,'martin','salesman',7698,'28-feb-12',1250,1400,30); insert employees values (7499,'allen','salesman',7698,'20-feb-11',1600,300,30); insert employees values (7844,'turner','salesman',7698,'08-sep-11',1500,0,30); insert employees values (7900,'james','clerk',7698,'22-feb-12',950,null,30); insert employees values (7521,'ward','salesman',7698,'22-feb-12',1250,500,30); insert employees values (7902,'ford','analyst',7566,'03-dec-11',3000,null,20); insert employees values (7369,'smith','clerk',7902,'17-dec-10',800,null,20); insert employees values (7788,'scott','analyst',7566,'09-dec-12',3000,null,20); insert employees values (7876,'adams','clerk',7788,'12-jan-10',1100,null,20); insert employees values (7934,'miller','clerk',7782,'23-jan-12',1300,null,10); 

the following customers table:

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 customers values (100,'jocksports','345 viewridge','belmont','ca','96711',415,'598-6609',7844,5000); insert customers values (101,'tkb sport shop','490 boli rd.','redwood city','ca','94061',415,'368-1223',7521,10000); insert customers values (102,'vollyrite','9722 hamilton','burlingame','ca','95133',415,'644-3341',7654,7000); insert customers values (103,'just tennis','hillview mall','burlingame','ca','97544',415,'677-9312',7521,3000); insert customers values (104,'every mountain','574 surry rd.','cupertino','ca','93301',408,'996-2323',7499,10000); insert customers values (105,'k + t sports','3476 el paseo','santa clara','ca','91003',408,'376-9966',7844,5000); insert customers values (106,'shape up','908 sequoia','palo alto','ca','94301',415,'364-9777',7521,6000); insert customers values (107,'womens sports','valco village','sunnyvale','ca','93301',408,'967-4398',7499,10000); insert customers values (108,'north woods fitness supply center','98 lone pine way','hibbing','mn','55649',612,'566-9123',7844,8000); 

the following query:

select ename, empid employees empid in (select count(repid) numberofcustomers customers group repid); 

why query not working?

i know want match empid employees repid in customers , count how many times rep id shows in customers. reason need employees table out put ename. im confused syntaxt need use because need output count of repid in customers table

you can use common table expression if using sql server 2005 , above.

;with cte (    select repid, count(*) cnt    customers     group repid   )  select e.ename, e.empid, isnull(c.cnt, 0)  employees e left outer join cte c on c.repid = e.empid 

demo


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -