473,508 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL statement on inner join that hits another table twice?

hi group --

Im having a nested inner join problem with an Access SQl
statement/Query design. Im running the query from ASP and not usng the
access interface at all. Here's the tables:

tblEmployees
empId -- EmpName -- EmpRole -- EmpManager
-------....------------.... ---------....---------------
1........ dan yella..........1..........2
2.........joe brown........2...........5
3.........steve green......3...........4
4.........tom terrific........4..........5
4.........nacho libre........5..........5
tblRoles
RoleId -- RoleName
--------... -------------
1.........Pencil Jockey
2.........Monkey Manager
3.........Client Harrasser
4.........Teleharassment Manager
5.........Big Cheese
tblSchedule
SchedId -- EmpId -- Days
----------...-------....------
1.............1.........M-F
2.............2.........F,S,Su
I would like run a basic inner join query that will result in the
following result sets displayed:

Emp Name ---- EmpRole ----- EmpSchd ---- EmpBoss ----- EmpBossRole
------------...-----------------..-------------...--------------...---------------
Dan Yella....Pencil Jockey....M-F........Joe Brown......Monkey Manager
Joe Brown..Monkey Manager...F,S,Su...Nacho libre......Big Cheese
Stv Green..Client Harrasser..M-F,Su...Tom Terrific......Tele Manager

Im having trouble with the inner join that has to hit the Employees
table twice to get both the 'emp Name' and then the emp's 'manager's
name'. I have no idea how to do that and have tried about 15
different flavors of SQL inner join syntaxes, and still no luck .

Anyone have a suggestion, or even better, a pointer to a site with an
example? I know Im gonna kick myself when I see it, it's gotta be
something simple Im overlooking.

Thanks,
Dave
Sep 19 '06 #1
6 9284
You don't say what you have tried. Include your best guess!
--
Bas Cost Budde
Holland
Sep 19 '06 #2
Try your query inside an Access mdb. If the query doesn't work inside
the Access mdb then you can find out what is wrong locally. That is
much easier than trying to debug sql from an .asp file. If the query
does work correctly inside the Access mdb - then the problem is not with
the query - it is with the .asp file - usually missing a tag <% %>.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 19 '06 #3
On Tue, 19 Sep 2006 13:26:23 GMT,
dm*****@mungedRemovethisAmericanways.com wrote:
>>hi group --

Im having a nested inner join problem with an Access SQl
statement/Query design. Im running the query from ASP and not usng the
access interface at all. Here's the tables:

tblEmployees
empId -- EmpName -- EmpRole -- EmpManager
-------....------------.... ---------....---------------
1........ dan yella..........1..........2
2.........joe brown........2...........5
3.........steve green......3...........4
4.........tom terrific........4..........5
4.........nacho libre........5..........5
tblRoles
RoleId -- RoleName
--------... -------------
1.........Pencil Jockey
2.........Monkey Manager
3.........Client Harrasser
4.........Teleharassment Manager
5.........Big Cheese
tblSchedule
SchedId -- EmpId -- Days
----------...-------....------
1.............1.........M-F
2.............2.........F,S,Su
I would like run a basic inner join query that will result in the
following result sets displayed:

Emp Name ---- EmpRole ----- EmpSchd ---- EmpBoss ----- EmpBossRole
------------...-----------------..-------------...--------------...---------------
Dan Yella....Pencil Jockey....M-F........Joe Brown......Monkey Manager
Joe Brown..Monkey Manager...F,S,Su...Nacho libre......Big Cheese
Stv Green..Client Harrasser..M-F,Su...Tom Terrific......Tele Manager

Im having trouble with the inner join that has to hit the Employees
table twice to get both the 'emp Name' and then the emp's 'manager's
name'. I have no idea how to do that and have tried about 15
different flavors of SQL inner join syntaxes, and still no luck .

Anyone have a suggestion, or even better, a pointer to a site with an
example? I know Im gonna kick myself when I see it, it's gotta be
something simple Im overlooking.

Thanks,
Dave
>On Tue, 19 Sep 2006 16:00:19 +0200,
Bas Cost Budde <b.*********@dev.null.comwrote:
>You don't say what you have tried. Include your best guess!
Here's my base query that Ive been working off of, but it doesn't
lookup up the details, in the Role or Emp tables it just returns
integers for the columns:

SELECT emp.empName as EmpName,
emp.EmpRole as EmpRole,
sched.Days as EmpSched,
emp.EmpManager as EmpBoss,
role.empRole as EmpBossRole
FROM tblSchedule as Sched
INNER JOIN (tblemployees as emp
ON sched.empid = emp.empid)
INNER JOIN (tblRoles as role
ON emp.role = role.roleid);
Off that base statement, Ive plugged in and floated 20 -30 variations
at this point. I didnt mention in my OP, but Im trying to avoid
subqueries by using the INNER JOIN syntax that might slow down the
statements execution. Im just not that familiar with explictly
writing inner joins, much less Access 2000 syntax, to boot.

Thanks

