drop view if exists AKUZ.V_EMDR_MESSAGE_SEND;
CREATE VIEW AKUZ.V_EMDR_MESSAGE_SEND
AS

WITH CTE AS(
	SELECT 
		 MedicalRecordID
		,EmdrDocType
	FROM AKUZ.T_CARD_NOTE
	WHERE (
		ReadyToBeSent = 1
		AND IsDelayed = 0
		)
)
SELECT 
	t0.EmdrMessageID
	,T0.WhereToSend
	,T1.SendingPrioritet
FROM CTE C
	JOIN AKUZ.T_EMDR_MESSAGE t0 ON t0.CardNote = C.MedicalRecordID
	JOIN AKUZ.T_EMDR_DOC_TYPE t1 ON t1.EmdrDocTypeID = C.EmdrDocType
WHERE
	t0.RegistrationState = 0	