Skip to content
Advertisement

How retrieve all parent and child rows population in Oracle sql?

I have a table “TB_Population” with some records about the population from all over the world. at this time I want to calculate each title’s population in particular row and demonstrate each level in that table.

I have this table with the following data:

The expected output table should be as below

Thanks and best regards

Advertisement

Answer

The tricky part which I see here is you want the LEVEL of title from “BOTTOM TO TOP” and POPULATION from “TOP TO BOTTOM”. For example, AMERICA’s level has to be 2 which means the LEVEL has to be measured from AMERICA -> WORLD, but AMERICA’s population has to be 25 which is the sum of population measured from AMERICA -> NEWYORK. So, I tried this:

You can have a look at the simulation here: https://rextester.com/HFTIH47397. Hope this helps you

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement