Ja, du skal gøre dette med en underforespørgsel, prøv med noget som dette (hvis du vil have 4. række til at få adgang til datoen i 3. række):
SELECT mo.AgentID, mo.date,
@r AS 'LAG(date)',
(case when @r<Date then 'YES' when @r is null then 'IS NULL' else 'NO' end) 'Is Bigger',
(@r := Date) AS Date
FROM (
SELECT m.*
FROM (
SELECT @_date = NULL
) variable,
data m
ORDER BY
AgentID
) mo
WHERE (CASE WHEN @_date IS NULL OR @_date <> date THEN @r := NULL ELSE NULL END IS NULL)
AND (@_date := date) IS NOT NULL
Du kan se en fungerende demo her
Eller du kan prøve denne forespørgsel, hvis du vil have, at 3. række har adgang til dato i 4. række
SELECT AgentID,date,LEAD_date,concat(Difference,' days') FROM
(SELECT mo.AgentID,
@r AS LEAD_date,
DATEDIFF(@r,Date) as Difference,
(@r := Date) AS Date
FROM (
SELECT m.*
FROM (
SELECT @_date = NULL
) variable,
data m
ORDER BY
AgentID,date desc
) mo
WHERE (CASE WHEN @_date IS NULL OR @_date <> date THEN @r := NULL ELSE NULL END IS NULL)
AND (@_date := date) IS NOT NULL) T
order by AgentID,date;
Du kan se en fungerende demo her