【发布时间】:2015-08-25 08:34:00
【问题描述】:
我有一个简单的 MySQL sql 脚本,它输出特定表随时间变化的行数(基于表中的日期时间字段)
SELECT concat('M-', s.label)
, s.cnt
, @tot := @tot + s.cnt AS running_subtotal
FROM ( SELECT DATE_FORMAT(t.created,'%y-%m') AS `label`
, COUNT(t.id) AS cnt
FROM `templates` t
GROUP BY `label`
ORDER BY `label`
) s
CROSS
JOIN ( SELECT @tot := 0 ) i
现在我想将它迁移到 PostgreSQL,但不知道如何将变量迁移到基于 pg 的语法。
内部语句当然没问题:
SELECT TO_CHAR(t.created,'YYYY-MM') AS label
, COUNT(t.id) AS cnt
FROM templates t
GROUP BY label
ORDER BY label
这里有谁可以帮我解决可变部分的问题吗?
这是一个包含数据的简单表格:
create TABLE "templates" (
"id" bigserial,
"title" varchar(2048) default NULL::character varying,
"created" timestamp,
PRIMARY KEY ("id")
);
insert into templates(title, created) values('test', '2011-03-01');
insert into templates(title, created) values('test 2', '2011-03-02');
insert into templates(title, created) values('test 3', '2011-03-03');
insert into templates(title, created) values('test 4', '2011-03-04');
insert into templates(title, created) values('test 5', '2011-03-05');
insert into templates(title, created) values('test 6', '2011-04-01');
insert into templates(title, created) values('test 7', '2011-04-02');
insert into templates(title, created) values('test 8', '2011-04-03');
insert into templates(title, created) values('test 9', '2011-04-04');
insert into templates(title, created) values('test 10', '2011-04-05');
… // 300 more for 2011-05
此查询的示例输出(基于带有“created”列的记录)是:
M-11-03:5 5 M-11-04:5 10(5 + 5) M-11-05:300 310(5 + 5 + 300)【问题讨论】:
-
您可能需要一些窗口功能。但是,如果您需要帮助,请通过添加一些数据以及您真正期望的结果来更新您的问题。
-
谢谢@Houari!我添加了示例输出以便于识别目标。
-
@wingedpanther 完成!
-
无需将日期转换为字符。您可以选择和聚合
date_trunc('mon', created)