•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Oracle section within the Web Development category of DaniWeb, a massive community of 426,590 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,677 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Oracle advertiser: Programming Forums
Views: 2806 | Replies: 5
![]() |
| |
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hey all,
Ok first my problem. I have a database that has an employee table and then also an absent table(for when they are off sick).
The problem i have is using sql to find the employee that is off the most! So for my sql on the absent table i do something like this
and this gives
Now this is all good as i now know that the employee with id 2 has had the most absences. However is it possible in 1 query to use the EMPLOYEEID i have been given there to find out the attributes of the employee that are stored in the EMPLOYEE table. I have been trying JOINS, UNIONS but no success as far. Does anyone know if it is actually possible to do this in 1 query?
I have also tried this
which gives
But that doesn't help much as i have nothing about which employeeid it corresponds to! Just the highest value!
Thank-you
DJ CLipz
Ok first my problem. I have a database that has an employee table and then also an absent table(for when they are off sick).
The problem i have is using sql to find the employee that is off the most! So for my sql on the absent table i do something like this
sql Syntax (Toggle Plain Text)
SELECT EMPLOYEEID, count(*) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID HAVING count(*)>= ALL(SELECT count(*) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID);
and this gives
EMPLOYEEID COUNT(*)
---------- ----------
2 4Now this is all good as i now know that the employee with id 2 has had the most absences. However is it possible in 1 query to use the EMPLOYEEID i have been given there to find out the attributes of the employee that are stored in the EMPLOYEE table. I have been trying JOINS, UNIONS but no success as far. Does anyone know if it is actually possible to do this in 1 query?
I have also tried this
sql Syntax (Toggle Plain Text)
SELECT max(count(*)) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID;
which gives
MAX(COUNT(*))
-------------
4But that doesn't help much as i have nothing about which employeeid it corresponds to! Just the highest value!
Thank-you
DJ CLipz
Last edited by peter_budo : Apr 17th, 2008 at 7:23 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Feb 2007
Location: Bangalore,India
Posts: 1,290
Reputation:
Rep Power: 4
Solved Threads: 83
•
•
Join Date: Nov 2007
Posts: 51
Reputation:
Rep Power: 1
Solved Threads: 9
Not the best solution but...
You should take into account that there may be more than one person with the worst attendance.
Nige
sql Syntax (Toggle Plain Text)
SELECT * FROM EMPLOYEE WHERE EMPLOYEEID IN ( SELECT EMPLOYEEID FROM ABSENCEHISTORY GROUP BY EMPLOYEEID HAVING count(*) = ALL(SELECT count(*) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID))
You should take into account that there may be more than one person with the worst attendance.
Nige
Last edited by peter_budo : Apr 23rd, 2008 at 7:01 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Nov 2007
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
Not the best solution but...
SELECT * FROM EMPLOYEE
WHERE EMPLOYEEID IN (
SELECT EMPLOYEEID FROM ABSENCEHISTORY GROUP BY EMPLOYEEID HAVING count(*) = ALL(SELECT count(*) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID))
You should take into account that there may be more than one person with the worst attendance.
Nige
How about a slight modification on that theme:
sql Syntax (Toggle Plain Text)
SELECT Z.Absences, E.* FROM EMPLOYEE WHERE EMPLOYEEID E INNER JOIN ( SELECT TOP 1 EMPLOYEEID, Absences FROM ( SELECT EMPLOYEEID, COUNT(EMPLOYEEID) AS Absences FROM ABSENCEHISTORY GROUP BY EMPLOYEEID ) Y ORDER BY Absences, EMPLOYEEID ) Z ON E.EMPLOYEEID=Z.EMPLOYEEID;
Last edited by peter_budo : Apr 23rd, 2008 at 7:02 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Nov 2007
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
Nige,
Sorry about that. I am not currently working with Oracle but, rather, with SQL Server (and my next most recent experience is on Teradata which also supports the TOP n feature) so I am not sure how to immulate the TOP n functionality in Oracle. I am, however, quite sure that there is a means for doing so.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Oracle Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- SQL String question (ASP)
- Questions for Web Gurus! (*??*) (ASP.NET)
Other Threads in the Oracle Forum
- Previous Thread: Dates between the two given dates
- Next Thread: Auto increment



Hybrid Mode