0

我有一个用于记录 Facebook 应用程序用户操作的表。我想获取特定操作 ID 不超过 9 分钟的最新行。该表有很多其他统计信息列,因此我将仅显示代表问题的 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

我想要做的是获得最新的行 where 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' ))

用于查看谁仍然登录到应用程序。

4

3 回答 3

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)
于 2012-05-28T12:19:57.630 回答
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;
于 2012-05-28T11:46:42.657 回答
0

您可以使用order by timestamp desc先根据最新对列进行排序,然后选择第一行。
或尝试使用max.

于 2012-05-28T11:49:19.647 回答