ALTER TABLE [AKUZ].[T_UNDERAGE_EXAMINATION] ADD [HealthGoodBefore] bit NULL
GO

update ue
set ue.HealthGoodBefore = 1
from [AKUZ].[T_UNDERAGE_EXAMINATION] ue
where not exists
(
	select *
	from AKUZ.T_EXAMINATION_DIAGNOSIS_BEFORE edb
	inner join AKUZ.T_EXAMINATION_DIAGNOSIS ed
		on ed.ExaminationDiagnosisID = edb.ExaminationDiagnosisID
		and ed.UnderageExamination = ue.MedicalRecordID
)
GO

update ue
set ue.HealthGoodBefore = 0
from [AKUZ].[T_UNDERAGE_EXAMINATION] ue
where ue.HealthGoodBefore is null
GO

ALTER TABLE [AKUZ].[T_UNDERAGE_EXAMINATION] ALTER COLUMN [HealthGoodBefore] bit NOT NULL
GO

ALTER TABLE [AKUZ].[T_UNDERAGE_EXAMINATION] ADD [HealthGoodAfter] bit NULL
GO

update ue
set ue.HealthGoodAfter = 1
from [AKUZ].[T_UNDERAGE_EXAMINATION] ue
where not exists
(
	select *
	from AKUZ.T_EXAMINATION_DIAGNOSIS_AFTER eda
	inner join AKUZ.T_EXAMINATION_DIAGNOSIS ed
		on ed.ExaminationDiagnosisID = eda.ExaminationDiagnosisID
		and ed.UnderageExamination = ue.MedicalRecordID
)
GO

update ue
set ue.HealthGoodAfter = 0
from [AKUZ].[T_UNDERAGE_EXAMINATION] ue
where ue.HealthGoodAfter is null
GO

ALTER TABLE [AKUZ].[T_UNDERAGE_EXAMINATION] ALTER COLUMN [HealthGoodAfter] bit NOT NULL
GO