这是我问题的试题表:
CREATE TABLE document (
id integer NOT NULL,
name character varying(120) NOT NULL,
owner_id bigint DEFAULT 0 NOT NULL,
doc_type_id smallint DEFAULT 1 NOT NULL,
archived boolean DEFAULT false NOT NULL,
insert_date timestamp without time zone DEFAULT now() NOT NULL,
modify_date timestamp without time zone DEFAULT now() NOT NULL,
last_writer_id bigint
);
Modify_date确定最后一次编辑文档的时间。
为了进行一些统计,我需要得到创建(Insert_date)和modify_date之间的时间。然后,为了显示条形图,我需要得到文档的计数,例如,这个时间间隔在0到5天、6天到10天之间。所以,我猜必须在查询中计算范围。预期的结果(或某种.)是:
Age Count
0-5 2
6-10 5
11-15 9
... ...
当然,年龄可以是0-5 == 0,6-10 == 1。我会准备数据来显示它们。
我发现了一个类似的帖子,但我无法将它应用到我的案件中。(为15分钟的windows - PostgreSQL选择数据)
谢谢你给我带来的任何答复。
编辑1:
范围需要从表中获得的最小和最大年龄动态生成。
发布于 2013-11-29 11:03:30
很抱歉,我不知道postgres,但如果您可以将其转换为postgres,它将起作用:
SELECT count(*)
FROM document
WHERE (TO_DAYS(modify_date) - TO_DAYS(insert_date)) < 11
AND (TO_DAYS(modify_date) - TO_DAYS(insert_date)) > 5
此查询计算6至10天内修改的文档数量。但是,您必须运行并修改查询以修改天数范围。
发布于 2013-11-29 11:04:13
最简单的方法(不是最好的表演!)是通过一个工会:
select '0-5' as age, count(*) as count
from document where insert_date < [6 days])
union
select '6-10' as age, count(*) as count
from document where between insert_date [6 days] and [11 days])
https://stackoverflow.com/questions/20283765
复制相似问题