Solved

Sum up Childs

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

16 Experts available now in Live!

Get 1:1 Help Now