Solved

Sum up Childs

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Union 2 queries to a cte (temp table perhaps) 9 41
ServiceCenter IR Query Expressions 1 40
IIF in access query 19 23
Formating field inside mysql query 2 13
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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