Hi guys,For me this is a tricky query and I've been stuck with this for 3 days. This is my query:Select sa.SubjectAlertKey,obj.ObjectRetrievalGroupFlags, COUNT(DISTINCT innersa.SubjectAlertViolationKey) as NoViolationsFrom ObjectRetrievalGroup objInner Join SubjectAlert sa on sa.SubjectAlertKey= ObjectRetrievalGroupFlagsLeft Join (SELECT --- TRICKY QUERYv.SubjectAlertKey, v.SubjectAlertViolationKey, v.ViolationSubjectKey, v.RuleKey, v.ViolationSubjectTypeKey FROM SubjectAlertViolation v JOIN SubjectAlert a WITH (NOLOCK) ON a.SubjectAlertKey = v.SubjectAlertKey WHERE ((a.RunDate >= '20010909' AND a.RunDate < '20090917') OR a.RunDate IS NULL) AND (a.AlertTypeId IN ('RTLR','ELEC','PAPR','COLA','TRAN','TRANAUTH') OR a.AlertTypeId IS NULL) AND a.MemberKey = 2) as innersaon innersa.RuleKey= ObjectRetrievalGroupFlags AND innersa.ViolationSubjectTypeKey = sa.SubjectAlertKey LEFT JOIN SubjectAlert a WITH (NOLOCK) ON a.SubjectAlertKey = innersa.SubjectAlertKey where (a.MemberKey =2 OR a.MemberKey IS null)Group By sa.SubjectAlertKey,obj.ObjectRetrievalGroupFlags Retuned Data:SubjectAlertKey ObjectRetrievalGroupFlags NoViolations--------------- ------------------------- ------------5 5 07 7 09 9 025 25 0
Problem: The "NoViolations" column should have values not Zeroes.Trying to run the Left Join (with remarks "TRICKY QUERY"), set ofdata is returned:SELECT -- TRICKY QUERYv.SubjectAlertKey, v.SubjectAlertViolationKey, v.ViolationSubjectKey, v.RuleKey, v.ViolationSubjectTypeKey FROM SubjectAlertViolation v JOIN SubjectAlert a WITH (NOLOCK) ON a.SubjectAlertKey = v.SubjectAlertKey WHERE ((a.RunDate >= '20010909' AND a.RunDate < '20090917') OR a.RunDate IS NULL) AND (a.AlertTypeId IN ('RTLR','ELEC','PAPR','COLA','TRAN','TRANAUTH') OR a.AlertTypeId IS NULL) AND a.MemberKey = 2Returned Data:SubjectAlertKey SubjectAlertViolationKey ViolationSubjectKey RuleKey ViolationSubjectTypeKey--------------- ------------------------ ------------------- ----------- -----------------------1 1 39 16 31 2 39 15 31 3 39 14 31 4 39 13 31 5 39 12 31 6 39 11 31 7 39 10 31 8 39 9 31 9 39 8 31 10 39 7 32 11 12 17 3.... and many more.
What I'm missing on my code?Why "NoViolation" column has no values where in fact there should be?Please help.Thank you.