473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Dummy

jim
Hi,

I have two queries:

qReg:

SELECT tblRegionalExperience.ConsultantID, tblRegionalExperience.RegionID
FROM tblRegionalExperience
WHERE (((tblRegionalExperience.RegionID)=1));
qLang:

SELECT tblLanguageSkills.ConsultantID, tblLanguageSkills.LanguageID
FROM tblLanguageSkills
WHERE (((tblLanguageSkills.LanguageID)=1));
i have these joined together in qAll:

SELECT qLang.ConsultantID, qLang.LanguageID, qReg.ConsultantID,
qReg.RegionID
FROM qLang INNER JOIN qReg ON qLang.ConsultantID = qReg.ConsultantID;

but i would like be able to write qAll without reference to qReg or
qLang. my efforts so far have been hopeless any help much appreciated.
Oct 25 '06 #1
6 2047
You can use what we call derived tables - which are just sub queries -
as long as you have at least Access 2000.
qry a: Select a from x
qry b: Select b From y
qAll Select a,b From (Select a from x) as aliasA, (Select b From y) as
Aliasb

AliasA & AliasB are just a names I am giving the derived tables, call
them whatever you want as long as it is not already being used
somewhere in the qry. Rather than using the letter t prefix I use for
the tables, I use an a (eg. aCustomers)

There are a couple issues.
1) Once this is created "if" you go back into the sql of the qry,
access messes up the syntax for you and changes your () on the derived
tables to [] so you have to change them back.
2) Access doesn't like to have these as the recordsource for forms or
reports - you will have problems unless you save it as a qry and then
use the qry as the recordsource.
3) It's hellacious to debug your sql.

Other than that I use them all the time.

Good luck
Pachydermitis

SELECT aLang.ConsultantID, aLang.LanguageID, aReg.ConsultantID,
aReg.RegionID
FROM (SELECT tblLanguageSkills.ConsultantID,
tblLanguageSkills.LanguageID
FROM tblLanguageSkills
WHERE (((tblLanguageSkills.LanguageID)=1))) as aLang INNER JOIN (SELECT
tblRegionalExperience.ConsultantID, tblRegionalExperience.RegionID
FROM tblRegionalExperience
WHERE (((tblRegionalExperience.RegionID)=1))) as aReg ON
aLang.ConsultantID = aReg.ConsultantID;
jim wrote:
Hi,

I have two queries:

qReg:

SELECT tblRegionalExperience.ConsultantID, tblRegionalExperience.RegionID
FROM tblRegionalExperience
WHERE (((tblRegionalExperience.RegionID)=1));
qLang:

SELECT tblLanguageSkills.ConsultantID, tblLanguageSkills.LanguageID
FROM tblLanguageSkills
WHERE (((tblLanguageSkills.LanguageID)=1));
i have these joined together in qAll:

SELECT qLang.ConsultantID, qLang.LanguageID, qReg.ConsultantID,
qReg.RegionID
FROM qLang INNER JOIN qReg ON qLang.ConsultantID = qReg.ConsultantID;

but i would like be able to write qAll without reference to qReg or
qLang. my efforts so far have been hopeless any help much appreciated.
Oct 25 '06 #2

jim wrote:
Hi,

I have two queries:

qReg:

SELECT tblRegionalExperience.ConsultantID, tblRegionalExperience.RegionID
FROM tblRegionalExperience
WHERE (((tblRegionalExperience.RegionID)=1));
qLang:

SELECT tblLanguageSkills.ConsultantID, tblLanguageSkills.LanguageID
FROM tblLanguageSkills
WHERE (((tblLanguageSkills.LanguageID)=1));
i have these joined together in qAll:

SELECT qLang.ConsultantID, qLang.LanguageID, qReg.ConsultantID,
qReg.RegionID
FROM qLang INNER JOIN qReg ON qLang.ConsultantID = qReg.ConsultantID;

but i would like be able to write qAll without reference to qReg or
qLang. my efforts so far have been hopeless any help much appreciated.
SELECT tblLanguageSkills.ConsultantID,
tblLanguageSkills.LanguageID,
tblRegionalExperience.ConsultantID,
tblRegionalExperience.RegionID
FROM tblRegionalExperience
INNER JOIN tblLanguageSkills
ON tblRegionalExperience.ConsultantID = tblLanguageSkills.ConsultantID
WHERE (tblRegionalExperience.RegionID=1) AND
(tblLanguageSkills.LanguageID=1)

