Because (As @Sam Saffron said in comments) it is indeed much much easier to do this kind of stuff in procedural code, it means that your table is not designed properly for SQL.
In a SQL database, each row should describe a single entity (or a single relationship between entities). Failing to do so if a common enough design flaw. For example, consider this Payroll table:
CREATE TABLE Payroll
(
employee_number CHAR(10) NOT NULL
REFERENCES Personnel (employee_number),
effective_date DATE NOT NULL,
salary_amount DECIMAL(19, 4) NOT NULL
CHECK (salary_amount >= 0),
UNIQUE (effective_date, employee_number)
);
INSERT INTO Payroll (employee_number, effective_date, salary_amount)
VALUES ('U83GHVPSGP', '2001-01-01', 5000),
('U83GHVPSGP', '2002-01-01', 7000),
('U83GHVPSGP', '2002-01-01', 9000);
The problem here is that it is modelling periods but each period's end date is dervied from another row's start date i.e. the entity (being a single period of salary) is modelled using two rows and each start date is playing two roles in the database. A side effect is that a simple query such as, "Get me the period employee U83GHVPSGP was paid 5000 MNT" is non-trivial (from an implementation point of view, it will involve a correlated subquery which is likely to perform badly on a given SQL platform). The above table will suffer non-obvious anomalies e.g. deleting the row for which U83GHVPSGP received 7000 MNT will implicitly change the data on another rows i.e. it now seems like U83GHVPSGP was paid 5000 MNT until '2002-01-01' when this wasn't actually so.
The fact that you have a column named rowspan
is the most perfect example I've ever seen of a 'smell' for this kind of design flaw.
I don't mean to sound harsh. Your table no doubt makes perfect sense for procedural code, so use procedural code and not SQL.
SQL works best with set based solutions, so if you want to use SQL then consider redesigning your table to model entities in separate tables from the relationship between entities and ensure that both flavours of table do not split a single entity's/relationship's data across multiple rows.