Solved

Sum up Childs

Posted on 2013-01-28
4
149 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 40

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 43

Accepted Solution

by:
Rob 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
converting integer data type to time data type in sql 4 45
SQL query and VBA 5 54
compare date to getdate() 8 16
Help with Progress 4gl Rounding Function 6 24
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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