Skip to content

Instantly share code, notes, and snippets.

@Kavignon
Last active December 13, 2017 16:36
Show Gist options
  • Save Kavignon/915bcc8ddb563ba404548441db82ce13 to your computer and use it in GitHub Desktop.
Save Kavignon/915bcc8ddb563ba404548441db82ce13 to your computer and use it in GitHub Desktop.
Query extracting whether or not an employee is suited (qualified) for a training
@"with SomeTemporaryEmployeeTable(EmployeeID, SkillID, IsQualified)
as
(
select e.ID, jSkill.ID, case when jsQualifications.QualificationID is null then 0 else 1 end
from Employee e
join Job job on job.EmployeeID = e.ID
and job.ID = (select max(ID) from Job
where EmployeeID = job.EmployeeID)
join JobGroup jobGroup on jobGroup.ID = job.JobGroupID
join JobSkill jSkill on jSkill.JobGroupID = job.JobGroupID
left join JobSkillQualification jsQualifications on jsQualifications.SkillID = cSkill.SkillID and jsQualifications.QualificationID = 0
)
select distinct CONVERT(DATETIME,ISNULL(DocHistory.EffectDate, DATEADD(month, 6,activeTraining.[Date]))) , employee.EmployeeID
from SomeTemporaryEmployeeTable employee
inner join EmployeeFormation employeeFormation on employeeFormation.EmployeeID = employee.EmployeeID
and employeeFormation.Passed is not null
inner join Training activeTraining on activeTraining.QualificationID = 0
and activeTraining.Approved is not null and activeTraining.Approved = 1
or employeeFormation.Passed is not null and employeeFormation.Passed = 1
inner join JobQualification jQualifications on jQualifications.ID = activeTraining.QualificationID
left join JobDocuments jDocuments on jDocuments.EmployeeID = employee.EmployeeID and jDocuments.QualificationID = jQualifications.ID
left join JobDocumentHistories jdHistories on jdHistories.DocumentID = jDocuments.ID and EffectDate =
(select max(EffectDate) from JobDocumentHistories JDHistories
where JDHistories.DocumentID = (select max(ID) from JobDocuments))
and activeTraining.Date < JDHistories.UploadTime and JDHistories.EffectDate < DATEADD(month, 6,activeTraining.Date)
where employee.IsQualified = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment