Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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:
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  
Old July 29th, 2003, 01:04 PM
Ronage Ronage is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Ronage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Ronage
Question Noobie SQL Problem

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

Reply With Quote
  #2  
Old July 29th, 2003, 05:06 PM
rkern rkern is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 33 rkern User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #3  
Old August 1st, 2003, 05:28 AM
Silian's Avatar
Silian Silian is offline
Gogga
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 198 Silian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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)

Reply With Quote
  #4  
Old August 1st, 2003, 06:57 PM
Ronage Ronage is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Ronage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Ronage
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:
Originally posted by Silian
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)

Reply With Quote
  #5  
Old August 1st, 2003, 07:00 PM
Ronage Ronage is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Ronage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Ronage
File

Here it is in a Zip
Attached Files
File Type: zip test.zip (37.7 KB, 229 views)

Reply With Quote
  #6  
Old August 4th, 2003, 02:32 AM
Silian's Avatar
Silian Silian is offline
Gogga
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 198 Silian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 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.

Reply With Quote
  #7  
Old August 4th, 2003, 01:00 PM
Ronage Ronage is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Ronage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Ronage
Post New File and Screenshots....

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:
Originally posted by Silian
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.
Attached Files
File Type: zip test.mdb.zip (448.0 KB, 202 views)

Reply With Quote
  #8  
Old August 4th, 2003, 02:32 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 4 sec
Reputation Power: 891
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 
rudy
http://rudy.ca/

Reply With Quote
  #9  
Old August 4th, 2003, 02:45 PM
Ronage Ronage is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Ronage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Ronage
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:
Originally posted by r937
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 
rudy
http://rudy.ca/

Reply With Quote
  #10  
Old August 4th, 2003, 02:52 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 4 sec
Reputation Power: 891
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 
rudy

Reply With Quote
  #11  
Old August 4th, 2003, 03:03 PM
Ronage Ronage is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Ronage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Ronage
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Noobie SQL Problem


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 |