473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Getting closest matching values

I apologise if this is a stupid question, but I would appreciated any
help on this subject.

I want to create a view (VIEW_1 in example below) where I take numeric
values from a field in one table (DEPTH_FROM in TABLE_1) and find the
closest matching values from another field in another table (DEPTH_AT
in TABLE_2) - the higher matching value and the lower matching value.
TABLE_1
BH_ID DEPTH_FROM
B1 10.6
B1 14.2
B1 16.1
B1 17.0

TABLE_2
BH_ID DEPTH_AT
B1 9
B1 10
B1 11
B1 12
B1 13
B1 14
B1 15
B1 16
B1 17
B1 18

VIEW_1
BH_ID DEPTH_FROM DEPTH_LOWER DEPTH_HIGHER
B1 10.6 10
11
B1 14.2 14
15
B1 16.1 16
17
B1 17.0 17
17

Any assistance would be appreciated.

Regards,
Hennie

Feb 20 '06 #1
2 3701
Should give you what you want
CREATE TABLE TABLE_1(BH_ID CHAR(2), DEPTH_FROM DECIMAL(5,1))

INSERT INTO TABLE_1(BH_ID,DEPTH_FROM) VALUES('B1',10.6)
INSERT INTO TABLE_1(BH_ID,DEPTH_FROM) VALUES('B1',14.2)
INSERT INTO TABLE_1(BH_ID,DEPTH_FROM) VALUES('B1',16.1)
INSERT INTO TABLE_1(BH_ID,DEPTH_FROM) VALUES('B1',17.0)
CREATE TABLE TABLE_2(BH_ID CHAR(2), DEPTH_AT DECIMAL(5,1))

INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',9)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',10)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',11)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',12)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',13)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',14)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',15)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',16)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',17)
INSERT INTO TABLE_2(BH_ID,DEPTH_AT) VALUES('B1',18)

GO

CREATE VIEW DEPTH_RANGES
AS
SELECT a.BH_ID,
a.DEPTH_AT AS DEPTH_LOWER,
b.DEPTH_AT AS DEPTH_HIGHER
FROM TABLE_2 a
INNER JOIN TABLE_2 b ON b.BH_ID=a.BH_ID
AND b.DEPTH_AT=(SELECT MIN(c.DEPTH_AT)
FROM TABLE_2 c
WHERE c.BH_ID=a.BH_ID
AND c.DEPTH_AT>a.DEPTH_AT)

GO

CREATE VIEW VIEW_1
AS
SELECT a.BH_ID,
b.DEPTH_LOWER,
a.DEPTH_FROM,
b.DEPTH_HIGHER
FROM TABLE_1 a
INNER JOIN DEPTH_RANGES b ON b.BH_ID=a.BH_ID
AND a.DEPTH_FROM >= b.DEPTH_LOWER
AND a.DEPTH_FROM < b.DEPTH_HIGHER
GO

Feb 20 '06 #2
Thanks Mark!

This helps a lot!

Regards,
Hennie

Feb 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
2
by: Joe | last post by:
Hello all! I need to display a list of names similar to a spell checker where the list is the closest match to the name entered. Not too sure where to begin with this - any suggestions? Thanks,...
2
by: vvyshak | last post by:
Hi all.. I have two tables such as cisco and ciscocom. and i wan to compare each row of ciscocom with cisco having same column values. i wan to get the count of matching columns for each row in...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
3
by: randy1200 | last post by:
I have a DataView (dv) that includes a StartDate DateTime column. I'd like to return the DataView record that contains a StartDate value closest to my IncidentDate value: string filter =...
9
by: CapCity | last post by:
We're rewritting an app using C# 2005 and it needs to read files in netCDF format. A dll is available for this and we've had success in calling its functions, unless it updates strings. We have...
6
by: Seth Illgard | last post by:
Hi. I need to obtain the exact postion of a div. I was using (variable and function names are in spanish, sorry): //Get X position function obtenerPosicionX(elemento) { var x=0;...
1
by: swellfr | last post by:
Hi i need a structure to store some 2D points and to be able to interpolate. The pb is that i need to find very very quickly what are the the closest values around my point. Is the...
5
by: p309444 | last post by:
Hi. I have an application in which the user can select a location and view it's distance from several Points Of Interests (POIs). When I retrieve these distances, I would also like to retrieve...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.