Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / November 2006

Tip: Looking for answers? Try searching our database.

SQL Dummy

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jim - 25 Oct 2006 15:45 GMT
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.
Pachydermitis - 25 Oct 2006 16:11 GMT
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;

> Hi,
>
[quoted text clipped - 20 lines]
> 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.
deluxeinformation@gmail.com - 25 Oct 2006 19:25 GMT
> Hi,
>
[quoted text clipped - 20 lines]
> 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
Pachydermitis - 25 Oct 2006 23:20 GMT
Bruce,
your method could produce a large carteisan product based on the other
records in the table, and skew the results.

> > Hi,
> >
[quoted text clipped - 36 lines]
>
> Bruce
jim - 26 Oct 2006 02:41 GMT
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...

> Bruce,
> your method could produce a large carteisan product based on the other
[quoted text clipped - 39 lines]
>>
>> Bruce
deluxeinformation@gmail.com - 26 Oct 2006 18:48 GMT
> 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
Pachydermitis - 02 Nov 2006 23:19 GMT
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

> > Bruce,
> > your method could produce a large carteisan product based on the other
[quoted text clipped - 16 lines]
>
> Bruce
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.