はい、サブクエリでこれを行う必要があります。次のようにしてみてください(4行目で3行目の日付にアクセスする場合):
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
動作するデモを
または、3行目が4行目の日付にアクセスできるようにする場合は、このクエリを試すことができます
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;
動作するデモを