SELECT
IR.INCIDENTID, IR.ASSESSID, IR.REVIEWDATE, DONR.*
FROM
IncidentReview IR
LEFT JOIN
(SELECT *
FROM
(SELECT
Category = 'ADL', DONRFunction = 'Eating',
IR.ASSESSID, Comment, LOI, UMN
FROM
IncidentReview IR
LEFT JOIN
(SELECT
ASSESSID, SCALE,
Comment = CONVERT (varchar, ITEM)
FROM
IncidentDetReview IDR
WHERE
IDR.SCALEID = 30085729) Comment ON IR.ASSESSID = Comment.ASSESSID
LEFT JOIN
(SELECT
ASSESSID, SCALE,
LOI = CASE
WHEN CONVERT(VARCHAR, ITEM) = '' THEN NULL
ELSE CONVERT(INT, LEFT (CONVERT(VARCHAR, ITEM), 1))
END
FROM
IncidentDetReview IDR
WHERE
IDR.SCALEID = 30085779) LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN
(SELECT
ASSESSID, SCALE,
UMN = CASE WHEN CONVERT(VARCHAR,ITEM) = '' THEN NULL
ELSE CONVERT(INT, LEFT (CONVERT(VARCHAR, ITEM), 1))
END
FROM
IncidentDetReview IDR
WHERE
IDR.SCALEID = 30085780) UMN ON IR.ASSESSID = UMN.ASSESSID
) eat ON IR.ASSESSID = EAT.ASSESSID
FULL OUTER JOIN
(SELECT *
FROM
(SELECT
Category = 'ADL', DONRFunction = 'Bathing',
IR.ASSESSID, Comment, LOI, UMN
FROM
IncidentReview IR
LEFT JOIN
(SELECT
ASSESSID, SCALE,
Comment = CONVERT(varchar, ITEM)
FROM
IncidentDetReview IDR
WHERE
IDR.SCALEID = 30085733) Comment ON IR.ASSESSID = Comment.ASSESSID
LEFT JOIN
(SELECT
ASSESSID, SCALE,
LOI = CASE WHEN CONVERT(VARCHAR, ITEM) = ''
THEN NULL
ELSE CONVERT(INT, LEFT (CONVERT(VARCHAR, ITEM), 1))
END
FROM IncidentDetReview IDR
WHERE IDR.SCALEID = 30085781) LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN
(SELECT
ASSESSID, SCALE,
UMN = CASE
WHEN CONVERT(VARCHAR, ITEM) = ''
THEN NULL
ELSE CONVERT(INT, LEFT (CONVERT(VARCHAR, ITEM), 1))
END
FROM IncidentDetReview IDR
WHERE IDR.SCALEID = 30085782) UMN ON IR.ASSESSID = UMN.ASSESSID) BATH ON EAT.ASSESSID = BATH.ASSESSID
)) DONR ON IR.ASSESSID = DONR.ASSESSID
WHERE
IR.IncidentID = 176744
AND IR.SCREENDESIGNID = 2105