| Author |
Topic |
|
Chipper
Starting Member
4 Posts |
Posted - 2011-08-18 : 13:15:03
|
| I have a table that shows testing information for a certain group of people. There are two types of people who can take the test: one is a group of people who have registered and have been verified and the other is a group that will never register but takes the test on invitation. These groups must remain separate for business reasons and the data gathered on each are in separate tables. (I inhertited that part of the database and am not authorized to change it). The test table contains the following columns:test_id - identifiertest_date - date of testtested_id - id of person taking the testid_source - which group table the tested_id refers to (Value 10 = registered, 99 = invited)completed - boolean to show whether the test was completed or notscore - score of the testIn the "registered" table I have, among other data elements:person_idlast_namefirst_nameorganizationIn the "invited" table I have the same data elements (and others as well)Yes, I know - not so good table design but what can you do when you inherit this stuff?For example I have in the table the following data:10 08/11/2011 438 10 1 100 (this person is registered and has completed a test with a score of 100)20 08/11/2011 1712 10 0 NULL (this person is registered and has not completed the test - no score yet)30 08/12/2011 813 99 1 86 (this person was invited and has completed a test with a score of 86)40 08/21/2011 438 99 1 72 (this person was invited and has completed a test with a score of 72)Note that in the data above I have two persons wit identical tested_ids but differentiated by the id_source.Now, I want a report of all people who have tested. I need:Last NameFirst NameOrganizationDate TestedScoreThe first three elements need to come from the referenced tested_id table and the last two from the test table. I am just beginning to learn TSQL syntax and could use some help constructing a select statement to do the above.Thank you in advance for your help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-18 : 13:32:58
|
seems to be a simple join like belowSELECT p.last_name,p.first_name,p.organization,t.dateTested,t.ScoreFROM test tINNER JOIN (SELECTperson_id,last_name,first_name,organizationFROM registeredUNION ALLSELECT person_id,last_name,first_name,organizationFROM invited)pON p.person_id = t.tested_id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Chipper
Starting Member
4 Posts |
Posted - 2011-08-18 : 17:45:02
|
| Thanks visakh 16. This is close but the Select query as you describe returns two entries when the same person_id is in both the registered and invited tables. In other words, from the example give, test_id 10 would have a line for the first_name, last_name and organization for from the person whose id was 438 in the registered table and another line for the first_name, last_name and organization for the person whose person_id happened to be 438 in the invited table - and these are two different people. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-19 : 01:58:56
|
| thats fine but will that make a difference? so far as name and other details are different its fine rite? Or do you want to restrict only one person per test?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Chipper
Starting Member
4 Posts |
Posted - 2011-08-19 : 10:06:32
|
| Yes, it makes a difference. For the sample data here:test table10 08/11/2011 438 10 1 100 20 08/11/2011 1712 10 0 NULL 30 08/12/2011 813 99 1 86 40 08/21/2011 438 99 1 72 registered table438 Smith George MyCorp511 Jones Bob Microswift Co1712 Thumb Tom Terrific Toysinvited table119 White Barbara OfficePlus438 Black Karen USB TV813 Green Hank Colors, IncWhat I want to see as results:Smith George MyCorp 08/11/2011 100Thumb Tom Terrific Toys 08/11/2011 NULLGreen Hank Colors, Inc 08/12/2011 86Black Karen USB TV 08/21/2011 72What your query returnsSmith George MyCorp 08/11/2011 100Black Karen USB TV 08/11/2011 100Thumb Tom Terrific Toys 08/11/2011 NULLGreen Hank Colors, Inc 08/12/2011 86Black Karen USB TV 08/21/2011 72Smith George MyCorp 08/21/2011 72 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-19 : 10:55:53
|
| [code]SELECT p.last_name,p.first_name,p.organization,t.dateTested,t.ScoreFROM test tINNER JOIN (SELECTperson_id,last_name,first_name,organization,10 AS SourceFROM registeredUNION ALLSELECT person_id,last_name,first_name,organization,99FROM invited)pON p.person_id = t.tested_idAND p.source = t.Id_source[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Chipper
Starting Member
4 Posts |
Posted - 2011-08-19 : 13:59:53
|
| Thank you! That did the trick for me. But I also want to learn from this (what else is this forum for, after all?). I follow everything in your code except the "99". Why is that there? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-20 : 01:41:41
|
| i have put it as a filler for id_source field in your source table to enable joining to correct record from either invited/registered table since ids can repeat in both------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|