0

我想计算 30 天范围内的唯一帐户总数。我在 MySQL (InfiniDB) 上使用的引擎似乎只支持在相等条件下连接表。

我的表如下所示:

sessions (date_id, account_id) =
{ '2013-07-07', 5
  '2013-07-07', 5
  '2013-07-07', 1
  '2013-07-08', 3
  '2013-07-09', 3
}

我当前的查询如下所示:

SELECT
  date_id, (
    SELECT COUNT(DISTINCT account_id)
    FROM sessions t2
    WHERE t2.date_id BETWEEN DATE_SUB(t1.date_id, INTERVAL 30 DAY) AND t1.date_id
  ) cnt
FROM sessions t1
GROUP BY date_id

仅支持等连接,因此BETWEEN操作员无法连接表。

任何帮助是极大的赞赏 :)

4

2 回答 2

0

Starting InfiniDB 4.0 you can do such calculation with Windowing function: You query look like following

SELECT date_id, COUNT(act_id)  AS Uniques
FROM ( SELECT date_id, 
              DISTINCT(account_id) OVER (PARTITION BY date_id RANGE INTERVAL 30 DAY PRECEDING) act_id
       FROM sessions ) t1
GROUP BY date_id

This would give you the expected results

于 2014-04-15T01:43:53.117 回答
0

我对 InfiniDB 不是特别熟悉,但我认为更简洁的是:

SELECT
  t1.date_id,
  COUNT(DISTINCT t2.account_id) AS Uniques
FROM sessions t1
INNER JOIN sessions t2 
  ON t2.date_id BETWEEN DATE_SUB(t1.date_id, INTERVAL 30 DAY) AND t1.date_id
GROUP BY t1.date_id;

结果:

DATE_ID                   UNIQUES
July, 07 2013 00:00:00+0000   2
July, 08 2013 00:00:00+0000   3
July, 09 2013 00:00:00+0000   3

sqlfiddle demo

这是你说的不支持吗?

于 2013-11-14T20:15:22.403 回答