User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Oracle section within the Web Development category of DaniWeb, a massive community of 374,190 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 3,483 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:
Views: 1775 | Replies: 5
Reply
Join Date: Apr 2008
Posts: 2
Reputation: djclipz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
djclipz djclipz is offline Offline
Newbie Poster

SQL Query question over multiple tables

  #1  
Apr 17th, 2008
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

  1. SELECT EMPLOYEEID, count(*) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID HAVING count(*)>= ALL(SELECT count(*) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID);

and this gives

EMPLOYEEID   COUNT(*)
----------         ----------
         2           4

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

  1. SELECT max(count(*)) FROM ABSENCEHISTORY GROUP BY EMPLOYEEID;

which gives

MAX(COUNT(*))
-------------
            4

But 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore,India
Posts: 1,173
Reputation: debasisdas is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 75
debasisdas's Avatar
debasisdas debasisdas is online now Online
Veteran Poster

Re: SQL Query question over multiple tables

  #2  
Apr 21st, 2008
also find out the employee id and pas that employee id to outer query to find out all the other details.
Share your Knowledge.
Reply With Quote  
Join Date: Nov 2007
Posts: 51
Reputation: Nige Ridd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
Nige Ridd Nige Ridd is offline Offline
Junior Poster in Training

Re: SQL Query question over multiple tables

  #3  
Apr 23rd, 2008
Not the best solution but...

  1. SELECT * FROM EMPLOYEE
  2. WHERE EMPLOYEEID IN (
  3. 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
Reply With Quote  
Join Date: Nov 2007
Posts: 15
Reputation: RDWilson2 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
RDWilson2 RDWilson2 is offline Offline
Newbie Poster

Re: SQL Query question over multiple tables

  #4  
Apr 23rd, 2008
Originally Posted by Nige Ridd View Post
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:

  1. SELECT Z.Absences, E.* FROM EMPLOYEE
  2. WHERE EMPLOYEEID E
  3. INNER JOIN
  4. (
  5. SELECT TOP 1 EMPLOYEEID, Absences
  6. FROM
  7. (
  8. SELECT EMPLOYEEID, COUNT(EMPLOYEEID) AS Absences
  9. FROM ABSENCEHISTORY
  10. GROUP BY EMPLOYEEID
  11. ) Y
  12. ORDER BY Absences, EMPLOYEEID
  13. ) Z
  14. 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
Reply With Quote  
Join Date: Nov 2007
Posts: 51
Reputation: Nige Ridd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
Nige Ridd Nige Ridd is offline Offline
Junior Poster in Training

Re: SQL Query question over multiple tables

  #5  
Apr 23rd, 2008
Unfortunately Select top n isn't supported by Oracle.
Nige
Reply With Quote  
Join Date: Nov 2007
Posts: 15
Reputation: RDWilson2 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
RDWilson2 RDWilson2 is offline Offline
Newbie Poster

Re: SQL Query question over multiple tables

  #6  
Apr 23rd, 2008
Originally Posted by Nige Ridd View Post
Unfortunately Select top n isn't supported by Oracle.
Nige


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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Oracle Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Oracle Forum

All times are GMT -4. The time now is 4:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC