[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

Sum up Childs

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
tonelm54
Asked:
tonelm54
1 Solution
 
als315Commented:
0
 
Tomas Helgi JohannssonCommented:
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
 
tonelm54Author Commented:
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
 
RobOwner (Aidellio)Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now