如何select按时间段(例如,小时)统计每个用户?
How to select count of each user by time period (e.g., hour)?
如何编写 Postgresql 查询以按小时查找用户计数?
Table:
date name
------------------- ----
2015-01-01 23:11:11 John
2015-02-02 23:22:22 John
2015-02-02 23:00:00 Mary
2015-02-02 23:59:59 Mary
2015-03-03 00:33:33 Mary
期望的输出:
hour | name | count
---------------------+---------+-------
2015-01-01 23:00:00 | John | 1
2015-02-02 23:00:00 | Mary | 2
2015-02-02 23:00:00 | John | 1
2015-03-03 00:00:00 | Mary | 1
我试过了 http://www.sqlfiddle.com/#!12/a50d4/2:
CREATE TABLE my_table (
date TIMESTAMP WITHOUT TIME ZONE,
name TEXT
);
INSERT INTO my_table (date, name) VALUES ('2015-01-01 23:11:11', 'John');
INSERT INTO my_table (date, name) VALUES ('2015-02-02 23:22:22', 'John');
INSERT INTO my_table (date, name) VALUES ('2015-02-02 23:00:00', 'Mary');
INSERT INTO my_table (date, name) VALUES ('2015-02-02 23:59:59', 'Mary');
INSERT INTO my_table (date, name) VALUES ('2015-03-03 00:33:33', 'Mary');
SELECT DISTINCT
date_trunc('hour', "date") AS hour,
name,
count(*) OVER (PARTITION BY date_trunc('hour', "date")) AS count
FROM my_table
ORDER BY hour, count;
但它给了我:
hour | name | count |
---------------------|------|-------|
2015-01-01 23:00:00 | John | 1 |
2015-02-02 23:00:00 | Mary | 3 |
2015-02-02 23:00:00 | John | 3 |
2015-03-03 00:00:00 | Mary | 1 |
相似:
- Select distinct users group by time range
- PostgreSQL: running count of rows for a query 'by minute'
select
date_trunc('hour', "date") as "hour",
name,
count(*) as "count"
from my_table
group by 1, 2
order by 1, 3 desc;
如果您想坚持使用 window 函数,您需要将 "name" 添加到您的列表中,如下所示:
http://www.sqlfiddle.com/#!12/a50d4/51
SELECT DISTINCT
date_trunc('hour', "date") AS "hour",
name,
count(name) OVER (PARTITION BY name, date_trunc('hour', "date")) AS "cnt"
FROM my_table
ORDER BY hour, cnt DESC
我想你正在找这个
SELECT DISTINCT
date_trunc('hour', "date") AS hour,
name,
count(*) as tcount
FROM my_table
GROUP BY hour,name
ORDER BY hour, count(*) Desc
输出:
hour | name | count
---------------------+---------+-------
2015-01-01 23:00:00 | John | 1
2015-02-02 23:00:00 | Mary | 2
2015-02-02 23:00:00 | John | 1
2015-03-03 00:00:00 | Mary | 1
这对我有用:http://www.sqlfiddle.com/#!12/a50d4/57
SELECT DISTINCT name,
date_trunc('hour', date) as hour,
count(*) as count
FROM my_table
GROUP BY name, hour
ORDER BY hour, count DESC;
如何编写 Postgresql 查询以按小时查找用户计数?
Table:
date name
------------------- ----
2015-01-01 23:11:11 John
2015-02-02 23:22:22 John
2015-02-02 23:00:00 Mary
2015-02-02 23:59:59 Mary
2015-03-03 00:33:33 Mary
期望的输出:
hour | name | count
---------------------+---------+-------
2015-01-01 23:00:00 | John | 1
2015-02-02 23:00:00 | Mary | 2
2015-02-02 23:00:00 | John | 1
2015-03-03 00:00:00 | Mary | 1
我试过了 http://www.sqlfiddle.com/#!12/a50d4/2:
CREATE TABLE my_table (
date TIMESTAMP WITHOUT TIME ZONE,
name TEXT
);
INSERT INTO my_table (date, name) VALUES ('2015-01-01 23:11:11', 'John');
INSERT INTO my_table (date, name) VALUES ('2015-02-02 23:22:22', 'John');
INSERT INTO my_table (date, name) VALUES ('2015-02-02 23:00:00', 'Mary');
INSERT INTO my_table (date, name) VALUES ('2015-02-02 23:59:59', 'Mary');
INSERT INTO my_table (date, name) VALUES ('2015-03-03 00:33:33', 'Mary');
SELECT DISTINCT
date_trunc('hour', "date") AS hour,
name,
count(*) OVER (PARTITION BY date_trunc('hour', "date")) AS count
FROM my_table
ORDER BY hour, count;
但它给了我:
hour | name | count |
---------------------|------|-------|
2015-01-01 23:00:00 | John | 1 |
2015-02-02 23:00:00 | Mary | 3 |
2015-02-02 23:00:00 | John | 3 |
2015-03-03 00:00:00 | Mary | 1 |
相似:
- Select distinct users group by time range
- PostgreSQL: running count of rows for a query 'by minute'
select
date_trunc('hour', "date") as "hour",
name,
count(*) as "count"
from my_table
group by 1, 2
order by 1, 3 desc;
如果您想坚持使用 window 函数,您需要将 "name" 添加到您的列表中,如下所示:
http://www.sqlfiddle.com/#!12/a50d4/51
SELECT DISTINCT
date_trunc('hour', "date") AS "hour",
name,
count(name) OVER (PARTITION BY name, date_trunc('hour', "date")) AS "cnt"
FROM my_table
ORDER BY hour, cnt DESC
我想你正在找这个
SELECT DISTINCT
date_trunc('hour', "date") AS hour,
name,
count(*) as tcount
FROM my_table
GROUP BY hour,name
ORDER BY hour, count(*) Desc
输出:
hour | name | count
---------------------+---------+-------
2015-01-01 23:00:00 | John | 1
2015-02-02 23:00:00 | Mary | 2
2015-02-02 23:00:00 | John | 1
2015-03-03 00:00:00 | Mary | 1
这对我有用:http://www.sqlfiddle.com/#!12/a50d4/57
SELECT DISTINCT name,
date_trunc('hour', date) as hour,
count(*) as count
FROM my_table
GROUP BY name, hour
ORDER BY hour, count DESC;