Is there a way to correctly write this? I want the sum of the difference between the date started(Started) and The Max Completed date for the Case Inspection records where Result = In Compliance and the case_no is in the date range.
Select SUM(DATEDIFF(dd, STARTED,
(select max(COMPLETED_DATE) from Case_Inspections
where RESULT = 'In Compliance'
and CASE_NO in (select CASE_NO from Case_Main
where STARTED >= '2015-06-01' and STARTED <= '2015-06-30'))))