I need to have my data showing in one row whether an individual has 'Finished' specific courses within the last 3 years.
This is my existing table & desired table (hope this link works as I can't post images yet!): https://i.stack.imgur.com/c8oJO.png
I have tried this code:
SELECT DISTINCT
EN,
(First_Name + ' ' + Last_Name) as Name,
First_Aid = CASE
WHEN Course = 'First Aid'
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN 'F'
ELSE 'Nope'
END,
Manual_Handling = CASE
WHEN Course = 'Manual Handling'
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN 'F'
ELSE 'Nope'
END,
Fire_Safety = CASE
WHEN Course = 'Fire Safety'
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN 'F'
ELSE 'Nope'
END
into MyTraining
FROM Learning_History`
This produces accurate results, but with a separate row for each CASE statement (plus an extra row with all “Nope” results). So I have tried this code:
SELECT
EN,
(First_Name + ' ' + Last_Name) as Name,
First_Aid =MAX(CASE
WHEN Course = 'First Aid'
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN 'F'
ELSE 'Nope'
END),
Manual_Handling =MAX(CASE
WHEN Course = 'Manual Handling'
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN 'F'
ELSE 'Nope'
END),
Fire_Safety =MAX(CASE
WHEN Course = 'Fire Safety'
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN 'F'
ELSE 'Nope'
END)
into MyTraining
FROM Learning_History
Group By EN,First_Name, Last_Name
This code does produce 1 row - but all the results are 'Nope'.
I don't really understand much of this (just Googled for most of the code) as I'm very much a beginner with SQL (so I may have made some simple errors). I think pivot tables may be the way to go - but I can't get my head around them...