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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to combine tables in one straight row?

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 FN
1 BANNER BRUCE
2 BLAZE JOHNNY
3 SUMMER SCOTT

STUDENT table:
PID STUDNUM
1 S000001
3 S000045

EXAM_RESULTS table:
PERSON_ID EXAM_DATE SUBJECT_ID SCORE GRADE
1 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 DESC
1 ALGEBRA
2 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 GRADE
S000001 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 GRADE
S000001 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 GRADE
FROM
YourTableOrQuery
GROUP 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
Go to Top of Page

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 GRADE2
FROM PERSON p
INNER JOIN EXAM_RESULTS er
ON er.PID = p.PID
INNER JOIN STUDENT s
ON s.PID = er.PERSON_ID
INNER JOIN SUBJECTS sub
ON sub.SUBJECT_ID = er.SUBJECT_ID
GROUP BY s.STUDNUM,
p.LN,
p.FN
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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
Go to Top of Page

sonjan
Starting Member

22 Posts

Posted - 2012-08-19 : 23:52:10
Hi Mark

You 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 GRADE2
FROM PERSON p
INNER JOIN EXAM_RESULTS er
ON er.PID = p.PID
INNER JOIN STUDENT s
ON s.PID = er.PERSON_ID
INNER JOIN SUBJECTS sub
ON 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.FN

Cheers
Sonja
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 FN
1 BANNER BRUCE
2 BLAZE JOHNNY
3 SUMMER SCOTT
4 PARKER PETER
5 STORM SUSAN

APPLICANT Table:
PID APPLICANT_NUMBER
1 2012-01
2 2012-02
3 2012-03
4 2012-04
5 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
Go to Top of Page

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.FN
FROM
PERSON p
INNER JOIN APPLICANT a ON
a.PID = a.PID
WHERE
APPLICANT_STATUS = 1
ORDER 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.FN
FROM
PERSON p
INNER JOIN APPLICANT a ON
a.PID = a.PID
WHERE
APPLICANT_NUMBER IS NULL
ORDER 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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-24 : 10:08:48
when you say this

In this example, record ID 2,4,5 have a NULL entry if I join them in student table

do you mean they dont have an entry at all or they've entry in student with NULL as values?


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -