If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-17-03, 09:47
kullu kullu is offline
Registered User
 
Join Date: Oct 2003
Posts: 11
Query

Hi, I have the following tables-
How do I answer this query?

EMPLOYEE
ssn ename dnumber

DEPARTMENT
dnumber dname

PROJECT
pnumber dnumber

WORKS_ON
ssn pnumber hours


For each employee, find the department he works in and the total hours he spent on projects not controlled by the department he is working on.
Ie, he works on some extra projects not controlled by his department.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 11-17-03, 11:49
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
This should get you going

Code:
SELECT ed.ename, ed.dname, sum(pw.hours)
FROM
  (SELECT e.ssn, e.ename, d.dname, e.dnumber
   FROM employee e, department d 
   WHERE e.dnumber = d.dnumber) ed,
  (SELECT ssn, dnumber, hours
  FROM works_on w, project p
  WHERE w.pnumber = p.pnumber) pw
WHERE ed.ssn = pw.ssn
AND ed.dnumber != pw.dnumber
GROUP BY ed.ename, ed.dname
;
Reply With Quote
  #3 (permalink)  
Old 11-17-03, 23:41
satish_ct satish_ct is offline
Registered User
 
Join Date: Nov 2003
Location: Bangalore, INDIA
Posts: 333
Thumbs up

Hi,

SELECT e.ename
, e.dname
, sum(p.hours)
FROM
employee e
, department d
, project p
, works_on w
WHERE
e.dnumber = d.dnumber
and e.ssn = w.ssn
and w.pnumber = p.pnumber
GROUP BY e.ename
;
__________________
SATHISH .
Reply With Quote
  #4 (permalink)  
Old 11-18-03, 10:16
kullu kullu is offline
Registered User
 
Join Date: Oct 2003
Posts: 11
Quote:
Originally posted by gannet
This should get you going

Code:
SELECT ed.ename, ed.dname, sum(pw.hours)
FROM
  (SELECT e.ssn, e.ename, d.dname, e.dnumber
   FROM employee e, department d 
   WHERE e.dnumber = d.dnumber) ed,
  (SELECT ssn, dnumber, hours
  FROM works_on w, project p
  WHERE w.pnumber = p.pnumber) pw
WHERE ed.ssn = pw.ssn
AND ed.dnumber != pw.dnumber
GROUP BY ed.ename, ed.dname
;
Thanks,
That was a great solution!!
:-)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On