SQLTeam.com | Weblogs | Forums

T-sql 2012 make logic more obvious


#1

In t-sql 2012, each of the sqls listed separately below work fine. However I
would like to be able to have t-sql where the c1.value either='N' or c1.value either<>'N' is
more obvious. I would like for that to be a distinguishing part of the sql using an if else,
case, and or whatever you think would work.
referenced sql:

IF
(Select c2.personID, c2.enrollmentID
,attributeID=3371
,c2.value,c2.date,c2.customGUID,c2.districtID
from O.dbo.Cust C1
JOIN O.dbo.Cust c2
on c2.personID=c1.personID
and c1.date=c2.date
and C2.attributeID= 1452
where C1.attributeID = 997 and c1.value ='N')

ELSE IF

  (SELECT 
  c2.personID,c2.enrollmentID
  ,attributeID=3370
  ,c2.value,c2.date,c2.customGUID,c2.districtID
   from O.dbo.Cust C1

JOIN O.dbo.Cust c2
on c2.personID=c1.personID
and c1.date=c2.date
and C2.attributeID= 1452
where C1.attributeID = 997 and c1.value <> 'N')

Thus would you modify the t-sql, I listed above to accomplish my goal?


#2
Select c2.personID, c2.enrollmentID
,attributeID=case when c1.value = 'N' then 3371 else 3370 end
,c2.value,c2.date,c2.customGUID,c2.districtID 
from O.dbo.Cust C1
JOIN O.dbo.Cust c2 
on c2.personID=c1.personID
and c1.date=c2.date
and C2.attributeID= 1452
where C1.attributeID = 997