SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL 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 ASP Free Forums Sponsor:
  #1  
Old February 27th, 2004, 09:53 AM
DDDruid DDDruid is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 DDDruid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Joining 3 tables with 1 to many records for a distinct recordset

I want to return a set of unique records from Three tables where one table holds distinct Student information, and another holds 1 to many records for each Student according to the number of classes being taken, and the third holds the number of classes being given per term:

Table 1: Studid (PK), Department, Location, Major

Table 2: Studid (PK), Classid (PK), Class-Name, Room-Number

Table 3: Classid (PK). Class-name



So, there are 9 total classes available this term, listed as 1 thru 9, from table 3. Student “Joe” is taking classes 1, 3, 4 and 9, Student “Mat” is taking classes 2, 3, 5, and 8. So table 2 has four records for Joe, Identified by his Stuid + Classid, and table 2 has four records for Mat, identified by the same key fields. I want to return a single record for each student, including a “flag” for all classes taken and not taken by identifying the classes taken (Table 2 records) and then filling in the recordset fields with either a Y (because table 2 had a entry for that class) or a N if the table didn’t have a record for that class/studid:



Name StuID Cl1 Cl2 Cl3 Cl4 Cl5 Cl6 Cl7 Cl8 Cl9

Joe 11121 -Y- -N- -Y- -Y- -N- -N- -N- -N- -Y-

Mat 12213 -N- -Y- -Y- -N- -Y- -N- -N- -Y- -N-



There are hundreds of students. I have done this by looping through the Table 1 recordset return, and for each student, running another set of queries to return table 2 values, then parsing into a list with Y or N as compared to table 3, but it takes WAY TO LONG TO RUN, and times out for any great number of records.



IS there a way to run just one Joined or Nested type of query to optimize this without having to loop through queries within queries? It’s an Oracle 9i DB displayed in Cold Fusion.



Thanks




Reply With Quote
  #2  
Old February 27th, 2004, 10:29 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
Okay, I have created a database to match your structure. Post some sample data I can add to the database and post how you expect (want) the data to look like when it is returned by the query.

Reply With Quote
  #3  
Old February 27th, 2004, 11:47 AM
DDDruid DDDruid is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 DDDruid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Structure and Resultset

TABLE 1
Studid____Dept____Location___Major
IS11100___IS________Jax_____IS/IT
IS12210___IS________Jax_____IS/IT
CS39006__CS________Jax_____HR
CS29000__CS________Jax_____HR

TABLE 2
Stuid___Classid____Class-Name____Room-Number
IS11100___2______Sys Analysis_____232c
IS11100___4______Database Tech___239a
IS11100___5______Database Lab____239a
IS11100___8______Program Basics___232e
IS12210___1______IT Management___211a
IS12210___2______Sys Analysis______232c
IS12210___8______Program Basics____232e
CS39006___2_____Sys Analysis______232c
CS39006___3_____Computer Design___100a
CS39006___4_____Database Tech____239a
CS39006___5_____Database Lab_____239a
CS29000___3_____Computer Design___100a
CS29000___6_____SQL______________121a
CS29000___7_____SQL Lab__________121a
CS29000___9_____Methodologies_____105a

Table 3
Classid Class-Name
1______IT Management
2______Sys Analysis
3______Computer Design
4______Database Tech
5______Database Lab
6______SQL
7______SQL Lab
8______Program Basics
9______Methodologies

RESULT SET:
StuID__Dept__Loc__IT___Sys___Comp___DB____DB__SQL_ __SQL___Prog__Methods
_________________Man_Anal___Design__Tech__Lab_____ ___Lab___Basics

IS11100_IS____Jax____N_____Y______N______Y_____Y__ ___N_____N_______Y______N
IS12210_IS____Jax____Y_____Y______N______N_____N__ ___N_____N_______Y______N
CS39006_CS___Jax____N_____Y______Y______Y_____Y___ __N_____N_______N______N
CS29000_CS___Atl____N_____N______Y______N_____N___ __Y_____Y_______N______Y

Sorry about the underscores, formatting problems lining up the columns

Reply With Quote
  #4  
Old February 27th, 2004, 12:26 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
First you should redesign you database structure, it isn't normalized.
Should be more like this
Code:
Table1
StudID
Department
Location
Major

Table2
StudID
ClassID

Table3
ClassID
ClassName
RoomNumber

I will work on the query with this structure.

Reply With Quote
  #5  
Old February 27th, 2004, 12:31 PM
DDDruid DDDruid is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 DDDruid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Tables

Very true, however, the tables are actually much larger then as given, and already in place within a enterprise wide structure, so there is little but maybe some things I can do. I just wanted to pull, for simplicity sake, those fields to give as an example of my problem. But I truly appreciate any assistance you can provide.

