I have two tables that have top_depth, bottom_depth, lith_type. One defines the different layers found in a borehole and the other define where samples are extracted in each of the layers.
I would like to find all samples that intersects the layer boundaries. Although, I was able to solve my problem, I was hoping there was a more elegant solution. This is what I did:
CREATE TABLE lithology ( official_name TEXT, top_depth FLOAT, bottom_depth FLOAT, lith_type TEXT );
INSERT INTO lithology VALUES('B1', 0, 3,'SC');
INSERT INTO lithology VALUES('B1', 3, 11,'SS');
INSERT INTO lithology VALUES('B1', 11, 20,'STS');
INSERT INTO lithology VALUES('B1', 1, 21, 'SS');
CREATE TABLE sample_data ( official_name TEXT, sample_id TEXT, top_depth FLOAT, bottom_depth FLOAT, lith_type TEXT );
INSERT INTO sample_data VALUES('B1', 1, 1, 2,'SC');
INSERT INTO sample_data VALUES('B1', 2, 3, 4,'SS');
INSERT INTO sample_data VALUES('B1', 3, 6, 7,'SS');
INSERT INTO sample_data VALUES('B1', 4, 10.5, 11.5,'SS');
INSERT INTO sample_data VALUES('B1', 5, 14, 15,'SS');
INSERT INTO sample_data VALUES('B1', 6, 19, 20,'SS');
select
L.official_name, S.sample_id,
L.top_depth, L.bottom_depth, L.lith_type,
S.top_depth, S.bottom_depth, S.lith_type
from
lithology L, sample_data S
where
L.official_name = S.official_name
and
(S.bottom_depth > L.top_depth and L.top_depth > S.top_depth or
S.bottom_depth > L.bottom_depth and L.bottom_depth > S.top_depth or
S.top_depth < L.top_depth and S.bottom_depth > L.bottom_depth )
order by L.official_name,S.sample_id
This should show that sample_id=4 is the sample to check.
Best Answer
The simplest way to work on this is to follow Martin Fowler's (and many others as well) technique: http://c2.com/cgi/wiki?TestIfDateRangesOverlap
Note: SQL Server syntax
If the samples at boundary layer should not be reported, replace the comparison of
<=
with<
as mentioned by ypercubeᵀᴹ and is shown below:The
lyth_type
is just riding along in this since you did not mention how to use it.