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

Help with table names in a query

I have a query that uses a person's state and other demographic information to look up rate factors. The rate factors are stored in tables. Each state has its own table (each state will have at least 15,000 records). I can get the rate factors to pull up correctly if I use a bunch of nested IIF statements. For example;

IIF(tblPerson.State = "AL", (Select [Factor] from [tblAL] where ...), IIF(tblPerson.State = "AZ", (Select [Factor from [tblAZ] where...), IIF(...

you get the idea - on and on for each state. It returns a single value. Is there an easier way to do this? We're talking 50 states here and my "where" conditions are somewhat complex - making the expression for this field extremely unwieldy.

Also, while we're at it, is there a way to do this for table fields as well. For instance, let's say I've got 50 fields in a table each corresponding to a state - is there a way to select the field name based on the person's state. Again, I can do it with nested conditionals, but it's messy:

IIF(tblPerson.State = "AL", (Select [fldAL] from [Table] Where ...), IIF(tblPerson.State = "AZ", (Select [fldAZ] from [Table] Where ...), IIF...
Nov 15 '06 #1
4 1466
cyberdwarf
218 Expert 100+
Couple of thoughts...
  1. You could normalize your data by pulling all your tables into one and adding the state code as an index
  2. You could use SQL to create a SELECT query with UNION links to each bit

Of course you may not want normalized data coz of the number of records and the potential performance hit, but IMHO it sure as hell beats trying to code it your way..

HTH

Steve
Nov 15 '06 #2
MMcCarthy
14,534 Expert Mod 8TB
Create a union query, you can only do this in sql view by the way. Since I don't know what the where statement is based on I am only guessing its included here. If you tell me what it is I'll have a better idea.

SELECT [Factor] FROM [tblAL]
WHERE ....
UNION
SELECT [Factor] FROM [tblAZ]
WHERE ....
UNION
SELECT [Factor] FROM ... etc.

This will give you one query to join to tblPerson

I can help a lot more if you give me the relevant fields from both tables and the relationship between the table and explain the WHERE condition you're using and why...
Nov 15 '06 #3
I appreciate your patience. I am pretty new to SQL programming. As an example, I'll use one of my simpler tables (there are 10 tables for each state). Let's call there are 4 fields in this table. The first two are lookup fields - Spouse (Y or N) and Children (0 to 10 without spouse, 0 to 9 with spouse). So, there are 20 combinations of these two fields. Then there are two factors stored in this table, one called BMM, the other called RX.

To get the BMM factor for a prospect, I would normally use the following SELECT query:

SELECT [BMM] from [tblAL]
WHERE [tblAL].[SpouseYN] = [tblProspectInfo].[SpouseYN] AND [tblAL].[Children] = [tblProspectInfo].[Children]

Unfortunately, not all the tables are this simple. They have more lookup fields that are used in the where statement. This results in some huge tables for each state. All the Queries that get factors from these tables have a similar form though:

SELECT [field] from [tblState]
WHERE {info about the prospect from tblProspectInfo matches lookup fields}

Does this give you enough info to help further? When I tried to make a Union query on just two states, using the example you gave me, it was treating the fields from the tblProspectInfo table in the "Where" clause as parameters and asked for user input.
Nov 15 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
SELECT [BMM] from [tblAL]
WHERE [tblAL].[SpouseYN] = [tblProspectInfo].[SpouseYN] AND [tblAL].[Children] = [tblProspectInfo].[Children]

I don't see how this ever worked as you are not referencing the second table.

SELECT [BMM]
FROM [tblAL] INNER JOIN tblProspectInfo
ON ([tblAL].[SpouseYN] = [tblProspectInfo].[SpouseYN] AND [tblAL].[Children] = [tblProspectInfo].[Children])
UNION
SELECT [BMM]
FROM [tblAZ] INNER JOIN tblProspectInfo
ON ([tblAZ].[SpouseYN] = [tblProspectInfo].[SpouseYN] AND [tblAL].[Children] = [tblProspectInfo].[Children])
UNION

etc

GROUP BY [BMM];

This would be a lot easier if you weren't trying to join on more than one value like this. This is why Primary and Foreign keys are so important.

If you had a foreign key reference in each of the tables to the primary key of the tblState you could just join on that key.

I think you are going to continue to have a lot of problems unless you go back and look at the way you have created your tables.

Check out the post at the top of the Access Forum called Links to Useful Sites.

http://www.thescripts.com/forum/thread562851.html

There is a link in there on how to structure your tables. Going back now and fixing them will save you a lot of time later on.

Have a good look at the site and try reforming your tables yourself then if you've any questions please ask.


I appreciate your patience. I am pretty new to SQL programming. As an example, I'll use one of my simpler tables (there are 10 tables for each state). Let's call there are 4 fields in this table. The first two are lookup fields - Spouse (Y or N) and Children (0 to 10 without spouse, 0 to 9 with spouse). So, there are 20 combinations of these two fields. Then there are two factors stored in this table, one called BMM, the other called RX.

To get the BMM factor for a prospect, I would normally use the following SELECT query:

SELECT [BMM] from [tblAL]
WHERE [tblAL].[SpouseYN] = [tblProspectInfo].[SpouseYN] AND [tblAL].[Children] = [tblProspectInfo].[Children]

Unfortunately, not all the tables are this simple. They have more lookup fields that are used in the where statement. This results in some huge tables for each state. All the Queries that get factors from these tables have a similar form though:

SELECT [field] from [tblState]
WHERE {info about the prospect from tblProspectInfo matches lookup fields}

Does this give you enough info to help further? When I tried to make a Union query on just two states, using the example you gave me, it was treating the fields from the tblProspectInfo table in the "Where" clause as parameters and asked for user input.
Nov 15 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: William Gill | last post by:
I have been trying to pass parameters as indicated in the api. when I use: sql= 'select * from %s where cusid = %s ' % name,recID) Cursor.execute(sql) it works fine, but when I try : sql=...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Randy | last post by:
Access= 2002 I'm NOT a Programmer, but I have used VB in the past to do some things ( Spaghetti Code King) so I have some understanding of Coding I need to replace a text field (teacher) in...
1
by: Melissa Kay Beeline | last post by:
OK, here's the sitch : we have an access control system at work that registers ever entry/exit of every employee. I recently made some queries in Access so the ppl in HR could make reports (who...
2
by: news.uow.edu.au | last post by:
Any suggestions for the following would be gratefully accepted. I have a table which includes name and interest fields e.g., contact interest smith carpentry jones gardening...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: Jordan M. | last post by:
Hi, Hoping to get some help modifying the following query that I have... TABLE: NAMES ID, FirstName, LastName TABLE: EMAILS ID,LinkID,Email,LastUpdateDate
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.