Solved

Sum up Childs

Posted on 2013-01-28
4
134 Views
Last Modified: 2013-09-15
Im looking for advise on working summing together all the time spent on a particular job, however also including child jobs.

So currently I have a table with 3 coloums:-
JobNo (smallInt)
ParentJobNo (smallInt)
TimeSpend (smallInt)

Filled with some test data:-

JobNo      ParentJobNo      TimeSpent
10                  5
11      10            10
12      10            15
13      10            5
14      10            10
15      14            15
16      14            20
17      13            5
18                  10
19                  15
20      19            10

Currently we have the query:-
SELECT Jobs.JobNo, Jobs.TimeSpent
FROM Jobs
WHERE (((Jobs.ParentJobNo) Is Null));

Open in new window


Which gives:-
JobNo            TimeSpent
10            5
18            10
19            15

However what I would like it to do is sum all the child jobs, and child jobs in that, so the correct answer would be:-
JobNo      TimeSpent
10      85
18      10
19      25


My current idea is to create a function in PHP to look at each row, and look for child jobs and then children of that returning the number each time, but I would prefer to do this in the query itself.

I did think of creating a function in MySQL, but as the function would have to have use recursion, which is set to quite low on my shared server.

Im going to be using MySQL in the end, but using Access to quickly show my issue.

I hope someone can cope up with a better idea.
0
Comment
Question by:tonelm54
4 Comments
 
LVL 39

Expert Comment

by:als315
ID: 38827551
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 38834890
Hi!

Try this

SELECT x.JobNo,SUM(x.TimeSpent) from(
SELECT b.ParentJobNo as JobNo, a.TimeSpent
FROM Jobs a, Jobs b
where a.JobNo = b.ParentJobNo
UNION ALL
SELECT c.ParentJobNo as JobNo, c.TimeSpent
FROM Jobs c
WHERE c.ParentJobNo Is Null))
)X
GROUP BY x.JobNo

Regards,
     Tomas Helgi
0
 

Author Comment

by:tonelm54
ID: 38884529
When I try I run the query:-
SELECT x.JobNo,SUM(x.TimeSpent) from(
SELECT b.ParentJobNo as JobNo, a.TimeSpent
FROM Jobs a, Jobs b
where a.JobNo = b.ParentJobNo
UNION ALL
SELECT c.ParentJobNo as JobNo, c.TimeSpent
FROM Jobs c
WHERE c.ParentJobNo Is Null))
)X
GROUP BY x.JobNo

Open in new window


MySQL says:-
Error Code: 1248. Every derived table must have its own alias      

If I do a quick dump on the database table, Ive got:-
CREATE TABLE `jobs` (
  `JobNo` int(11) NOT NULL,
  `ParentJobNo` int(11) DEFAULT NULL,
  `TimeSpent` int(11) DEFAULT NULL,
  PRIMARY KEY (`JobNo`)
);

INSERT INTO `jobs` VALUES (10,NULL,5),(11,10,10),(12,10,15),(13,10,5),(14,10,10)
,(15,14,15),(16,14,20),(17,13,5),(18,NULL,10),(19,NULL,15),(20,19,10);

Open in new window


Can you advise, the query seems to do what I want, but I cannot understand it to figure out how to debug it.
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39448527
You need to specify an alias for the table and remove the erroneous brackets

SELECT x.JobNo,SUM(x.TimeSpent) from(
SELECT b.ParentJobNo as JobNo, a.TimeSpent
FROM Jobs a, Jobs b
where a.JobNo = b.ParentJobNo
UNION ALL
SELECT c.ParentJobNo as JobNo, c.TimeSpent
FROM Jobs c
WHERE c.ParentJobNo Is Null
) as x
GROUP BY x.JobNo

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now