|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Situation:
Access Database Goal: Construct a SQL statement to join data from two tables. Table1 is a schedule Fields are like this: Date | Time | Team1 | Team2 |Location etc. Table2 has the Team Names: Team_ID | TeamName I am trying to write a SQL statement that will give me the complete schedule for all teams which includes the Team Name and team number for each of team1 and team2. this is not right, but it's a start :::> SELECT S.GameDate, S.GameTime, S.LocationNumber, S.VisitingTeamNumber, S.HomeTeamNumber, T.TeamNumber, T.TeamName FROM SchGames S, Team T WHERE S.VisitingTeamNumber = T.TeamNumber; TIA |
|
#2
|
|||
|
|||
|
for starters, from what you have posted it looks like you will get results where the team numbers are equal. Not a likely situation, and you are limited to only that set.
I would define a common field (key) for each table, use 'games' or your 'gamedate'. Each team is identified with a game and select on that . Just don't see any reason for selecting on team number. cheers RK |
|
#3
|
||||
|
||||
|
Not sure if this is what you are looking 4 (I'm assuming that team1 and team2 are the id's 4 the teams):
Code:
select a.date, a.time, a.location, b.teamName, c.teamName from table1 a left join table2 b on (b.team_id = a.team1) left join table2 c on (c.team_id = a.team2) |
|
#4
|
|||
|
|||
|
Close I think but getting Error
Thanks a lot for responding, I think it's definitely the right direction below although I am getting a syntax error.
I have attached a sample db that I tried the below on along with serveral variations to try and remedy the error with no luck. Code:
SELECT S.GameDate, S.GameTime, H.TeamName, V.TeamName FROM SchGames S LEFT JOIN Team H ON (H.TeamNumber = S.HomeTeamNumber) LEFT JOIN Team V ON (V.TeamNumber = S.VisitingTeamNumber); Quote:
|
|
#5
|
|||
|
|||
|
File
Here it is in a Zip
|
|
#6
|
||||
|
||||
|
I can't open your file (it tells me it does't recognise the format.) (Don't know if there is something wrong with my ms access) Can you give me the fields and types for the tables? (Or test the file, and if it isn't working, repost it)
Thanks, Silian. |
|
#7
|
|||
|
|||
|
Rename the attached file to test.mdb (from test.zip). Data types are number and text; Here are two screenshots of the tables with sample data...
Thanks a lot for your help! Quote:
|
|
#8
|
||||
|
||||
|
you didn't say what the problem was
try this: Code:
select S.ScheduleNumber
, S.GameNumber
, S.GameDate
, S.GameTime
, H.TeamName as HomeTeam
, V.TeamName as VisitingTeam
from SchGames S
left outer
join Team H
on S.HomeTeamNumber = H.TeamNumber
left outer
join Team V
on S.VisitingTeamNumber = V.TeamNumber
http://rudy.ca/ |
|
#9
|
|||
|
|||
|
Right, sorry about that. I keep getting a syntax error with the example above and below that reads:
Syntax error (missing operator) in query expression ‘S.HomeTeamNumber = H.TeamNumber Left outer Join Team V On S.VisitingTeamnumber = V.TeamNumber’ Quote:
|
|
#10
|
||||
|
||||
|
aw crap, my bad
that there was standard syntax access requires parenthesized joins: Code:
select S.ScheduleNumber
, S.GameNumber
, S.GameDate
, S.GameTime
, H.TeamName as HomeTeam
, V.TeamName as VisitingTeam
from (
SchGames S
left outer
join Team H
on S.HomeTeamNumber = H.TeamNumber
)
left outer
join Team V
on S.VisitingTeamNumber = V.TeamNumber
|
|
#11
|
|||
|
|||
|
Absolutely fabulous Rudy, thanks for the help, that is what I was looking for. I tried a couple () combos but could not find the right placement.
Regards, Ron |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Noobie SQL Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|