473,703 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2124
Do you just want to join on BETWEEN:

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

or am I misunderstandin g 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
3068
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 solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche Nachricht-----
1
470
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 works. Thanks to everybody who contibuted. David and Joe, thanks so much. UPDATE T1 SET factor = (SELECT T2.factor
3
2915
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 reading about it on google, but there is something I still don't understand, and I'm hoping someone will be willing to explain it to me in small words. Let's say I have a table for addresses, and it includes a field for state. What I would...
3
10654
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 that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
1
3251
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 programming in the HotDocs software. I've kind of accomplished my goal in access, but I'm not quite there yet and figure I've really screwed something up. The db consists of 5 tables I believe. T1 is the main contact info, with an autonumber ID...
1
1899
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 can be added to by users of the database. What is the best way to construct this lookup? Should it lookup a Table/Query or a Value List? If it looks up to a Table/Query should the lookup table just have a single "text" field or should it have...
0
2212
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 correctly, posting to the wrong forum, or committing some other sort of faux pas. My team is developing a Windows Forms application using VS 2005 with SQL Server 2005 on the back end and we're having a problem using ComboBoxes data bound to lookup...
11
2650
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 tblPeople called PreferredPet. 2. A lookup field that stores text values. Create a text field in tblPeople called PreferredPetID and use it to lookup an identical text field in tblPreferredPets.
7
2585
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
8744
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8659
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9240
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7846
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2055
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.