MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 22nd, 2004, 04:08 PM
Ann Green Ann Green is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 Ann Green User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question MS Access SQL Syntax Error

There is a JOIN syntax error in this SQL, but my slow brain cannot figure out where. I tried to join two queries, which had been successful. Problem occurred when I added second left join. Can anyone help?

PARAMETERS pstrFinYear Text ( 255 ), pintAdjMonth Long;

SELECT A.BudgetLineID, A.BudgetLine, B.NumIsDevelopBusinessInternationally, B.NumIsDeeperParticipation, B.NumIsNewExporter, B.NumProjects, C.NumCompanies, A.KMISReportOrder
FROM ( tblkpBudgetLine AS A

LEFT JOIN

[SELECT BudgetLineID, FinancialYear, SUM(IsDevelopBusinessInternationally) AS NumIsDevelopBusinessInternationally, SUM(IsDeeperParticipation) AS NumIsDeeperParticipation, (-1*SUM(AdjustedNewExpMonth=pintAdjMonth)) AS NumIsNewExporter, COUNT(ProjectID) AS NumProjects
FROM qryBoardReport_Actuals
WHERE FinancialYear=pstrFinYear
AND AdjustedProjectStartMonth=pintAdjMonth
GROUP BY BudgetLineID, FinancialYear]. AS B
ON (A.BudgetLineID=B.BudgetLineID) AND (A.FinancialYear=B.BudgetLine)

LEFT JOIN

[SELECT Z.BudgetLineID, Z.FinancialYear, Z.AdjustedProjectStartMonth, COUNT(Z.Company) AS NumCompanies
FROM [SELECT DISTINCT qryBoardReport_Actuals.BudgetLineID AS BudgetLineID, qryBoardReport_Actuals.FinancialYear AS
FinancialYear, qryBoardReport_Actuals.AdjustedProjectStartMonth, qryBoardReport_Actuals.CompanyID as Company
FROM qryBoardReport_Actuals
WHERE FinancialYear=pstrFinYear
AND AdjustedProjectStartMonth=pintAdjMonth
GROUP BY qryBoardReport_Actuals.FinancialYear, qryBoardReport_Actuals.BudgetLineID, qryBoardReport_Actuals.AdjustedProjectStartMonth, qryBoardReport_Actuals.CompanyID]. AS Z
GROUP BY FinancialYear, BudgetLineID, AdjustedProjectStartMonth] as C

ON (A.BudgetLineID=C.BudgetLineID) and (A.FinancialYear=C.FinancialYear))


WHERE A.FinancialYear=pstrFinYear
ORDER BY A.KMISReportOrder;

Reply With Quote
  #2  
Old April 22nd, 2004, 05:33 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
In my limited experience w/MS Access...I don't think you can join table A to table B, then join table A to Table C. I think you have to join table A to (Table B join Table C), see what I'm saying...you have this going on.
Code:
Select ...
from    (TableA
           Left Join TableB on TableA = TableB
           Left Join TableC on TableA = TableC)

Give this a try...I think you need to join the "farthest" tables first inside parens, tableB and tableC, and then add your syntax to complete the join on TableA to TableB. The above where you had table A joined to table B joined to table C all in one set of parens, I don't think Access allows that. Access is really picky where parens go, and even more so w/parenthetical matching w/queries that use an exaggerated # of joins. Try this psuedo query though...My apologies if this is incorrect.
Code:
Select ....
from   Table A
left   join TableB
              (Left Join Table C on TableB = TableC) 
        on TableA = TableB

here's an example w/a query using a 5 table join (same syntax regaurdless of left, right, full joins)...note that w/every new table joined, a parenthetical match is required...
Code:
select  LessonAttendID,
        LS.LessonDescription,
        C.CustomerID, C.LastName,
        R.ReservationID,
        WS.workDate
from    tLessonAttendance as L
inner   join(tCustomers C
             inner join(tLessonReservations R
                        inner join(tLessons LS
                                   inner join tWeeklyHours on LS.lessonID = WS.workID)
                         on R.LessonID = LS.LessonID)
              on C.customerID = R.customerID)
on L.customerid = C.customerID;

here's a link that gives a pretty good example on how to nest joins in Access. The example is near the bottom of the pg.

http://www.yk.psu.edu/~lxn/IST_210/...nner_joins.html

Last edited by Username=NULL : April 23rd, 2004 at 03:16 AM.

Reply With Quote
  #3  
Old April 23rd, 2004, 07:30 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,921 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 58 m 55 sec
Reputation Power: 1022
be careful with what you read on some web sites

for example, the above-mentioned site contains this gem:
Quote:
You can also link several ON clauses in a JOIN statement, using the following syntax:

