| Author |
Topic |
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-05-17 : 03:09:33
|
| I have 4 tables. Student,Mark,SubjectGroup,Subjects. My problem is, I am storing student mark in marks table. The only relation between these two tablesare schoolid and registernumberIn students table we have subject group which is related to Subjectgroup table.Subject & subject Group table are related by SubjectGroupIDMy problem is, I have to create a temporary table having values of regno,schoolid,subjectname,markIt should look likeSchoolid regno Mark MinMark MaxMarkab0 111 12 21 50ab0 111 26 21 50ab0 222 43 21 50ab0 222 44 21 50ab0 333 22 22 50ab0 333 24 22 50ab0 444 34 21 50ab0 444 35 21 50That means Marks for each student should be stored row by row insted of column by columnThank you CeemaStudent-------RegNo SchoolID SubjectGroupID111 ab0 A222 ab0 A333 ab0 B444 ab0 ASubjectGroup------------SubjectGroupID SchoolID SubjectCode MinMark MaxMarkA ab0 1 21 50A ab0 2 21 50B ab0 1 22 50B ab0 2 22 50Subject-------SchoolID SubjectGroupID SubjectCode SubjectNameab0 A 1 Englishab0 A 2 Frenchab0 B 1 Mathsab0 B 2 FrenchMarkSchoolID RegNo Mark01 Mark02 ab0 111 12 26ab0 222 43 44ab0 333 22 24ab0 444 34 35 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-17 : 06:35:16
|
Before you do anything, you should normalise your Mark table - it will make this problem much easier, and will prevent many a headache in the future.That said, this gives the result you require for the question you stated...--datadeclare @Student table (RegNo int, SchoolID varchar(10), SubjectGroupID varchar(10))insert @Student select 111, 'ab0', 'A'union all select 222, 'ab0', 'A'union all select 333, 'ab0', 'B'union all select 444, 'ab0', 'A'declare @SubjectGroup table (SubjectGroupID varchar(10), SchoolID varchar(10), SubjectCode int, MinMark int, MaxMark int)insert @SubjectGroup select 'A', 'ab0', 1, 21, 50union all select 'A', 'ab0', 2, 21, 50union all select 'B', 'ab0', 1, 22, 50union all select 'B', 'ab0', 2, 22, 50declare @Subject table (SchoolID varchar(10), SubjectGroupID varchar(10), SubjectCode int, SubjectName varchar(10))insert @Subject select 'ab0', 'A', 1, 'English'union all select 'ab0', 'A', 2, 'French'union all select 'ab0', 'B', 1, 'Maths'union all select 'ab0', 'B', 2, 'French'declare @Mark table (SchoolID varchar(10), RegNo int, Mark01 int, Mark02 int)insert @Mark select 'ab0', 111, 12, 26union all select 'ab0', 222, 43, 44union all select 'ab0', 333, 22, 24union all select 'ab0', 444, 34, 35--calculationselect s.Schoolid, s.regno, m.Mark01 as Mark, g.MinMark, g.MaxMarkfrom @Student s inner join @Mark m on s.SchoolID = m.SchoolID and s.RegNo = m.RegNo inner join @SubjectGroup g on s.SchoolID = g.SchoolID and s.SubjectGroupID = g.SubjectGroupIDwhere SubjectCode = 1union allselect s.Schoolid, s.regno, m.Mark02, g.MinMark, g.MaxMarkfrom @Student s inner join @Mark m on s.SchoolID = m.SchoolID and s.RegNo = m.RegNo inner join @SubjectGroup g on s.SchoolID = g.SchoolID and s.SubjectGroupID = g.SubjectGroupIDwhere SubjectCode = 2order by s.Schoolid, s.regno, Mark Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-05-17 : 07:34:43
|
| Hello Ryan Randall, Thank you for the post, it helped me a lot. Could you please do one more favour, is there a way to display the subjectname also in the same result?I can't change my table structure, sincce my table is created by our old developers and we are making a new version in front end with the same table.Thank you so muchCeema |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-17 : 08:03:38
|
Just let the Subject table join in...  --datadeclare @Student table (RegNo int, SchoolID varchar(10), SubjectGroupID varchar(10))insert @Student select 111, 'ab0', 'A'union all select 222, 'ab0', 'A'union all select 333, 'ab0', 'B'union all select 444, 'ab0', 'A'declare @SubjectGroup table (SubjectGroupID varchar(10), SchoolID varchar(10), SubjectCode int, MinMark int, MaxMark int)insert @SubjectGroup select 'A', 'ab0', 1, 21, 50union all select 'A', 'ab0', 2, 21, 50union all select 'B', 'ab0', 1, 22, 50union all select 'B', 'ab0', 2, 22, 50declare @Subject table (SchoolID varchar(10), SubjectGroupID varchar(10), SubjectCode int, SubjectName varchar(10))insert @Subject select 'ab0', 'A', 1, 'English'union all select 'ab0', 'A', 2, 'French'union all select 'ab0', 'B', 1, 'Maths'union all select 'ab0', 'B', 2, 'French'declare @Mark table (SchoolID varchar(10), RegNo int, Mark01 int, Mark02 int)insert @Mark select 'ab0', 111, 12, 26union all select 'ab0', 222, 43, 44union all select 'ab0', 333, 22, 24union all select 'ab0', 444, 34, 35--calculationselect s.Schoolid, s.regno, m.Mark01 as Mark, g.MinMark, g.MaxMark, j.SubjectNamefrom @Student s inner join @Mark m on s.SchoolID = m.SchoolID and s.RegNo = m.RegNo inner join @SubjectGroup g on s.SchoolID = g.SchoolID and s.SubjectGroupID = g.SubjectGroupID inner join @Subject j on g.SchoolID = j.SchoolID and g.SubjectGroupID = j.SubjectGroupID and g.SubjectCode = j.SubjectCodewhere g.SubjectCode = 1union allselect s.Schoolid, s.regno, m.Mark02, g.MinMark, g.MaxMark, j.SubjectNamefrom @Student s inner join @Mark m on s.SchoolID = m.SchoolID and s.RegNo = m.RegNo inner join @SubjectGroup g on s.SchoolID = g.SchoolID and s.SubjectGroupID = g.SubjectGroupID inner join @Subject j on g.SchoolID = j.SchoolID and g.SubjectGroupID = j.SubjectGroupID and g.SubjectCode = j.SubjectCodewhere g.SubjectCode = 2order by s.Schoolid, s.regno, Mark Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-17 : 08:14:19
|
Actually, it's probably better to normalise as soon as possible and then do the query based on that.Use this calculation instead.select s.Schoolid, s.regno, m.Mark, g.MinMark, g.MaxMark, j.SubjectNamefrom @Student s inner join ( select SchoolID, RegNo, 1 as SubjectCode, Mark01 as Mark from @Mark union all select SchoolID, RegNo, 2, Mark02 from @Mark) m --NormalisedMark on s.SchoolID = m.SchoolID and s.RegNo = m.RegNo inner join @SubjectGroup g on s.SchoolID = g.SchoolID and s.SubjectGroupID = g.SubjectGroupID and g.SubjectCode = m.SubjectCode inner join @Subject j on g.SchoolID = j.SchoolID and g.SubjectGroupID = j.SubjectGroupID and g.SubjectCode = j.SubjectCodeorder by s.Schoolid, s.regno, Mark Better yet, create a view called 'NormalisedMark' (however you want to spell it!), define it the same as the derived table above, and then use that in your query. It's so much easier working with normalised data Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-05-17 : 09:16:51
|
| RyanRandall, This query is having extra fields in joining, likeg.SubjectCode=m.SubjectCode,g.SubjectGroupID=j.SubjectGroupID etc.So, it's not giginf the exact result as expected. I tried to avoid those and it's giging the nswer two times. please helpThanksCeema |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-17 : 09:37:39
|
| Hi Ceema,Try to find and post some of the data that's giving you problems. It's hard for me (or anyone else) to help unless I can see the problem you're getting with an example.Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|