Reply With Quote
  #6  
Old February 27th, 2004, 12:42 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
I'm sorry, there really isn't much I can do to return the results you want, since the database structure isn't in the proper arrangement. About the only thing you could do is return the Students and the Classes they are enrolled in, but as far as doing something like a Case statement to determine 'Y' or 'N', I don't see how that is possible with the current structure.

Reply With Quote
  #7  
Old February 27th, 2004, 01:05 PM
DDDruid DDDruid is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 DDDruid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Table Structure

Thanks anyway for your assistance.

Reply With Quote
  #8  
Old February 27th, 2004, 01:07 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You might try sending a private message to sbaxter. He's a solid database person.

Reply With Quote
  #9  
Old March 1st, 2004, 11:28 AM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
I agree with Memnoch, you need to see what you can do with the table structure, but....



SQL Statement (works in MS Access)

Code:
TRANSFORM First(IIf([Table2]![StuID] Is Null,"N","Y")) AS Marker
SELECT Q.StuID, Q.Department, Q.Location
FROM 
[SELECT Table1.StuID, Table3.ClassID, Table1.Department, Table1.Location, Table3.ClassName
FROM Table1, Table3 Order By ClassID]. AS Q 
LEFT JOIN Table2 ON (Q.StuID = Table2.StuID) AND (Q.ClassID = Table2.ClassID)
GROUP BY Q.StuID, Q.Department, Q.Location
PIVOT Q.ClassName;


PHP Code:
 Results
StuID    Department    Location    Computer Design    Database Lab    Database Tech    IT Management    Methodologies    Program Basics    SQL    SQL  Lab    Sys Analysis
CS29000    CS    JAX    Y    N    N    N    Y    N    Y    Y    N
CS39006    CS    JAX    Y    Y    Y    N    N    N    N    N    Y
IS11100    IS    JAX    N    Y    Y    N    N    Y    N    N    Y
IS12210    IS    JAX    N    N    N    Y    N    Y    N    N    Y 



Will have to be slightly different in SQl Server, 'First' is not valid in SQL Server, and I am not sure how Pivot statements react in SQL Server

S-

Reply With Quote
  #10  
Old March 29th, 2004, 04:17 AM
RichB RichB is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 RichB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Joining 3 tables

Memnoch,


I have just found your posts on joining 3 tables. I have a similar problem and I'm hoping you can help...

I have 3 tables

Table 1
EmployeeID
First Name
Last Name
Email

Table 2
ID
TaskID
EmployeeID


Table 3
TaskID
TaskName
RaisedBy (this is an employeeID)
Status (closed, new, assigned)


I would like to list all the tasks assigned to an employee, who the project manager is and who else may be assigned to the task (if any)
something like this...

TaskIdTaskName ProjectManager AssignedTo Status
123 Install Win2K DaveManager JoeDoggsBody Assigned
bob
sam
I am using ASP and Microsoft Access and I am working with the following SQL query but I just cant get it right.


sqlString = "SELECT TASK_TABLE.TASK_ID, TASK_NAME, SLAVE.FIRST_NAME, SLAVE.LAST_NAME, BOSS.FIRST_NAME, BOSS.LAST_NAME, BOSS.EMAIL, ETC, STATUS " &_
"FROM " &_

"TASK_TABLE INNER JOIN EMP_TABLE BOSS " &_
"ON TASK_TABLE.REQUEST_BY = BOSS.EMP_ID " &_

"INNER JOIN " &_

"EMP_TASK_LINK INNER JOIN EMP_TABLE SLAVE " &_
"ON EMP_TASK_LINK.EMP_ID = SLAVE.EMP_ID " &_

"ON " &_

"TASK_TABLE.TASK_ID = EMP_TASK_LINK.TASK_ID"


Thank you for any help

Rich

Reply With Quote
  #11  
Old March 29th, 2004, 04:10 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
SELECT ETL.TaskID, TT.TaskName, ET.FirstName AS SlaveFname, ET.LastName AS SlaveLName, ET1.FirstName AS BossFname, ET1.LastName AS BossLName, ET1.Email AS BossEmail, TT.Status
FROM (EMP_TABLE AS ET1 INNER JOIN TASK_TABLE AS TT ON ET1.EmployeeID = TT.RaisedBy) INNER JOIN (EMP_TABLE AS ET INNER JOIN EMP_TASK_LINK AS ETL ON ET.EmployeeID = ETL.EmployeeID) ON TT.TaskID = ETL.EmployeeID;



s-

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Joining 3 tables with 1 to many records for a distinct recordset


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 |