通过一个选择计算每日基数的访问量

时间:2014-02-18 14:22:44

标签: sql postgresql select aggregate-functions range-types

我在PostgreSQL中有这个表:

CREATE TABLE visits(
id BIGSERIAL NOT NULL PRIMARY KEY,
timeslot TSRANGE NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id),
CONSTRAINT overlapping_timeslots EXCLUDE USING GIST (
    user_id WITH =,
    timeslot WITH &&
));

使用此数据:

|   id  |   timeslot                                |   user_id |
|   1   |   10.02.2014 10:00 - 10.02.2014 17:00     |   2       |
|   2   |   10.02.2014 18:00 - 10.02.2014 19:00     |   2       |    
|   3   |   11.02.2014 01:00 - 11.02.2014 02:00     |   2       |
|   4   |   10.02.2014 12:00 - 11.02.2014 17:00     |   2       |
|   5   |   11.02.2014 12:00 - 11.02.2014 12:30     |   2       |

我需要知道每天有多少用户访问我的商店。如果用户每天访问商店两次,则应计算两次。

在上面的示例中,它应该是。

Users at 10.02 = 3 (ID: 1,2,4)
Users at 11.02 = 3 (ID: 3,4,5)

2 个答案:

答案 0 :(得分:1)

假设缺乏“每天”定义的任意时间段:

SELECT  day, count(*) AS visits, array_agg(id) AS ids
FROM    generate_series ('2014-02-10'::date
                       , '2014-02-12'::date
                       , interval '1 day') AS d(day)
JOIN    visits ON tsrange(day::timestamp
                        , day::timestamp + interval '1 day') && timeslot
GROUP   BY 1;

&& is the "overlap" operator for range types

使用LEFT JOIN在结果中包含0次访问的天数。

-> SQLfiddle demo.

答案 1 :(得分:0)

SELECT  user_id, LEFT(timeslot, 10) as date_visit ,COUNT(*) as day_visit
FROM vistis
GROUP BY USER_ID,  LEFT(timeslot, 10)

UNION

SELECT  user_id, SUBSTR(timeslot, 13, 23) as date_visit ,COUNT(*) as day_visit
FROM vistis
GROUP BY USER_ID,  SUBSTR(timeslot, 13, 23)

如果您想在一天内获得所有用户的计数器,请在user_id列中删除select和group