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 2000 Forums
 SQL Server Development (2000)
 Please help with this query

Author  Topic 

ceema
Yak Posting Veteran

80 Posts

Posted - 2006-09-26 : 01:08:56
In marks table, my fields & Values are like
(I have seperated the field names with a '-'

SchoolID -RegNo- TermCode- ERxamCode- Sub01- Sub02- Sub03
---------------------------------------------------------
AB0 111 T1 E1 11 12 Null
AB0 112 T1 E1 Null 10 14




And in Subject Table

SubCode-SubjectName-MinMark-Maxmark-SchoolID
--------------------------------------------

01 Eng 10 20 AB0
02 Mat 12 20 AB0
03 Fre 10 20 AB0


Student
-------

SchoolID SEnglishName sregNo
--------------------------------
AB0 Joyal 111
AB0 Vincent 112




Now my requirement is

I have to read it like

SchoolID-sRegNo-SubjectName-Maxmark-MinMark-Mark - SEnglishName
------------------------------------------------
Ab0 111 Eng 20 10 11 Joyal
Ab0 111 Mat 20 12 12 Joyal
Ab0 111 Mat 20 12 Null Joyal
Ab0 112 Eng 20 10 Null Vincent
Ab0 112 Mat 20 12 10 Vincent
Ab0 112 Mat 20 12 14 Vincent


ie, Mark of Sub01(In Marks Table) is connected to SubCode of 01 in Subject Table
Mark of Sub02(In Marks Table) is connected to SubCode of 02in Subject Table..extra

Please give me a solution. I know the table design is not correct,
but I can't change it, because my superiors want it like this only


Thank you

Ceema


chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-26 : 02:00:49
your expected output does not matches with the input u have provided.
and it will be great if you can redesign your table structure..

Declare @Table1 Table
(
SchoolID varchar(100),
RegNo int ,
TermCode varchar(10),
ERxamCode varchar(10),
Sub01 float,
Sub02 float,
Sub03 float
)

Insert @Table1
Select 'AB0',111,'T1','E1',11, 12, Null Union all
Select 'AB0',112,'T1','E1',Null,10,14


Declare @Table2 Table
(
SubCode varchar(10),
SubjectName varchar(100),
MinMark float,
Maxmark float,
SchoolID varchar(10)
)

Insert @Table2
Select '01','Eng',10,20,'AB0' union all
Select '02','Mat',12,20,'AB0' union all
Select '03','Fre',10,20,'AB0'

Declare @Table3 TAble
(
SchoolID varchar(10),
SEnglishName varchar(10),
sregNo int
)

Insert @Table3
Select 'AB0','Joyal',111 union all
Select 'AB0','Vincent',112



Select b.SchoolID,c.sRegno,b.SubjectName,b.MaxMark,b.MinMark,a.Mark,c.SEnglishName
From
(
Select SChoolID,RegNo,Sub01 Mark,'01' as SubCode From @Table1
Union All
Select SChoolID,RegNo,Sub02 Mark,'02' as SubCode From @Table1
Union All
Select SChoolID,RegNo,Sub03 Mark,'03' as SubCode From @Table1
) as A
Left outer Join @Table2 as B on
a.SubCode = b.SubCode
Inner Join @TAble3 as c on
c.sregNo= a.RegNo


--Output
SchoolID sRegno SubjectName MaxMark MinMark Mark SEnglishName
---------- ----------- ----------- ----------- ----------- ----------- ------------
AB0 111 Eng 20 10 11 Joyal
AB0 111 Mat 20 12 12 Joyal
AB0 111 Fre 20 10 NULL Joyal
AB0 112 Eng 20 10 NULL Vincent
AB0 112 Mat 20 12 10 Vincent
AB0 112 Fre 20 10 14 Vincent


Chirag
Go to Top of Page

ceema
Yak Posting Veteran

80 Posts

Posted - 2006-09-26 : 02:08:10
Thgank you chiragkhabaria,

It's working fine, I am sorry to give a wrong output.


Regards
Ceema
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-26 : 02:41:23
No hassles with that, but try and normalize your data,it will helpful for you retrive the data based on the simple queries..

Chirag
Go to Top of Page
   

- Advertisement -