I have a table for logging a Facebook application user actions. I want to get the latest row that is not older then 9 minutes for a specific action ID. The table has a lot of columns of other statistical information, so I will show only 2 of the rows that represent the problem:
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
What I want to do is getting the latest row where actionID = 'cpf'.
What I’ve tried is:
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' ))
The result of the query is:
facebookID numOfTickets MAX(x.timestamp)
100003286974944 4 2012-05-28 13:16:38
The timestamp returned is correct but the numOfTickets is 4 instead of 2.
For note, the inner query:
SELECT facebookID FROM E4S_FB.e4s_session_data
WHERE TIMEDIFF(NOW() , sessionStart) < '00:09:00' ))
Is used to see who is still logged in to the application.
1 Answer