SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 26th, 2004, 09:19 AM
victor victor is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 9 victor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
one more problem...

Hi, I just stumbeled over another problem i hope you can help me with

Having the following tables:

Dept
-------
*Deptname
Owes


Emp
------
*Name
DeptName (foreign key: Dept.DeptName)


Car
------
*CarNr
OutTo (foreign key: Emp.Name)
Cost


The outout I want is:
DeptName, Liability (for now the liability is just the cars cost), Total (what the department owes + liability)
not including the it dept and depts owing less than/equal to 1400


My sql query looks like the following:
SELECT d.DeptName, c.Cost AS Liability, d.Owes + Liability AS Total
FROM Dept d, Emp e, Car c
WHERE d.DeptName = e.DeptName
AND e.Name = c.OutTo
AND d.DeptName <> 'IT'
AND d.Owes >= 1400


The problem is that I get one of the deptnames listed several times, because there's more than one person borrowing a car in that dept.

Do you understand the problem? I'm not sure how to explain it. Any suggestions on the solution?

Last edited by victor : March 26th, 2004 at 09:25 AM.

Reply With Quote
  #2  
Old March 29th, 2004, 03:48 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
And this is a problem because?? Include employee name so you get Department/Employee Name for each liability


OR

This for a total sum for each column by deptName

SELECT Dept.Deptname, Sum(Car.Cost) AS LIABILITY, Sum([Dept]![Owes]+[Car]![Cost]) AS Total
FROM (Dept INNER JOIN Emp ON Dept.Deptname = Emp.DeptName) INNER JOIN Car ON Emp.Name = Car.OutTo
GROUP BY Dept.Deptname
HAVING (((Dept.Deptname)<>"it") AND ((First(Dept.Owes))>1400));



S-

Reply With Quote
  #3  
Old March 30th, 2004, 09:09 AM
victor victor is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 9 victor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I want the infomation pr department. I tried your suggestion but I get the error: Too few parameters. Expected 1.

When I tried this:

SELECT d.DeptName, SUM(c.Cost) AS Liability, SUM(d.Owes + c.Cost) AS Total
FROM Dept d, Emp e, Car c
WHERE d.DeptName = e.DeptName
AND e.Name = c.OutTo
AND d.DeptName <> 'IT'
AND d.Owes >= 1400
GROUP BY Dept.Deptname, c.Cost, d.Owes

The problem is still there.. Any suggestions?

Reply With Quote
  #4  
Old March 30th, 2004, 04:14 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
Which DB are you using. The SQL I sent you works great in Access.

S-

Reply With Quote
  #5  
Old March 31st, 2004, 01:10 AM
victor victor is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 9 victor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hmm, I use Access as well. I tried the query using Access only and it worked but i still get the error on the server..

I'm learnig JSTL and it's on a tomcat 4.1 server. I don't think there's a problem with the connection because all the other queries I've made works perfectly fine on the same database.. Do you have an idea of what the problem might be?

Reply With Quote
  #6  
Old March 31st, 2004, 01:32 AM
victor victor is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 9 victor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I discoverd an other problem as well: If the sales department owes 5000 and their liability is 200 your suggestion gives the total of 10200 not 5200..

Solution:

SELECT Dept.Deptname, Sum(Car.Cost) AS LIABILITY, Dep.Owes + Liability AS Total
FROM (Dept INNER JOIN Emp ON Dept.Deptname = Emp.DeptName) INNER JOIN Car ON Emp.Name = Car.OutTo
GROUP BY Dept.Deptname
HAVING (((Dept.Deptname)<>"it") AND ((First(Dept.Owes))>1400));

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > one more problem...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT