| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.server -> Query to show actual value if count=1
We have a table that tracks the tasks employees work on every day and
also the employees' locations. We need a query that summarizes for a
certain time period the tasks and locations for each employee. If an
employee worked on multiple tasks/locations we want the query to say
'Many', otherwise it should display the task/location.
CREATE TABLE empjobs (empid NUMBER, day DATE, jobid NUMBER, location NUMBER);
INSERT INTO empjobs VALUES (1,TRUNC(SYSDATE),100,1000);
INSERT INTO empjobs VALUES (1,TRUNC(SYSDATE-1),200,1000);
INSERT INTO empjobs VALUES (1,TRUNC(SYSDATE-2),200,2000);
INSERT INTO empjobs VALUES (2,TRUNC(SYSDATE),100,1000);
INSERT INTO empjobs VALUES (2,TRUNC(SYSDATE-1),200,1000);
INSERT INTO empjobs VALUES (2,TRUNC(SYSDATE-2),200,1000);
INSERT INTO empjobs VALUES (3,TRUNC(SYSDATE),100,1000);
INSERT INTO empjobs VALUES (3,TRUNC(SYSDATE-1),100,1000);
INSERT INTO empjobs VALUES (3,TRUNC(SYSDATE-2),100,1000);
Desired result:
EMPID JOBID LOCATION
----- ----- --------
1 Many Many
2 Many 1000
3 100 1000
The closest I can get is using a query like this:
SELECT empid,
DECODE(COUNT(DISTINCT jobid),1,'One','Many') jobid, DECODE(COUNT(DISTINCT location),1,'One','Many') locationFROM empjobs
Which gives me the following result:
EMPID JOBID LOCATION
----- ----- --------
1 Many Many
2 Many One
3 One One
Oracle8i Enterprise Edition Release 8.1.6.1.0
Thanks a lot,
-Tom Received on Thu Nov 14 2002 - 13:07:15 CST
![]() |
![]() |