I suggest two subquery levels with window functions instead of correlated subqueries:
SELECT *
FROM (
SELECT the_date, company, max(col3) OVER (PARTITION BY company, grp) AS col3
FROM (
SELECT *, count(col3) OVER (PARTITION BY company ORDER BY the_date) AS grp
FROM tbl
) sub1
) sub2
WHERE col3 IS NOT NULL
ORDER BY the_date, company;
Produces the requested result.
This assumes unique entries per (company, the_date)
. Should be much faster for tables with more than just a few rows. A (unique to enforce uniqueness?!) index would help performance a lot:
CREATE INDEX tbl_company_date_idx ON tbl (company, the_date);
How?
The aggregate function count()
ignores NULL values when counting. Used as aggregate-window function, it computes the running count of a the column according to the default window definition, which is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
. This results in the count being "stuck" for rows with NULL values, thereby forming a peer group that shares the same (non-null) value.
In the second window function, the only non-null value per group is easily extracted with max()
. The group before the first non-null value retains NULL, which is easily eliminated in the final SELECT
.
See: