【发布时间】:2013-12-30 20:37:45
【问题描述】:
我有下表,很简单。
==========================================================================
attendanceID | agentID | incurredDate | points | comment
==========================================================================
10 | vimunson | 2013-07-22 | 2 | Some Text
11 | vimunson | 2013-07-29 | 2 | Some Text
12 | vimunson | 2013-12-06 | 1 | Some Text
他的查询如下:
SELECT
attendanceID,
agentID,
incurredDate,
leadDate,
points,
@1F:=IF(incurredDate <= curdate() - 90
AND leadDate = NULL,
points - 1,
IF(DATEDIFF(leadDate, incurredDate) > 90,
points - 1,
points)) AS '1stFallOff',
@2F:=IF(incurredDate <= curdate() - 180
AND leadDate = NULL,
points - 2,
IF(DATEDIFF(leadDate, incurredDate) > 180,
points - 2,
@1F)) AS '2ndFallOff',
IF(@total < 0, 0, @total:=@total + @2F) AS Total,
comment,
linked
FROM
(SELECT
attendanceID,
mo.agentID,
@r AS leadDate,
(@r:=incurredDate) AS incurredDate,
comment,
points,
linked
FROM
(SELECT
m . *
FROM
(SELECT @_date = NULL, @total:=0) varaible, attendance m
ORDER by agentID , incurredDate desc) mo
where
agentID = 'vimunson'
AND (case
WHEN @_date is NULL or @_date <> incurredDate THEN @r:=NULL
ELSE NULL
END IS NULL)
AND (@_date:=incurredDate) IS NOT NULL) T
ORDER BY agentID , incurredDate
当我运行查询时,它返回以下内容:
========================================================================================================================================
attendanceID | agentID | incurredDate | leadDate | points | 1stFallOff | 2ndFallOff | Total | comment
========================================================================================================================================
10 | vimunson | 2013-07-22 | NULL | 2 | 2 | 2 | 2 | Some Text
11 | vimunson | 2013-07-29 | NULL | 2 | 2 | 2 | 4 | Some Text
12 | vimunson | 2013-12-06 | NULL | 1 | 2 | 1 | 5 | Some Text
我无法弄清楚为什么leadDate 列是“空”。我已将其缩小到用户会话。例如,如果我使用相同的用户会话再次运行它,它将返回我想要的内容。
【问题讨论】:
-
对我来说只有一次为空。这是您想要的结果吗:sqlfiddle.com/#!2/6f0ac/3/0?
-
是的,
leadDate在同一个用户会话中第一次为空。但是,如果我在同一个用户中并再次运行它,它会给我我想要的结果。 -
那么为什么当我在服务器上运行它但在 sqlfiddle 上运行良好时这不起作用?
标签: mysql sql select stored-procedures join