should do what you want, but there's really no reason to include both
tblLanguageSkills.ConsultantID and tblRegionalExperience.ConsultantID
since these should be equal because of your ON clause.

Bruce

Oct 25 '06 #3
Bruce,
your method could produce a large carteisan product based on the other
records in the table, and skew the results.

de***************@gmail.com wrote:
jim wrote:
Hi,

I have two queries:

qReg:

SELECT tblRegionalExperience.ConsultantID, tblRegionalExperience.RegionID
FROM tblRegionalExperience
WHERE (((tblRegionalExperience.RegionID)=1));
qLang:

SELECT tblLanguageSkills.ConsultantID, tblLanguageSkills.LanguageID
FROM tblLanguageSkills
WHERE (((tblLanguageSkills.LanguageID)=1));
i have these joined together in qAll:

SELECT qLang.ConsultantID, qLang.LanguageID, qReg.ConsultantID,
qReg.RegionID
FROM qLang INNER JOIN qReg ON qLang.ConsultantID = qReg.ConsultantID;

but i would like be able to write qAll without reference to qReg or
qLang. my efforts so far have been hopeless any help much appreciated.

SELECT tblLanguageSkills.ConsultantID,
tblLanguageSkills.LanguageID,
tblRegionalExperience.ConsultantID,
tblRegionalExperience.RegionID
FROM tblRegionalExperience
INNER JOIN tblLanguageSkills
ON tblRegionalExperience.ConsultantID = tblLanguageSkills.ConsultantID
WHERE (tblRegionalExperience.RegionID=1) AND
(tblLanguageSkills.LanguageID=1)

should do what you want, but there's really no reason to include both
tblLanguageSkills.ConsultantID and tblRegionalExperience.ConsultantID
since these should be equal because of your ON clause.

Bruce
Oct 25 '06 #4
jim
Thanks guys, have done some reading on sub queries and managed to make
some real progress with this, which i have been struggling with for over
a week.... many thanks for your input...

Pachydermitis wrote:
Bruce,
your method could produce a large carteisan product based on the other
records in the table, and skew the results.

de***************@gmail.com wrote:
>jim wrote:
>>Hi,

I have two queries:

qReg:

SELECT tblRegionalExperience.ConsultantID, tblRegionalExperience.RegionID
FROM tblRegionalExperience
WHERE (((tblRegionalExperience.RegionID)=1));
qLang:

SELECT tblLanguageSkills.ConsultantID, tblLanguageSkills.LanguageID
FROM tblLanguageSkills
WHERE (((tblLanguageSkills.LanguageID)=1));
i have these joined together in qAll:

SELECT qLang.ConsultantID, qLang.LanguageID, qReg.ConsultantID,
qReg.RegionID
FROM qLang INNER JOIN qReg ON qLang.ConsultantID = qReg.ConsultantID;

but i would like be able to write qAll without reference to qReg or
qLang. my efforts so far have been hopeless any help much appreciated.
SELECT tblLanguageSkills.ConsultantID,
tblLanguageSkills.LanguageID,
tblRegionalExperience.ConsultantID,
tblRegionalExperience.RegionID
FROM tblRegionalExperience
INNER JOIN tblLanguageSkills
ON tblRegionalExperience.ConsultantID = tblLanguageSkills.ConsultantID
WHERE (tblRegionalExperience.RegionID=1) AND
(tblLanguageSkills.LanguageID=1)

should do what you want, but there's really no reason to include both
tblLanguageSkills.ConsultantID and tblRegionalExperience.ConsultantID
since these should be equal because of your ON clause.

Bruce
Oct 26 '06 #5

Pachydermitis wrote:
Bruce,
your method could produce a large carteisan product based on the other
records in the table, and skew the results.
A Cartesian product would be the result of a cross join, not an inner
join, in that a Cartesian product returns all rows from the right side
of a join for each row in the left side. I'm not seeing a situation in
which our two queries would produce different results. If the intent
is to return a set of consultants from a specified region with a
specified language skill then both queries do this. However, a simple
select without subqueries suffices for this task as best I can tell.
That being said, I'm always up for learning something new or being
shown the error of my ways. I really just don't see the necessity of
subqueries for this. I think we've simply reversed the order in which
we are retrieving our results. Your subqueries first filter each table
separately and then perform a join. I simply join first and then
filter. Please let me know if I am overlooking something. It wouldn't
be the first time :)

