Запрос из SQL Anywhere не работает на MS SQL, ошибка:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery
SELECT (CASE
WHEN EXISTS (SELECT 1
FROM READCHK_READSEQ_OVRCOMP
WHERE READCHK_ID = :readChkId AND READSEQ_ID = :readSeqId)
THEN 'M'
WHEN ( COUNT(*) = COALESCE( SUM( SELECT COUNT(*)
FROM READCHKFLEX flx
WHERE flx.READCHKFLD_ID = rcfld.READCHKFLD_ID
AND flx.READCHK_ID = :readChkId)), 0 )
THEN 'A'
ELSE 'I'
END) AS CompletionStatus
FROM READSEQFLD rsfld
JOIN READSEQ rseq on rseq.READSEQ_ID = rsfld.READSEQ_ID
JOIN READCHKPAGEFLD rcpf ON rcpf.READCHKTYPE_ID = rseq.READCHKTYPE_ID
AND rcpf.READCHKFLD_ID = rsfld.READCHKFLD_ID
JOIN READCHKFLD rcfld ON rcfld.READCHKFLD_ID = rcpf.READCHKFLD_ID
WHERE rsfld.READSEQ_ID = :readSeqId
AND UPPER(rsfld.ACTIVATE_FLG) = 'Y'
AND rcfld.CALFORM_ID IS NULL
AND (rcpf.fld_properties is null OR UPPER(rcpf.fld_properties) <> 'HIDE')