我有一个用于记录Facebook应用程序用户操作的表。我希望得到一个不早于9分钟的特定操作ID的最新行。该表有很多其他统计信息列,所以我只会显示代表问题的2行:
timestamp facebookID producerID eventID actionID numOfTickets
2012-05-28 13:16:38 100003286974944 9 1741 cpf 2
2012-05-28 13:16:13 100003286974944 9 1741 cpf 4
我想要做的是获取actionID = 'cpf'
的最新行。
我尝试的是:
SELECT CONCAT_WS(' ', y.firstName, y.lastName) as fullName, x.facebookID, x.numOfTickets, MAX(x.timestamp)
FROM E4S_ANALYTICS.e4s_analytic_data x INNER JOIN E4S_FB.e4s_user_details y ON x.facebookID = y.facebookID
WHERE (x.actionID = 'CPF' AND x.numOfTickets > 0 AND x.producerID = 9 AND TIMEDIFF(NOW() , x.timestamp) < '00:09:00'
AND x.facebookID IN (SELECT facebookID FROM E4S_FB.e4s_session_data WHERE
TIMEDIFF(NOW() , sessionStart) < '00:09:00' ))
查询结果为:
facebookID numOfTickets MAX(x.timestamp)
100003286974944 4 2012-05-28 13:16:38
返回的时间戳是正确的,但numOfTickets
是4而不是2。
注意,内部查询:
SELECT facebookID FROM E4S_FB.e4s_session_data
WHERE TIMEDIFF(NOW() , sessionStart) < '00:09:00' ))
用于查看谁仍然登录到该应用程序。
答案 0 :(得分:1)
SELECT CONCAT_WS(' ', y.firstName, y.lastName) AS fullName,
x.facebookID,
x.numOfTickets,
x.timestamp
FROM (
SELECT *
FROM E4S_ANALYTICS.e4s_analytic_data
WHERE actionID = 'cpf'
AND numOfTickets > 0
AND producerID = 9
AND TIMEDIFF(NOW() , timestamp) < '00:09:00'
AND (facebookID, timestamp) IN (
SELECT facebookID, MAX(timestamp)
FROM E4S_ANALYTICS.e4s_analytic_data
WHERE actionID = 'cpf'
AND numOfTickets > 0
AND producerID = 9
AND TIMEDIFF(NOW() , timestamp) < '00:09:00'
AND facebookID IN (
SELECT facebookID
FROM E4S_FB.e4s_session_data
WHERE TIMEDIFF(NOW() , sessionStart) < '00:09:00'
)
GROUP BY facebookID
)
) AS x
INNER JOIN E4S_FB.e4s_user_details AS y USING (facebookID)
答案 1 :(得分:0)
您是否尝试过不使用MAX()? 假设您已经检查了JOIN和SUBQUERY结果。
SELECT CONCAT_WS(' ', y.firstName, y.lastName) as fullName, x.facebookID, x.numOfTickets, MAX(x.timestamp)
FROM E4S_ANALYTICS.e4s_analytic_data x
INNER JOIN E4S_FB.e4s_user_details y ON x.facebookID = y.facebookID
GROUP BY x.facebookID
WHERE (x.actionID = 'CPF' AND x.numOfTickets > 0 AND x.producerID = 9 AND TIMEDIFF(NOW() , x.timestamp) < '00:09:00'
AND x.facebookID IN
(SELECT facebookID FROM E4S_FB.e4s_session_data
WHERE TIMEDIFF(NOW() , sessionStart) < '00:09:00' ))
ORDER BY x.timestamp DESC;
答案 2 :(得分:0)
您可以使用order by timestamp desc
按照最新的顺序排列列,然后选择第一行
或尝试使用max
。