Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
marksquall
Starting Member
17 Posts |
Posted - 2012-08-19 : 04:25:41
|
Hello friends,I hope every one is in good condition upon reading this forum. I am finding a hard time combining these tables.PERSON table:PID LN FN1 BANNER BRUCE2 BLAZE JOHNNY3 SUMMER SCOTTSTUDENT table:PID STUDNUM1 S0000013 S000045 EXAM_RESULTS table:PERSON_ID EXAM_DATE SUBJECT_ID SCORE GRADE1 2012-08-08 1 98 A+1 2012-08-08 2 89 B+2 2011-11-11 1 95 A 2 2011-11-11 2 85 B-SUBJECTS table:SUBJECT_ID DESC1 ALGEBRA2 PHYSICS I want to combine columns in such way that If I search for exam results of Bruce Banner, the expected result would be:STUDNUM LN FN SUBJ SCORE GRADE SUBJ SCORE GRADES000001 BANNER BRUCE ALGEBRA 98 A+ PHYSICS 89 B+ Anyone has an idea what combination of SELECT statement to use, I tried the INNER JOIN but this is the result:STUDNUM LN FN SUBJ SCORE GRADES000001 BANNER BRUCE ALGEBRA 98 A+ S000001 BANNER BRUCE PHYSICS 89 B+ I want to be it in a just one line, or row...Then I will use the query to populate my DataGraidView in my simple VB .NET application.Thank you and more power.Respectfully Yours,Mark Squall"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-08-19 : 06:59:18
|
If you know the subjects in advance, you can pivot your existing query like this:SELECT STUDNUM, LN, FN, MAX(CASE WHEN SUBJ ='ALGEBRA' THEN SUBJ END) AS SUBJ, MAX(CASE WHEN SUBJ ='ALGEBRA' THEN SCORE END) AS SCORE, MAX(CASE WHEN SUBJ ='ALGEBRA' THEN GRADE END) AS GRADE, MAX(CASE WHEN SUBJ ='PHYSICS' THEN SUBJ END) AS SUBJ, MAX(CASE WHEN SUBJ ='PHYSICS' THEN SCORE END) AS SCORE, MAX(CASE WHEN SUBJ ='PHYSICS' THEN GRADE END) AS GRADEFROM YourTableOrQueryGROUP BY STUDNUM, LN, FN; But, if you have unknown number of subjects, you would need to use dynamic pivot - see Madhivanan's blog here:http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-19 : 10:35:04
|
[code]SELECT s.STUDNUM,p.LN,p.FN,MAX(CASE WHEN er.SUBJECT_ID=1 THEN sub.[DESC] END) AS SUBJ1,MAX(CASE WHEN er.SUBJECT_ID=1 THEN er.SCORE END) AS SCORE1,MAX(CASE WHEN er.SUBJECT_ID=1 THEN er.GRADE END) AS GRADE1,MAX(CASE WHEN er.SUBJECT_ID=2 THEN sub.[DESC] END) AS SUBJ2,MAX(CASE WHEN er.SUBJECT_ID=2 THEN er.SCORE END) AS SCORE2,MAX(CASE WHEN er.SUBJECT_ID=2 THEN er.GRADE END) AS GRADE2FROM PERSON pINNER JOIN EXAM_RESULTS erON er.PID = p.PID INNER JOIN STUDENT sON s.PID = er.PERSON_IDINNER JOIN SUBJECTS subON sub.SUBJECT_ID = er.SUBJECT_ID GROUP BY s.STUDNUM,p.LN,p.FN[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
marksquall
Starting Member
17 Posts |
Posted - 2012-08-19 : 23:09:46
|
Dear visakh16 and sunitabeck,Thank you, thank you, thank you! I tried to attempt to use multiple WITH blocks just to "group" results in Physics and Algebra...but little success.Again, thank you so much to the both of you.Another question if you all don't mind? How to proper construct the WHERE clause? I want something like look for student who have taken the exams from 1/1/2010 to 1/1/2012 something like that.Warm regards,Mark Squall"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20 |
 |
|
sonjan
Starting Member
22 Posts |
Posted - 2012-08-19 : 23:52:10
|
Hi MarkYou could try this:SELECT s.STUDNUM,p.LN,p.FN,MAX(CASE WHEN er.SUBJECT_ID=1 THEN sub.[DESC] END) AS SUBJ1,MAX(CASE WHEN er.SUBJECT_ID=1 THEN er.SCORE END) AS SCORE1,MAX(CASE WHEN er.SUBJECT_ID=1 THEN er.GRADE END) AS GRADE1,MAX(CASE WHEN er.SUBJECT_ID=2 THEN sub.[DESC] END) AS SUBJ2,MAX(CASE WHEN er.SUBJECT_ID=2 THEN er.SCORE END) AS SCORE2,MAX(CASE WHEN er.SUBJECT_ID=2 THEN er.GRADE END) AS GRADE2FROM PERSON pINNER JOIN EXAM_RESULTS erON er.PID = p.PID INNER JOIN STUDENT sON s.PID = er.PERSON_IDINNER JOIN SUBJECTS subON sub.SUBJECT_ID = er.SUBJECT_ID WHERE EXAM_RESULTS.EXAM_DATE BETWEEN CONVERT(DATETIME, '2010-01-01 00:00:00') AND CONVERT (DATETIME, '2012-01-01 23:59:00)GROUP BY s.STUDNUM,p.LN,p.FNCheersSonja |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-19 : 23:55:55
|
quote: Originally posted by marksquall Dear visakh16 and sunitabeck,Thank you, thank you, thank you! I tried to attempt to use multiple WITH blocks just to "group" results in Physics and Algebra...but little success.Again, thank you so much to the both of you.Another question if you all don't mind? How to proper construct the WHERE clause? I want something like look for student who have taken the exams from 1/1/2010 to 1/1/2012 something like that.Warm regards,Mark Squall"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20
it would be like...WHERE datefield >= '20100101'AND datefield < '20120101' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
marksquall
Starting Member
17 Posts |
Posted - 2012-08-24 : 03:35:45
|
Dear visakh16, sunitabeck, sonjan,thank you, thank you, thank you!!! You all save my tiring days.One last question, assuming again the:PERSON table:PID LN FN1 BANNER BRUCE2 BLAZE JOHNNY3 SUMMER SCOTT4 PARKER PETER5 STORM SUSANAPPLICANT Table:PID APPLICANT_NUMBER1 2012-012 2012-023 2012-034 2012-045 2012-05 The scenario is this: A school system encodes an applicant (a "soon-to-be" student) in the APPLICANT table, then if they pass the entrance exams, and pay the tuition fee (of course), the applicant will be save in the STUDENT table, making them officially enrolled in the school, but the system will NOT delete their APPLICANT table entry for further reports, stats, etc. purposes. In this example, record ID 2,4,5 have a NULL entry if I join them in student table, 1,3 are the only applicants who we will assume who have successfully passed the exam. Now the report is simple as display the applicant_number, and name of a person who are still considered an applicant, or not currently enrolled in the school.Warm regards,Mark Squall"Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20 |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-08-24 : 07:17:23
|
quote: In this example, record ID 2,4,5 have a NULL entry if I join them in student table,
Do you have a column in the applicant table or elsewhere that indicates that an applicant has been moved from applicant status to student status? Or were you saying that the applicant number will be blanked out (i.e., set to NULL) for such persons?A good option in my opinion, if you are able to do that, is to have a status column somewhere - perhaps in the Applicant Table. For example:CREATE TABLE APPLICANT( PID INT, APPLICANT_NUMBER VARCHAR(32), APPLICATION_STATUS INT); Then, you might consider APPLICATION_STATUS = 1 as application in-process, 2 = ACCEPTED AND ENROLLED, 3 = REJECTED etc. You might even have a reference table that lists the various statuses. If so, you can get all the applicants using:SELECT p.PID, a.LN, p.FNFROM PERSON p INNER JOIN APPLICANT a ON a.PID = a.PIDWHERE APPLICANT_STATUS = 1ORDER BY LN,FN; If a null value of APPLICANT_NUMBER indicates an applicant who was moved from applicant status to enrolled student status, then you can do the following:SELECT p.PID, a.LN, p.FNFROM PERSON p INNER JOIN APPLICANT a ON a.PID = a.PIDWHERE APPLICANT_NUMBER IS NULLORDER BY LN,FN; But this is not as robust as what I suggested - for example, if an applicant simply decided to withdraw her application, you would not be able to distinguish her status from another person who applied and enrolled. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-24 : 10:08:48
|
when you say thisIn this example, record ID 2,4,5 have a NULL entry if I join them in student tabledo you mean they dont have an entry at all or they've entry in student with NULL as values?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|