

SELECT 
	--EvnVaccination_isManual,
	--EV.EvnVaccination_id,
	--VD.VaccinationData_id,
CASE
   WHEN vt.VaccinationType_id=163 and EvnVaccination_ReactionSize is NOT NULL
    THEN cast(EvnVaccination_ReactionSize as VARCHAR)|| ', мм'
   else '' 
END "ReactionSize" 
,TubDiagnosisType_Name AS "TubDiagnosisType_Name"
,coalesce(vlass.VaccinationClass_id, -1) as "VaccinationClass_id"
	,V.VaccinationType_id AS "VaccinationType_id"
	,V.Vaccination_Code AS "Vaccination_Code"
	,V.Vaccination_Name AS "Vaccination_Name"
	,dbo.AgeCalculate (ps.person_birthday, ev.evnvaccination_setdate) "age"
	,TO_CHAR(EV.EvnVaccination_setDate, 'DD.MM.YYYY') as "DateVac"
	,cast(EV.Prep_Dose as VARCHAR) ||' '||GU.GoodsUnit_Name AS "DoseWithUnits"
	,EV.Prep_Ser AS "Prep_Ser"
	,Prep_id AS "Prep_id"
	,D.DrugTorg_Name AS "DrugTorg_Name"
	,EV.EvnVaccination_Reaction  AS "EvnVaccination_Reaction"
	,vrt.VaccinationRefuseType_Name AS "VaccinationRefuseType_Name"
	,TO_CHAR(PersonVaccinationRefuse_begDT,	'DD.MM.YYYY') ||' - '|| to_char(PersonVaccinationRefuse_endDT,'DD.MM.YYYY')  "dateRefuse"

FROM dbo.v_EvnVaccination EV
	LEFT OUTER JOIN vc.v_VaccinationData VD ON EV.EvnVaccination_id = VD.EvnVaccination_id
	LEFT OUTER JOIN vc.v_Vaccination V ON V.Vaccination_id = VD.Vaccination_id
	LEFT OUTER JOIN vc.vaccinationtype vt ON vt.vaccinationtype_id = v.vaccinationtype_id	
	LEFT OUTER JOIN vc.v_VaccinationClass vlass ON vt.VaccinationClass_id = vlass.VaccinationClass_id	
	LEFT OUTER JOIN vc.v_TubDiagnosisType tdt  ON vt.TubDiagnosisType_id = tdt.TubDiagnosisType_id
	--LEFT OUTER JOIN vc.v_VaccinationSourceType VST WITH (NOLOCK) ON VST.VaccinationSourceType_id = EV.VaccinationSourceType_id
	--LEFT OUTER JOIN v_Lpu L WITH (NOLOCK) ON L.Lpu_id = EV.Lpu_mid	
	LEFT OUTER JOIN rls.v_Drug D ON D.Drug_id = EV.Prep_id
	LEFT OUTER JOIN dbo.v_GoodsUnit GU ON GU.GoodsUnit_id = EV.Prep_DoseType::int8
	--LEFT OUTER JOIN dbo.PrescriptionIntroType PIT WITH (NOLOCK) ON PIT.PrescriptionIntroType_id = EV.PrescriptionIntroType_id
	--LEFT OUTER JOIN dbo.PrescriptionIntroPlace PIP WITH (NOLOCK) ON PIP.PrescriptionIntroPlace_id = EV.PrescriptionIntroPlace_id
	--LEFT OUTER JOIN vc.VaccinationTubReactionType VTRT WITH (NOLOCK) ON VTRT.VaccinationTubReactionType_id = EV.VaccinationTubReactionType_id
	--LEFT OUTER JOIN vc.VaccinationTubReactionLevelType VTRLT WITH (NOLOCK) ON VTRLT.VaccinationTubReactionLevelType_id = EV.VaccinationTubReactionLevelType_id
	--LEFT OUTER JOIN vc.VaccinationReactionNotify VRN WITH (NOLOCK) ON VRN.EvnVaccination_id = EV.EvnVaccination_id  
	LEFT OUTER JOIN vc.v_PersonVaccinationRefuse  PVR  ON PVR.Vaccination_id = V.Vaccination_id   
	LEFT OUTER JOIN vc.v_VaccinationRefuseType vrt ON vrt.VaccinationRefuseType_id = PVR.VaccinationRefuseType_id
	LEFT OUTER JOIN v_personstate ps ON ps.person_id = ev.person_id

WHERE EV.Person_id = cast( $1 as bigint)
AND COALESCE(EV.EvnStatus_id, 0) = 40 
AND VD.Vaccination_id IS NOT NULL 
order by 3