SELECT fields
FROM table1 INNER JOIN table2
ON table1.field1 compopr table2.field1 AND
ON table1.field2 compopr table2.field2) OR
ON table1.field3 compopr table2.field3)];

patently wrong

a silly, trivial, human error, but wrong nonetheless
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old April 24th, 2004, 09:11 AM
Ann Green Ann Green is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 Ann Green User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Will sort out parenthesis and joins.

Thanks for your help.

Reply With Quote
  #5  
Old June 17th, 2004, 02:45 PM
jonevance jonevance is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 jonevance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have a similar problem. I'm not sure what I want to do is even possible using Access, though it does fit within SQL standards.

My situation is complex, but this is a simple example:

Table T1 links to table T2 via fields a1 = a2
T1 links to table T3 via fields b1 = b3
T1 links to table T4 via fields c1 = c4

T2, T3 and T4 may not have records associated with any given record in T1, and T2, T3 and T4 are not related to each other directly at all.

If I want to pull a record from T1 with all associated records (if any) from T2, T3, and T4, I must use LEFT JOINs. Standard SQL would have me do it thus:

SELECT * FROM T1 LEFT JOIN T2 ON T1.a1 = T2.a2 LEFT JOIN T3 ON T1.b1 = T3.b3 LEFT JOIN T4 ON T1.c1 = T4.c4

But that, of course, does not work on Access. In fact, there is no method that I can find to make it work with their screwy parenthesized nested joins. Can anyone clear that up for me?

Reply With Quote
  #6  
Old June 20th, 2004, 04:19 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,921 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 58 m 55 sec
Reputation Power: 1022
Quote:
Originally Posted by jonevance
If I want to pull a record from T1 with all associated records (if any) from T2, T3, and T4, I must use LEFT JOINs.
actually, if T2, T3, and T4 are unrelated, what you want is UNIONs

t1 join t2
union
t1 join t3
union
t1 join t4

Reply With Quote
  #7  
Old July 7th, 2004, 10:11 AM
brchris brchris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 brchris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question *Revisted*

I'm having a similar issue I was hoping someone here could shed some light on.

SELECT *
FROM ((((((SK_SKILLSINAGES AS sia INNER JOIN AG_AGESINERAS AS ae ON sia.AgeCode = ae.AgeCode) INNER JOIN AG_ERA AS e ON ae.EraCode = e.EraCode) INNER JOIN SK_CATEGORYGROUPS AS cg ON sia.GroupCode = cg.GroupCode) INNER JOIN SK_CATEGORY AS c ON (sia.CategoryCode = c.CategoryCode) AND (sia.GroupCode = c.GroupCode)) INNER JOIN SK_SKILLS AS s ON sia.SkillCode = s.SkillCode) LEFT JOIN SK_SUBSKILLS AS ss ON (sia.SkillCode = ss.SkillCode AND sia.SubSkillCode = ss.SubSkillCode) OR (sia.SkillCode = ss.SkillCode AND ss.Table = -1)) LEFT JOIN Query2 AS uq ON uq.Table = ss.SubSkillCode AND uq.Code = sia.SubSkillCode
ORDER BY e.Description, ae.Description, cg.Description, c.Description, s.Description, ss.Description;


Yes, a complicated SQL query, but necessary nonetheless. The issue I'm having is with the code in DarkRed. Apparently if I removed the line, the query works in Access. Keeping the line I get a join expression not supported. It seems removing the AND and the the joining statement allows Access to parse it. I could use either A or B as a statement and have it work but not together. Is there something I'm missing? I checked out the site and I could try it using that syntax but it seems the syntax I am using works it's just that when joining A and B together is when it breaks.

Thanks!

Reply With Quote
  #8  
Old July 7th, 2004, 12:43 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
Access parenthesis mania. Try something like

Code:
ON ((uq.Table = ss.SubSkillCode) AND (uq.Code = sia.SubSkillCode))

Reply With Quote
  #9  
Old July 7th, 2004, 01:20 PM
brchris brchris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 brchris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy

nope... darn.... here's an interesting twist.

Quote:
ON uq.Table = ss.SubSkillCode AND uq.Table = ss.SubSkillCode


works!

Which tells me perhaps there is a disconnect in my joins. What I don't understand is I call the table given the subname "sia" in the join just before this one without a problem. Query2 is a UNION query so I replaced it with just a simple table, and it still doesn't like it, so it is definitely a disconnent somewhere. I have a have a feeling I'm inches from the solution but just don't see it.

edit: If I change LEFT JOIN (both of them only) to INNER JOIN it works. Unfortunately LEFT JOIN is important.


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > MS Access SQL Syntax Error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT