473,321 Members | 1,667 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,321 software developers and data experts.

Lookup value query joining two tables

Two tables:

T1 (c1 int, TestVal numeric(18,2), ResultFactor numeric(18,2))--c1 is
the primary key.

T2 (x1 int, FromVal numeric(18,2), ToVal numeric(18,2), Factor
numeric(18,2))--x1 is the primary key. T2 contains non-overlapping
values. So for eg., a few rows in T2 may look like.

1, 51, 51.999, 51
2, 52, 52.999, 52
....
....
32, 82, 82.999, 82
....
....

T2 is basically a lookup table. There is no relationship between the
two tables T1 and T2. However, if the TestVal from T1 falls in the
range between FromVal and ToVal in T2, then I want to update
ResultFactor in T1 with the corresponding value of Factor from the T2
table.

------Example for illustration only---------------
Even though tables cannot be joined using keys, the above problem is a
very common one in our everyday life. For example T1 could be
employees PayRaise table, c1=EmployeeID, with "TestVal" representing
test scores (from 1 to 100). T2 representing lookup of the ranges,
with "Factor" representing percent raise to be given to the employee.
If TestVal is 65 (employee scored 65% in a test), and a row in T2
(FromVal=60, ToVal=70, Factor=12), then I would like to update 12 in
table T1 from T2 using sql;. Basically T2 (like a global table)
applies to all the employees, so EmpID cannot serve as a key in T2.
---------------------------------------------------------

Could anyone suggest how I would solve MY PROBLEM using sql? I would
like to avoid cursors and loops.

Reply appreciated.

Thanks
Jul 20 '05 #1
1 2110
Do you just want to join on BETWEEN:

....
FROM T1 JOIN T2 ON T1.testval BETWEEN T2.fromval AND T2.toval

or am I misunderstanding your question?
I want to update
ResultFactor in T1 with the corresponding value of Factor from the T2
table.


Why do you want to do that? Don't create redundant, derived results in your
table if you can avoid it. Do the calculation in a view or query.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2

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

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
1
by: gudia | last post by:
David: It is not a join as keys are not present in both the tables; between will just restrict the values for test_val. However, the following solution has been proposed by Joe Celko, which...
3
by: my-wings | last post by:
I've been reading about how evil Lookup fields in tables are, but I've got to be missing something really basic. I know this subject has been covered before, because I've just spent an hour or two...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
1
by: Robert | last post by:
I am trying to create a db for service providers by county. I'm relatively new to db programming, but I have done quite a bit of programming ranging from the old basic days up to doing some...
1
by: Paul H | last post by:
Say I have a table called tblPeopleInfo, one of the fields in the table is called FavouriteFruit. The FavouriteFruit field is a lookup field and will contain Apples, Oranges, Grapes etc..The list...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
11
by: Paul H | last post by:
Suppose I have a table called tblPeople and I want a field to illustrate whether each person prefers cats or dogs. I could do it one of three ways. 1. A plain text field Create a text field in...
7
by: samdev | last post by:
I have set up a table with a few fields that are set to required....all work fine except the fields that are Lookup from another table. What am I forgetting to set? Thanks!!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.