0

I have a data table something along the lines of

table

The parentID is the schoolId of the parent. I work with MySQL 8.0.

I followed Building a hierarchical tree with a single SQL query, this question doesn't sum the values of the children and the parents. The most questions i find online are about the sum of the children and not the parents and the children. I tried it with a declare table and then adding the data to the table. Is there a way to do this with just sql? I don't have much experience with sql.

I want to get something along the lines of

wanted

For now it work with just counting the sum of the children. This is something like this

SELECT 
            id,
            parentId,
            year, 
            amountStudents,
            name
            ISNULL(( select sum(amountStudents) from School sibiling with (nolock)
                inner join School parent with (nolock) on parentId = sibling.parentId
                as SchoolbestuurOmzet
            FROM School.

This code might not work because i stripped some code from it. The basic concept was i compare the elements to see if they are sibling

Now i'm trying to it with something like this

DECLARE @result TABLE(
        id int NOT NULL,
        parentID int NULL,
        parentName varchar(100) NULL,
        year int NULL,
        name varchar(100) NULL,
        amountStudents int NULL,
        totalAmount int NULL
);
Hedley
  • 1,060
  • 10
  • 24
Maxime C.
  • 37
  • 11

0 Answers0