Sep 19 '06 #4
To get an employee manager:

SELECT emp.empName, boss.empname AS empBoss
FROM tblemployees AS boss INNER JOIN tblemployees AS emp
ON boss.empid=emp.empmanager;

In your query,

SELECT emp.empName as EmpName,
emp.EmpRole as EmpRole,
sched.Days as EmpSched,
emp.EmpManager as EmpBoss,
role.empRole as EmpBossRole
FROM tblSchedule as Sched
INNER JOIN (tblemployees as emp
ON sched.empid = emp.empid)
INNER JOIN (tblRoles as role
ON emp.role = role.roleid);

you SELECT the emprole fields. Select names instead and you should have
the result you sketch. My statement would be this:

SELECT emp.empName AS EmpName, role.RoleName AS EmpRole, sched.Days AS
EmpSchd, boss.empName AS EmpBoss, bossrole.RoleName AS EmpBossRole
FROM (((tblEmployee AS emp INNER JOIN tblSchedule AS sched ON
sched.empid=emp.empid) INNER JOIN tblEmployee AS boss ON
emp.empmanager=boss.empid) INNER JOIN tblRoles AS role ON
emp.empRole=role.roleID) INNER JOIN tblRoles as bossrole ON
boss.empRole=bossrole.roleID
Here's my base query that Ive been working off of, but it doesn't
lookup up the details, in the Role or Emp tables it just returns
integers for the columns:

SELECT emp.empName as EmpName,
emp.EmpRole as EmpRole,
sched.Days as EmpSched,
emp.EmpManager as EmpBoss,
role.empRole as EmpBossRole
FROM tblSchedule as Sched
INNER JOIN (tblemployees as emp
ON sched.empid = emp.empid)
INNER JOIN (tblRoles as role
ON emp.role = role.roleid);
Off that base statement, Ive plugged in and floated 20 -30 variations
at this point. I didnt mention in my OP, but Im trying to avoid
subqueries by using the INNER JOIN syntax that might slow down the
statements execution. Im just not that familiar with explictly
writing inner joins, much less Access 2000 syntax, to boot.

Thanks
--
Bas Cost Budde
Holland
Sep 19 '06 #5
>Here's my base query that Ive been working off of, but it doesn't
lookup up the details, in the Role or Emp tables it just returns
integers for the columns:

SELECT emp.empName as EmpName,
emp.EmpRole as EmpRole,
sched.Days as EmpSched,
emp.EmpManager as EmpBoss,
role.empRole as EmpBossRole
FROM tblSchedule as Sched
INNER JOIN (tblemployees as emp
ON sched.empid = emp.empid)
INNER JOIN (tblRoles as role
ON emp.role = role.roleid);
Off that base statement, Ive plugged in and floated 20 -30 variations
at this point. I didnt mention in my OP, but Im trying to avoid
subqueries by using the INNER JOIN syntax that might slow down the
statements execution. Im just not that familiar with explictly
writing inner joins, much less Access 2000 syntax, to boot.

Thanks
Dave
On Tue, 19 Sep 2006 19:38:08 +0200, Bas Cost Budde
<b.*********@dev.null.comwrote:
>To get an employee manager:

SELECT emp.empName, boss.empname AS empBoss
FROM tblemployees AS boss INNER JOIN tblemployees AS emp
ON boss.empid=emp.empmanager;

In your query,

SELECT emp.empName as EmpName,
emp.EmpRole as EmpRole,
sched.Days as EmpSched,
emp.EmpManager as EmpBoss,
role.empRole as EmpBossRole
FROM tblSchedule as Sched
INNER JOIN (tblemployees as emp
ON sched.empid = emp.empid)
INNER JOIN (tblRoles as role
ON emp.role = role.roleid);

you SELECT the emprole fields. Select names instead and you should have
the result you sketch. My statement would be this:

SELECT emp.empName AS EmpName, role.RoleName AS EmpRole, sched.Days AS
EmpSchd, boss.empName AS EmpBoss, bossrole.RoleName AS EmpBossRole
FROM (((tblEmployee AS emp INNER JOIN tblSchedule AS sched ON
sched.empid=emp.empid) INNER JOIN tblEmployee AS boss ON
emp.empmanager=boss.empid) INNER JOIN tblRoles AS role ON
emp.empRole=role.roleID) INNER JOIN tblRoles as bossrole ON
boss.empRole=bossrole.roleID
ok, thanks much, Bas. That's exactly the column data I was trying to
return-- but even better, I think I now grasp the pattern needed to
construct those type of joins in the future. The key point i was
missing is that there needs to be a separate inner join to all 'lookup
tables' for each item that needs it. Just because youre hitting the
'role' table for the a record to get the emp's role info on one join,
you still have to create yet another join to get the emp's manager's
role too--- even though they are both part of the same record. Got
it.

------------------------------------
Four quick follow up questions so I can digest and fully grok what
you've shown.

