さて、ここで問題です。
SELECT CTE.TECHNICIANID, SUM(TLEVEL) AS TLEVEL, TECH.PASSEDDRUGTEST, TECH.PASSEDBACKGROUNGCHECK, TECH.FIRSTNAME, TECH.LASTNAME,
CM.CITYID, CM.ADDRESS, CM.EMAIL, CM.PHONENUMBER, CM.ZIPCODE, CNTRY.COUNTRYNAME, SM.STATENAME,
CM.LATITUDE, CM.LONGITUDE FROM CTE
INNER JOIN TECHNICIAN TECH ON CTE.TECHNICIANID = TECH.TECHNICIANID
INNER JOIN SERVICEORDER SO ON SO.SERVICEORDERID = @SERVICEORDERID
INNER JOIN CONTRACT CONT ON SO.CONTRACTID = CONT.CONTRACTID
INNER JOIN CONTACTMASTER CM ON CM.CONTACTID = TECH.CONTACTID
INNER JOIN COUNTRYMASTER CNTRY ON CM.COUNTRYID = CNTRY.COUNTRYID
INNER JOIN STATEMASTER SM ON SM.STATEID = CM.STATEID
--WHERE CONT.DRUGSCREENINGREQUIRED = CASE CONT.DRUGSCREENINGREQUIRED WHEN 'TRUE' THEN TECH.PASSEDDRUGTEST END AND
--CONT.BACKGROUNDCHECKREQUIRED = CASE CONT.BACKGROUNDCHECKREQUIRED WHEN 'TRUE' THEN TECH.PASSEDBACKGROUNGCHECK END
GROUP BY CTE.TECHNICIANID, TECH.PASSEDDRUGTEST, TECH.PASSEDBACKGROUNGCHECK,CM.LATITUDE, TECH.FIRSTNAME, TECH.LASTNAME,
CM.CITYID, CM.ADDRESS, CM.EMAIL, CM.PHONENUMBER, CM.ZIPCODE, CNTRY.COUNTRYNAME, SM.STATENAME, CM.LONGITUDE ORDER BY TLEVEL DESC
where句を含むクエリのコメント部分を見てください。必要なときにdrugscreening
必要なのは、条件を適用したくない場合と同じようにtrue
すべてのレコードが必要なことです。passeddrugtest
true
編集: CONT.DRUGSCREENINGREQUIRED
andが true の場合、 andCONT.BACKGROUNDCHECKREQUIRED
でレコードをフィルタリングしたいだけです。それらが真でない場合、 andが真か偽かに関係なく、すべてのレコードが必要です。TECH.PASSEDDRUGTEST
TECH.PASSEDBACKGROUNGCHECK
TECH.PASSEDDRUGTEST
TECH.PASSEDBACKGROUNGCHECK