473,656 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a Query Result as a Column Name in a Different Query

Greetings,

I would like to use a query result as a column name in another query, I
can't seem to get this to work using Subqueries. Is there a good way to
do this?

Example:

Table "Employees" :
Name Title
Dave Manager
John Accountant
Ricky Fireman

Table "Rates":
Rate_Schedule Manager Accountant Fireman
A $5 $1 $2
B $8 $3 $6
C $7 $2 $1

Table "Projects":
Project Rate_Schedule
Mesa A
Houston B
Dallas A
Dulles C

I know that I could rearrage my information storage to make this easy
by combining these tables. But, I would like to keep the Rates table as
is so that it matches the contact documents. This is the way my
company has always thought about this subject and I hate to change our
culture because of a database limitation.

So, how can I formulate a series of queries that will get me the rate
for John for the Dallas project?

I am using Access 2002, and can code the solution in VB if necessary,
though I'd rather not, just for maintenance sake.

TIA,
Dave

May 1 '06 #1
3 2723
rs.Open "Select Title from Employees where Name = John"
Select case rs!Title
case "Manager"
SELECT Rates.Manager
FROM Rates INNER JOIN Projects ON
Rates.Rate_Sche dule = Projects.Rate_S chedule
WHERE Projects.Projec t = "Dallas"
case "Accountant "
SELECT Rates.Accountan t
FROM Rates INNER JOIN Projects ON
Rates.Rate_Sche dule = Projects.Rate_S chedule
WHERE Projects.Projec t = "Dallas"
case "Fireman"
SELECT Rates.Fireman
FROM Rates INNER JOIN Projects ON
Rates.Rate_Sche dule = Projects.Rate_S chedule
WHERE Projects.Projec t = "Dallas"
End Select

Hope this helps.

May 2 '06 #2
milam wrote:
Greetings,

I would like to use a query result as a column name in another query, I
can't seem to get this to work using Subqueries. Is there a good way to
do this?

Example:

Table "Employees" :
Name Title
Dave Manager
John Accountant
Ricky Fireman

Table "Rates":
Rate_Schedule Manager Accountant Fireman
A $5 $1 $2
B $8 $3 $6
C $7 $2 $1

Table "Projects":
Project Rate_Schedule
Mesa A
Houston B
Dallas A
Dulles C

I know that I could rearrage my information storage to make this easy
by combining these tables. But, I would like to keep the Rates table as
is so that it matches the contact documents. This is the way my
company has always thought about this subject and I hate to change our
culture because of a database limitation.

So, how can I formulate a series of queries that will get me the rate
for John for the Dallas project?

I am using Access 2002, and can code the solution in VB if necessary,
though I'd rather not, just for maintenance sake.

TIA,
Dave


Here's another way. After changing Name to EmployeeName and adding ID
fields to the tables from your example I decided to see if I could roll
a pseudo-normal schema using the same ideology as the one behind the
rational fortran (RATFOR) language paradigm. qryEmployees is used to
create what Employees should have been. Call this plan RATSCHEMA if
you'd like. An additional table 'Roles' is used to create the quasi
foreign key. Besides, who knows what else will break if you try to fix
it at this point?

Roles
RoleID AutoNumber
RoleName Text

RoleID RoleName
1 Manager
2 Accountant
3 Fireman

qryEmployees:
SELECT EmployeeID, EmployeeName,
Switch([EmployeeTitle]="Manager",1 ,[EmployeeTitle]="Accountant",2 ,[EmployeeTitle]="Fireman",3 )
AS RefRoleID FROM Employees;

!qryEmployees:
EmployeeID EmployeeName RefRoleID
1 Dave 1
2 John 2
3 Ricky 3

qryProjectRates :
SELECT EmployeeName, Project,
Switch(RefRoleI D=1,[Manager],RefRoleID=2,[Accountant],RefRoleID=3,[Fireman])
AS theRate FROM Rates INNER JOIN Projects ON Rates.Rate_Sche dule =
Projects.Rate_S chedule, qryEmployees INNER JOIN Roles ON
qryEmployees.Re fRoleID = Roles.RoleID WHERE qryEmployees.Em ployeeID=2
AND Projects.Projec tID=3;

!qryProjectRate s:
EmployeeName Project theRate
John Dallas $1

James A. Fortune
CD********@Fort uneJames.com

May 2 '06 #3
I know you're not going to like this, but the structure of your Rates
table is not very helpful. If you structure it like this:

CREATE TABLE Rates(
RateSchedule CHAR,
EmployeeType VARCHAR2(25).
HourlyRate DECIMAL,
PRIMARY KEY (RateSchedule, EmployeeType))

then the joins in your query will do the work for you. I would
definitely go this route if you can have lots of employee types and
rate schedules. In the long run, it will much easier to maintain.

Just my two cents....

May 2 '06 #4

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

Similar topics

20
10132
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
9
3122
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
4
2142
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets over the years. The idea is that from any widget in the database you can look forward and backward to see different versions of the widget through the years. Here are the tables: WIDGETS widget_id name
6
8513
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
3
2530
by: bob.herbst | last post by:
I have been trying to use HTML_Table from PEAR to write a PHP script that will access a database and retrieve my data into an HTML table that can be sorted by column. Currently I am using the script below, which does not include sorting (I want the basic table to work first) but all I get is the column headers and no data in the column can anyone tell me how to fix this problem and have the script access my database to display the table...
10
6721
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that contains only tables. I have linked the tables for the front end to the back end database. I am trying to set the recordsource of a form to a query established by the user to narrow the scope but I don't want to display the form if there are no...
3
1865
by: fstenoughsnoopy | last post by:
Ok the complete story. I have a Contact Table, Query and Form, that are used to input and store the contact info for customers. They have FirstName, LastName and Address as the primary key fields to keep out duplicates. I am trying to put a full name box on the query, that uses the FirstName, LastName and Middle Initial and puts them together to form a full name. That I have so far. On my order database(consisting of a master table with...
2
2352
by: Venk | last post by:
hi all, I saw one reply to arun on the subject "Dynamic Query in Ms-Access" by one Mr Rick I found it very useful. Now to extend this solution forward I have the following situation. I have a Query Which Retrives one or more records from the following table(MASTER_TABLE) based on the user requirement
16
2153
by: kinkin83 | last post by:
Dear All, i would like to get different result from 1 column in 1 query. the details result is below database name: Data table name: table1 column: id, phoneno, timeframe, mo if MO=1 means MO else MO=0 means MT
0
8382
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
8297
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
8816
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
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
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...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1600
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.