Bruce

Oct 26 '06 #6
Hi Bruce,
I hope that I did not offend you at all. Since we don't know all the
data in the tables and how it relates to other tables, I was answering
the question in a way that I was sure would not have any undesired
results.

You can also get an cartesian product if you are joining fields that
are not primary keys. for example:
Table1
id prod Cust Date
1 123 2 1/1/2001
2 456 2 1/1/2001
3 123 1 2/1/2001
4 123 2 2/1/2001

Table2
ID cust inv amt
22 1 555 5
23 2 666 10
24 2 777 5

SELECT Table1.Cust, Sum(Table2.amt) AS SumOfamt, count(prod) as
CountOfProd
FROM Table1 INNER JOIN Table2 ON Table1.Cust = Table2.cust
GROUP BY Table1.Cust;

the invoice total for cust 2 will be 45 when it should be 15 because we
need other joins or sub queries.

SELECT aTable1.cust AS Expr1, Sum(Table2.amt) AS SumOfamt,
Count(aTable1.prod) AS CountOfprod
FROM (select cust, count(Table1.prod) as prod FROM Table1 group by
cust) AS aTable1 INNER JOIN Table2 ON aTable1.cust = Table2.cust
GROUP BY aTable1.cust;

will give us the correct numbers.

Another time derived tables can be a life saver is when you have huge
recordsets that you are joining. Say you have two tables that have 5
million rows each. If you can limit each with a derived table, then
join the smaller sets, you can speed up your data pull quite a bit. 5
million won't fly in access, but on some of the more robust engines I
have run into that scenario.

That being said I also may be overlooking something, and love this
forum because of all the stuff I learn from it. :)

P

de***************@gmail.com wrote:
Pachydermitis wrote:
Bruce,
your method could produce a large carteisan product based on the other
records in the table, and skew the results.

A Cartesian product would be the result of a cross join, not an inner
join, in that a Cartesian product returns all rows from the right side
of a join for each row in the left side. I'm not seeing a situation in
which our two queries would produce different results. If the intent
is to return a set of consultants from a specified region with a
specified language skill then both queries do this. However, a simple
select without subqueries suffices for this task as best I can tell.
That being said, I'm always up for learning something new or being
shown the error of my ways. I really just don't see the necessity of
subqueries for this. I think we've simply reversed the order in which
we are retrieving our results. Your subqueries first filter each table
separately and then perform a join. I simply join first and then
filter. Please let me know if I am overlooking something. It wouldn't
be the first time :)

Bruce
Nov 2 '06 #7

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

Similar topics

2
1787
by: Christian Giordano | last post by:
Hi guys, I've an hosting in media temple (appliance-server) with plesk 7.5.1. I'm trying to bring make a CMS I did in another server to work there. The issues is related the file uploading, in fact...
0
1293
by: Jonathan Fine | last post by:
I'd appreciate comments on what I'm doing. Please be kind, I'm a new kid on the block. Here's my application. I'm writing some test code. (I'm a recent convert to unit testing.)
2
5129
by: JohnnyOnTheSpot | last post by:
If I select from a table and, for example, group by the month or day to determine how much activity there is day to day or month to month, it will only return months and days with a record and...
4
2902
by: wireless | last post by:
I've written code that dynamically builds an sql query based on various constraint possibilities. The problem is the code would have been very complex had I not come up with a dummy constraint...
1
1536
by: CSDunn | last post by:
Hello, I am working with a vb6 Windows application that runs on Tablet PC's. There are about five SQL Server 2000 databases that tie into the application. SQL Server MSDE runs on the tablets,...
1
2377
by: nek | last post by:
Greetings, We try to automate database creation via script which gets executed remotely for a number of servers. This works fine for most of the servers. However, it occasionally fails some...
1
1452
by: markjcostello | last post by:
Hi, I have a service running on my machine that generates continuous stats in xml. I want to constantly reference an xslt file (using <?xml-stylesheet type="text/xsl" href="Test3.xsl"?>) within...
15
3077
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
3
4228
by: Simon Brooke | last post by:
As various people will have noticed, I've been having a lot of trouble with XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and it worked well enough for me, so it's been...
35
21491
by: erik gartz | last post by:
Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can...
0
7260
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
7384
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,...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
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...
1
5089
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...
0
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.