I would expect the query below to name the case column according to the date range - however all are defaulting to error - what am I missing? It has been quite sometime since I have worked in mySQL. I am trying to group based on the intervals overdue, today, tomorrow, 30 days, 60 days, 90 days.
SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)AS rDate,
CURDATE()AS rDate,DATE_ADD(CURDATE(), INTERVAL 1 DAY)AS rDate,
DATE_ADD(CURDATE(), INTERVAL 7 DAY)AS rDate,
DATE_ADD(CURDATE(), INTERVAL 14 DAY)AS rDate,
DATE_ADD(CURDATE(), INTERVAL 30 DAY)AS rDate,
DATE_ADD(CURDATE(), INTERVAL 60 DAY)AS rDate,
DATE_ADD(CURDATE(), INTERVAL 90 DAY)AS rDate,
CASE a.nextDueDate
WHEN a.nextDueDate < CURDATE() THEN 'Overdue'
WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 7
DAY))THEN '7 Days'
WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL
14 DAY))THEN '15 Days'
WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 30 DAY))THEN '30 Days'
WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 60 DAY))THEN '60 Days'
WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 90 DAY))THEN '90 Days'
ELSE 'Error'
END,
g.groupId,g.name AS groupName,l.logId,l.name AS logName, i.itemID,
i.name AS itemName, le.userName,completed, i.optimalMin,i.optimalMax ,le.value,
u.name AS unitDescription,
g1.parentId AS parentId1, g1.name AS group1Name,g2.parentId AS parentId2, g2.name
AS group2Name,
g3.parentId AS parentId3, g3.name AS group3Name,
a.name AS activityName,scheduleType,scheduleInterval,a.details,nextDueDate
FROM logExceptions le
INNER JOIN logs l ON l.logID = le.logID
INNER JOIN groups g ON g.groupId = l.groupId
INNER JOIN items i ON le.itemId = i.itemId
INNER JOIN activities a ON l.logId = a.logId
LEFT JOIN units u ON i.unitId = u.unitId
LEFT JOIN groups g1 ON g.parentId = g1.groupId
LEFT JOIN groups g2 ON g1.parentId = g2.groupId
LEFT JOIN groups g3 ON g2.parentId = g3.groupId
WHERE nextDueDate IS NOT NULL
Remove a.nextDueDate
from after the CASE
keyword.
The CASE
expression comes in two forms:
CASE <expression>
WHEN <test-value1> THEN <result-value1>
WHEN <test-value2> THEN <result-value2>
...
ELSE <else-value>
END
In this form, the value of <expression>
is compared against each <test-valueN>
, and when it matches, the corresponding <result-valueN>
is returned. If none match, the <else-value>
is returned. This form is analogous to switch
or case
statements in other programming languages.
The other form is:
CASE WHEN <test-expression1> THEN <result-value1>
WHEN <test-expression2> THEN <result-value2>
...
ELSE <else-value>
END
In this case, each <test-expressionN>
is evaluated, and if when one is true the corresponding <result-valueN>
is returned. If none are true, the <else-value>
is returned. This is analogous to if/then/elseif/else
in other programming languages.
Since you're using the latter type of CASE
, you shouldn't have an expression right after CASE
.
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.