I am making a database to sum up a system of electrical loads, connected via various buses. The idea is to be able to check no bus has its load rating exceeded. I am using APEX and an Oracle database.
I have two tables, LOAD
and BUS
.
LOAD
has the columns IDENTIFIER, PARENT_BUS, POWER
BUS
has the colums IDENTIFIER, PARENT_BUS
(This is just a simplification of the tables, but the other data is not relevant to this problem I don't think)
I have created a table structure that looks a little bit like this:
BUS 0 (root bus)
BUS 1
-LOAD 1000
-LOAD 1009
BUS 101
-LOAD 1001
-LOAD 1002
-LOAD 1007
BUS 102
-LOAD 1003
BUS 2
BUS 201
-LOAD 1004
-LOAD 1008
BUS 202
-LOAD 1005
-LOAD 1006
There are a set of buses that link to another bus in the bus table using a foreign key from BUS.PARENT_BUS
to BUS.IDENTIFIER
. The loads in the load table also link via a foreign key to a bus: i.e. LOAD.PARENT_BUS
to BUS.IDENTIFIER
. There would typically be hundreds or even thousands of loads, connected to several dozen buses.
Note that it is possible for a load to attach anywhere in the bus structure, and there can be any level of buses (although I only show two levels in the example - three or more levels would be uncommon but not impossible)
I need to sum all of the loads at each bus level, and have this feed up to the next-higher bus. i.e. for BUS 101
I need the sum of LOAD 1001 + LOAD 1002 + LOAD 1007
, then for BUS 1
I need the sum of BUS 101 + BUS 102 + LOAD 1000 + LOAD 1009
.
I have tried doing this:
SELECT bus.IDENTIFIER AS "PARENT", bus.PARENT_BUS AS "GRANDPARENT", SUM(load.POWER) AS "POWER", level
FROM BUS_DATA bus
LEFT JOIN LOAD_DATA load
ON load.PARENT_BUS = bus.IDENTIFIER
START WITH bus.PARENT_BUS = '0' -- This is the root that all buses eventually link to
CONNECT BY PRIOR bus.IDENTIFIER = bus.PARENT_BUS
GROUP BY bus.IDENTIFIER, bus.PARENT_BUS, level;
And this gives me
PARENT, GRANDPARENT, POWER, LEVEL
201 2 307.6 2
202 2 189.4 2
Which is correct, because with the data I used to generate this there are no loads attached to BUS 2. I also need to see a row:
PARENT, GRANDPARENT, POWER, LEVEL
2 0 497 1
i.e. the sum of 201 and 202, plus any loads that might be attached directly to BUS 2.
What can I do to have the summed power data feed up through the bus structure to the parent bus?