1.The query as is, pulls back just one row from the emp table. No
biggie. Im sure I can hunt down why, obviously one of the joins is
acting as a limiting filter. But I was expecting the query to return
one row for every employee in the emp table, since there is no 'where
clause' to filter the data. Just wondering it that's normal among
nested inner joins?

2. Do you personally construct the joins from the inner joins out, or
the outter joins in? Does Access care about the order? ( Ill
experiment a bit and see myself, just wondering how others with
experience do it?)

2.Performancewise, if you know, does the jet engine take a speed hit
for each additional 'inner join' a query has? IOW, is 6 'inner joins"
is 'x' amont slower than 3 inner joins?

3. Does the order of the nested 'inner joins' make a difference to
performance? ( again, Ill play with it later, just curious for a quick
answer)

Thanks again for setting me on the correct path.

Dave
Sep 19 '06 #6
ok, thanks much, Bas. That's exactly the column data I was trying to
return-- but even better, I think I now grasp the pattern needed to
construct those type of joins in the future. The key point i was
missing is that there needs to be a separate inner join to all 'lookup
tables' for each item that needs it. Just because youre hitting the
'role' table for the a record to get the emp's role info on one join,
you still have to create yet another join to get the emp's manager's
role too--- even though they are both part of the same record. Got
it.
Good! And that for a forgotten "AIR CODE" warning :) Let's get back to
that in a second.
------------------------------------
Four quick follow up questions so I can digest and fully grok what
you've shown.

1.The query as is, pulls back just one row from the emp table. No
biggie. Im sure I can hunt down why, obviously one of the joins is
acting as a limiting filter. But I was expecting the query to return
one row for every employee in the emp table, since there is no 'where
clause' to filter the data. Just wondering it that's normal among
nested inner joins?
Inner join, functionally seen, is not too much different from WHERE. So,
yes, inner joins normally limit the number of rows.

But here I think I have been over-limiting. Insert parentheses somewhere
:D no, seriously, call me back if you can't catch that one, I'll set up
a small test environment and come up with the correct statement then.
Only giving you pointers here.

Does every employee have a schedule? Does every role occur? with every
manager as well? If not, you must use left joins here (and step
carefully to avoid the sequence mines)
2. Do you personally construct the joins from the inner joins out, or
the outter joins in?
Neither. I write whichever seems "closely related" next to each other,
and build on from these.
To be honest: I always do it from the interface. Which answers:
Does Access care about the order? ( Ill
experiment a bit and see myself, just wondering how others with
experience do it?)
Certainly, that is: I certainly think so.

Now, let's renumber your questions :D
3.Performancewise, if you know, does the jet engine take a speed hit
for each additional 'inner join' a query has? IOW, is 6 'inner joins"
is 'x' amont slower than 3 inner joins?
Hm. No data. But I think that if your indexes are well defined, an inner
join can be significantly *faster* than a WHERE clause. Mine frequently are.
4. Does the order of the nested 'inner joins' make a difference to
performance? ( again, Ill play with it later, just curious for a quick
answer)
Absolutely. But the query optimizer will interfere with how you spell
the SQL statement. I was about to state "if you put a large dataset in
the inner substatements..." as I realized that there is nothing you can
say about the implementation of a given statement.
>
Thanks again for setting me on the correct path.
Hey, welcome!
--
Bas Cost Budde
Holland
Sep 19 '06 #7

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

Similar topics

1
3414
by: Stephen Patten | last post by:
Hi All, While in the process of building my table (40 or so Insert statments) can I then query ("select * from @Table_variable") and use the results up to theat point for another insert into...
4
8425
by: Steve Hall | last post by:
Folks, My secnario involves two tables - ObservationRegister, and Person. ObservationRegister contains most of the "useful" fields, including the UserID of the person that raised the record, and...
1
4461
by: brett | last post by:
Here is my SQL string: "SELECT to_ordnum, to_orddate," _ & "(SELECT SUM((DDPROD.pr_stanmat * DDPROD.pr_prfact) * (DOBOM2.b2_quant * DDORD.or_quant)) FROM DDPROD INNER JOIN DOBOM2 ON...
2
2502
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
1
1434
by: KBuser | last post by:
Preface: I'm building an intranet site to build custom queries against our SQL Server (2000) db; The page is developed in ASP.net (2.0) with C# Codebehind. I dynamically generate and populate...
9
2263
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader>...
14
3081
by: Matt | last post by:
I need to add the following variable into an SQL statement and not sure how to do it. strGCID needs to be inserted into the following statement: SQL = "SELECT tblContacts.* FROM tblContacts...
1
3615
by: Matt | last post by:
I am writing a DELETE statement and I want to filter the records using another SELECT statement. My SELECT statement is a GROUP BY query that grabs all social security numbers from the "Data...
2
2816
ntxsoft
by: ntxsoft | last post by:
Hello everybody, I have a problem while I am trying to execute multiple query in a statement. My query like that DROP TABLE IF EXISTS query; CREATE TEMPORARY TABLE query SELECT...
0
7225
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
7385
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...
1
7046
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
7498
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...
0
5629
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,...
1
5053
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
3195
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
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.