-3

I have two tables given below

service table

serviceid      serviceName     title   isrestricted
---           --------------  ------  ----------
1                  abc          t1     0
2                  asd          t2     1
3                  def          t3     0


serviceRestricted table

sampletype   serviceid
--------     ---------
a            2


output

 serviceid      serviceName     title   isrestricted
    ---           --------------  ------  ----------
    1                  abc          t1     0   
    3                  def          t3     0

See the output .For Service id 2 isrestricted=1 and there is entry in the servicerestricted table. So serviceid 2 is not visible in the output

sandeep.mishra
  • 825
  • 4
  • 19
  • 40

2 Answers2

0

Left Join should solve your problem as:

select S.serviceid,S.serviceName,S.title,S.isrestricted
from @service S
Left Join @serviceRestricted SR on SR.serviceid = S.serviceid
where S.isrestricted = 1

Sample code here..

Deepshikha
  • 9,896
  • 2
  • 21
  • 21
0

I am able to achive it by below query

select S.serviceid,S.serviceName,S.title,S.isrestricted
from service S
WHERE
((S.IsRestricted =1 and S.serviceid NOT IN (SELECT SERVICEID FROM servicerestricted WHERE SampleType=@SampleType) ) 
OR (S.IsRestricted is null OR S.isrestricted =0))
sandeep.mishra
  • 825
  • 4
  • 19
  • 40