Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: Nested Coalescing possible in SQL?
>> if an entity instance (one row) does not specify a value for one of
its field [sic]s it should inherit the values from its nearest parent
that does have a value for this field [sic]. <<
Rows are not records; fields are not columns; tables are not files. It drives me nuts to see people screw up the terms and therefore their mental model of how SQL works. Let's put this into a simplified nested set model which has more NULL-able columns than the payroll system of a major auto company:
CREATE TABLE Nodes
(node_id INTEGER NOT NULL PRIMARY KEY,
col_1 CHAR(5),
col_2 CHAR(5),
..);
CREATE TABLE Tree
(node_id INTEGER NOT NULL UNIQUE
REFERENCES Nodes(node_id),
lft INTEGER NOT NULL,
rgt INTEGER NOT NULL,
PRIMARY KEY (lft, rgt),
<< other tree constraints here >>
..);
Now update all the rows where any column has a NULL. You will have to run this update once for every level in the tree at most.
UPDATE Nodes
SET col_1
This probably scared you. The WHERE clause uses the fact that NULLs propagate; we don't care *which* column is NULL, so why look for useless details? That left outer join scalar subquery is how to get the immediate superiors (B= boss, E= employee) in a hierarchy. The COALESCE() will retain an existing non-NULL value. Received on Fri Jun 04 2004 - 19:35:43 CDT