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)
 need help on SQL --- I'm confuse

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2012-01-29 : 22:36:00
My table and data as following,

declare @tSubject table
(idx int identity, cd char(2), descrp varchar(100), isCompulsory bit);
insert into @tSubject values('01','BM',1)
insert into @tSubject values('02','BI',1)
insert into @tSubject values('03','MT',1)
insert into @tSubject values('04','SJ',0)
insert into @tSubject values('05','PI',0)
insert into @tSubject values('06','BIO',0)
insert into @tSubject values('07','EA',0)
insert into @tSubject values('08','MJ',0)


declare @tMyResult table
(idx int identity, myID int, tSubject_cd char(2), myGred char(1))
/*
Relationship for @tMyResult to @tSubject is a 1 to many
@tMyResult(tSubject_cd) is a foreign key to @tSubject(cd)
Combination of myID and tSubject_cd is a unique
*/

insert into @tMyResult values('1','02','A')
insert into @tMyResult values('1','03','B')
insert into @tMyResult values('1','07','B')
insert into @tMyResult values('1','08','C')
insert into @tMyResult values('2','01','C')
insert into @tMyResult values('2','04','B')


My expected result as following,

if myID=1,
myID   | tSubject_cd  | myGred
--------------------------------------------
1 02 A
1 03 B
1 07 B
1 08 C
null 01 null



if myID=2,
myID   | tSubject_cd  | myGred
--------------------------------------------
2 01 C
2 04 B
null 02 null
null 03 null



if myID=100, (this scenario is myID not exists in @tMyResult)
myID   | tSubject_cd  | myGred
--------------------------------------------
null 01 null
null 02 null
null 03 null


Hint,
1. All @tSubject must be display if isCompulsory=true, but if it exists in @tMyResult don't have to display

I'm stuck to built the SQL for above scenario

Really need help

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-29 : 22:57:48
why 01 alone came for id=1? what about 02,03,07?

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

Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2012-01-29 : 23:02:09
quote:
Originally posted by visakh16

why 01 alone came for id=1? what about 02,03,07?

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





For myID=1, 01 is not exists in @tMyResult but in @tSubject the isCompulsory=true

that's why it's came in the result

help me sir
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2012-01-30 : 00:04:39
plz help me
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-30 : 11:22:21
quote:
Originally posted by Delinda

plz help me



select myID, tSubject_cd, myGred
from @tMyResult
where myID = @yourpassedvalue

union all

select null,t.cd,null
from @tSubject t
where isCompulsory = 1
and not exists (select 1
from @tMyResult
where myID = @yourpassedvalue
and t.cd = tSubject_cd)


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

Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2012-01-31 : 08:10:32
tq sir
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-31 : 12:55:28
welcome

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

Go to Top of Page
   

- Advertisement -