Trying to convert this working SQL from MySql database to work on a MS Access database:
SELECT u.LastName AS LAST, u.FirstName AS FIRST,
MAX(IF(`e.ClassName`='MDC (Intro)', DateCompleted, NULL)) AS 'MDC',
MAX(IF(`e.ClassName`='800 MHz Radio (Intro)', DateCompleted, NULL)) AS 'RADIO',
MAX(IF(`e.ClassName`='ePCR (Intro)', DateCompleted, NULL)) AS 'ePCR',
MAX(IF(`e.ClassName`='Firehouse (Incident)', DateCompleted, NULL)) AS 'Firehouse'
FROM EnrollmentsTbl e INNER JOIN UsersDataTbl u ON e.UserName = u.UserName
GROUP BY e.UserName
WHERE u.LastName LIKE 'Bar%'
ORDER BY u.LastName
A few items of syntax requires conversion:
GROUP BY
must include ALL non-aggregated columns. So add FirstName
and LastName
to the grouping. IF
for conditional expressions but IIF()
. %
wildcard for LIKE
evaluation but Access via the GUI .exe program uses *
by default. But its ALIKE
operator is valid in both setup types. Consider the following SQL adjustment:
SELECT u.LastName AS `LAST`, u.FirstName AS `FIRST`,
MAX(IIF(e.`ClassName`='MDC (Intro)', DateCompleted, NULL)) AS `MDC`,
MAX(IIF(e.`ClassName`='800 MHz Radio (Intro)', DateCompleted, NULL)) AS `RADIO`,
MAX(IIF(e.`ClassName`='ePCR (Intro)', DateCompleted, NULL)) AS `ePCR`,
MAX(IIF(e.`ClassName`='Firehouse (Incident)', DateCompleted, NULL)) AS `Firehouse`
FROM EnrollmentsTbl e
INNER JOIN UsersDataTbl u ON e.UserName = u.UserName
GROUP BY u.LastName, u.FirstName, e.UserName
WHERE u.LastName ALIKE 'Bar%'
ORDER BY u.LastName
I'm not sure if its aligned exactly as I want it (need first/last in the first column, and the GROUP BY
was the main problem, but this returns results from Access:
SELECT u.LastName AS [LAST],
Max(IIf([e.ClassName]='MDC (Intro)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [MDC],
Max(IIf([e.ClassName]='800 MHz Radio (Intro)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [RADIO],
Max(IIf([e.ClassName]='ePCR (Intro)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [ePCR],
Max(IIf([e.ClassName]='Firehouse (Incident)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [Firehouse]
FROM EnrollmentsTbl e
INNER JOIN UsersDataTbl u ON e.UserName = u.UserName
GROUP BY u.LastName, u.FirstName, e.UserName
WHERE u.UserName LIKE 'bar%'
ORDER BY u.LastName;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.