Group and Count Query [message #6459] |
Fri, 18 April 2003 10:25 |
Michele
Messages: 77 Registered: December 2000
|
Member |
|
|
Hello,
I have the below query:
select c.LAST_NAME as Name, count(distinct s.pers_id)
from cntrctr c, svc_date s
where c.rep_status = '1' and c.territory = s.territory
group by c.LAST_NAME;
Name Count
Duck 10
Mouse 5
This basically gives me a count of a persons in the svc_date table that report to each manager in the cntrctr table.
What I need to add is a 3rd column that counts the number of persons that have a certain status. IE:
select count(*) from cntrctr,svc_date where cntrctr.territory = svc_date.territory and cntrctr.rep_status = '2';
Name Count Count2
Duck 10 9
Mouse 5 2
I would like to add the second code into the first query and have one query but I am having trouble.
Thanks
Michele
|
|
|
Re: Group and Count Query [message #6460 is a reply to message #6459] |
Fri, 18 April 2003 10:44 |
Art Metzer
Messages: 2480 Registered: December 2002
|
Senior Member |
|
|
TrySQL> SELECT * FROM cntrctr;
LAST_ R TERRITORY
----- - ----------
Duck 1 1007
Mouse 1 1008
Duck 2 1009
Mouse 2 1010
SQL> SELECT * FROM svc_date;
PERS_ID TERRITORY
---------- ----------
101 1007
102 1007
103 1007
104 1007
105 1007
106 1007
107 1007
108 1007
109 1007
110 1007
151 1008
152 1008
153 1008
154 1008
155 1008
201 1009
202 1009
203 1009
204 1009
205 1009
206 1009
207 1009
208 1009
209 1009
251 1010
252 1010
26 rows selected.
SQL> SELECT c.last_name name
2 , SUM(DECODE(c.rep_status,'1',1,0)) rep_status_1
3 , SUM(DECODE(c.rep_status,'2',1,0)) rep_status_2
4 FROM cntrctr c
5 , svc_date s
6 WHERE c.territory = s.territory
7 GROUP BY c.last_name
8 /
NAME REP_STATUS_1 REP_STATUS_2
----- ------------ ------------
Duck 10 9
Mouse 5 2
SQL> Good luck,
A
|
|
|
Re: Group and Count Query [message #6462 is a reply to message #6459] |
Fri, 18 April 2003 11:19 |
Michele
Messages: 77 Registered: December 2000
|
Member |
|
|
Hi Art,
Thanks for your quick reply but I should have been more clear.
CNTRCTR
ID Name REP_STATUS Territory
1 Duck 1 05
2 Mouse 1 06
3 Goofy 2 05
4 Minney 2 06
SVC_DATE
ID TERRITORY
1 05
2 05
3 06
4 06
Rep_status 1 is the master record and then I need all the people who are rep_status '2' and are in the master territory
NAME COUNT(includes Master) Count(excludes Master)
DUCK 2 1
MOUSE 2 1
Sorry for the misunderstanding
Thanks Michele
|
|
|
|
|