Is there an aggregate function that could return first non-null value within a group?
I may be misunderstanding why ROW NUMBER would not work for you. I do not have Oracle, but I did test this in SQL Server, and I believe it provides the results you requested:
WITH soTable AS
(
SELECT 'a' AS Name, null AS YearOfBirth
UNION ALL SELECT 'a', 2001
UNION ALL SELECT 'a', 2002
UNION ALL SELECT 'b', 1990
UNION ALL SELECT 'b', null
UNION ALL SELECT 'b', 1994
UNION ALL SELECT 'b', 1981
UNION ALL SELECT 'c', null
UNION ALL SELECT 'c', 2009
UNION ALL SELECT 'c', 2001
)
, soTableNoNulls AS
(
SELECT so.Name, so.YearOfBirth, ROW_NUMBER() OVER (PARTITION BY so.Name ORDER BY so.Name ASC) AS RowNumber
FROM soTable AS so
WHERE so.YearOfBirth IS NOT NULL
)
SELECT nn.Name, nn.YearOfBirth
FROM soTableNoNulls AS nn
WHERE nn.RowNumber = 1
If by "first" you mean the record with the lowest birth year, then you can do the following:
WITH s1 AS
(
SELECT 'a' AS name, NULL AS birth_year FROM dual
UNION ALL SELECT 'a', 2001 FROM dual
UNION ALL SELECT 'a', 2002 FROM dual
UNION ALL SELECT 'b', 1990 FROM dual
UNION ALL SELECT 'b', null FROM dual
UNION ALL SELECT 'b', 1994 FROM dual
UNION ALL SELECT 'b', 1981 FROM dual
UNION ALL SELECT 'c', null FROM dual
UNION ALL SELECT 'c', 2009 FROM dual
UNION ALL SELECT 'c', 2001 FROM dual
)
SELECT name, birth_year FROM (
SELECT name, birth_year
, FIRST_VALUE(birth_year IGNORE NULLS) OVER ( PARTITION BY name ORDER BY birth_year ) AS first_birth_year
FROM s1
) WHERE birth_year = first_birth_year
The advantage of using FIRST_VALUE()
over ROW_NUMBER()
is that the former will return multiple rows in the event of ties. For example, if you had another a
born in 2001 in your data, then the resulting data would look like this:
NAME BIRTH_YEAR
a 2001
a 2001
b 1981
c 2001
The ROW_NUMBER()
solution would return only one of the above rows. However, that could also be solved by using RANK()
.
If there is some other way of defining "first" (e.g., an entry date column), simply use that in the ORDER BY
clause of FIRST_VALUE()
.