How can I avoid the duplicated subquery in the following T-SQL statement:
update r set
column1=(select top 1 max(column1) from region r2 where (overlapping test conditions)),
column2=(select top 1 max(column2) from region r2 where (overlapping test conditions))
from region r
Basically, I have a table containing rectangle regions. For each overlapped region, I need to set some columns to the aggregation values of that overlapped region.
Thanks
EDIT: I am adding a over-simplified overlap condition here:
(r.left >= r2.left and r.left < r2.right) or (r.right <= r2.right and r.right > r.left)
The point is: both r and r2 will be referenced in the subquery. It seems like this is a perfect situation for common table expression but I cannot figure out how to use it for each record.