如何向此查询添加显示不同行数的列?
我不知道如何清楚地问我的问题,所以我只能给你看钱。
首先,这里有一个示例表:
CREATE TABLE sandbox (
id integer NOT NULL,
callsign text NOT NULL,
this text NOT NULL,
that text NOT NULL,
"timestamp" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE SEQUENCE sandbox_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE sandbox_id_seq OWNED BY sandbox.id;
SELECT pg_catalog.setval('sandbox_id_seq', 14, true);
ALTER TABLE sandbox ALTER COLUMN id SET DEFAULT nextval('sandbox_id_seq'::regclass);
INSERT INTO sandbox VALUES (1, 'alpha', 'foo', 'qux', '2010-12-29 16:51:09.897579+00');
INSERT INTO sandbox VALUES (2, 'alpha', 'foo', 'qux', '2010-12-29 16:51:36.108867+00');
INSERT INTO sandbox VALUES (3, 'bravo', 'bar', 'quxx', '2010-12-29 16:52:36.370507+00');
INSERT INTO sandbox VALUES (4, 'bravo', 'foo', 'quxx', '2010-12-29 16:52:47.584663+00');
INSERT INTO sandbox VALUES (5, 'charlie', 'foo', 'corge', '2010-12-29 16:53:00.742356+00');
INSERT INTO sandbox VALUES (6, 'delta', 'foo', 'qux', '2010-12-29 16:53:10.884721+00');
INSERT INTO sandbox VALUES (7, 'alpha', 'foo', 'corge', '2010-12-29 16:53:21.242904+00');
INSERT INTO sandbox VALUES (8, 'alpha', 'bar', 'corge', '2010-12-29 16:54:33.318907+00');
INSERT INTO sandbox VALUES (9, 'alpha', 'baz', 'quxx', '2010-12-29 16:54:38.727095+00');
INSERT INTO sandbox VALUES (10, 'alpha', 'bar', 'qux', '2010-12-29 16:54:46.237294+00');
INSERT INTO sandbox VALUES (11, 'alpha', 'baz', 'qux', '2010-12-29 16:54:53.891606+00');
INSERT INTO sandbox VALUES (12, 'alpha', 'baz', 'corge', '2010-12-29 16:55:39.596076+00');
INSERT INTO sandbox VALUES (13, 'alpha', 'baz', 'corge', '2010-12-29 16:55:44.834019+00');
INSERT INTO sandbox VALUES (14, 'alpha', 'foo', 'qux', '2010-12-29 16:55:52.848792+00');
ALTER TABLE ONLY sandbox
ADD CONSTRAINT sandbox_pkey PRIMARY KEY (id);
这是我当前的 SQL 查询:
SELECT
*
FROM
(
SELECT
DISTINCT ON (this, that)
id, this, that, timestamp
FROM
sandbox
WHERE
callsign = 'alpha'
AND
CAST(timestamp AS date) = '2010-12-29'
)
playground
ORDER BY
timestamp
DESC
这是它给我的结果:
id this that timestamp
-----------------------------------------------------
14 foo qux 2010-12-29 16:55:52.848792+00
13 baz corge 2010-12-29 16:55:44.834019+00
11 baz qux 2010-12-29 16:54:53.891606+00
10 bar qux 2010-12-29 16:54:46.237294+00
9 baz quxx 2010-12-29 16:54:38.727095+00
8 bar corge 2010-12-29 16:54:33.318907+00
7 foo corge 2010-12-29 16:53:21.242904+00
这是我想看到的:
id this that timestamp count
-------------------------------------------------------------
14 foo qux 2010-12-29 16:55:52.848792+00 3
13 baz corge 2010-12-29 16:55:44.834019+00 2
11 baz qux 2010-12-29 16:54:53.891606+00 1
10 bar qux 2010-12-29 16:54:46.237294+00 1
9 baz quxx 2010-12-29 16:54:38.727095+00 1
8 bar corge 2010-12-29 16:54:33.318907+00 1
7 foo corge 2010-12-29 16:53:21.242904+00 1
编辑:
我正在使用 PostgreSQL 9.0.* (如果有帮助的话)。
I don't know how to ask my question clearly so I'll just show you the money.
To start with, here's a sample table:
CREATE TABLE sandbox (
id integer NOT NULL,
callsign text NOT NULL,
this text NOT NULL,
that text NOT NULL,
"timestamp" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE SEQUENCE sandbox_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE sandbox_id_seq OWNED BY sandbox.id;
SELECT pg_catalog.setval('sandbox_id_seq', 14, true);
ALTER TABLE sandbox ALTER COLUMN id SET DEFAULT nextval('sandbox_id_seq'::regclass);
INSERT INTO sandbox VALUES (1, 'alpha', 'foo', 'qux', '2010-12-29 16:51:09.897579+00');
INSERT INTO sandbox VALUES (2, 'alpha', 'foo', 'qux', '2010-12-29 16:51:36.108867+00');
INSERT INTO sandbox VALUES (3, 'bravo', 'bar', 'quxx', '2010-12-29 16:52:36.370507+00');
INSERT INTO sandbox VALUES (4, 'bravo', 'foo', 'quxx', '2010-12-29 16:52:47.584663+00');
INSERT INTO sandbox VALUES (5, 'charlie', 'foo', 'corge', '2010-12-29 16:53:00.742356+00');
INSERT INTO sandbox VALUES (6, 'delta', 'foo', 'qux', '2010-12-29 16:53:10.884721+00');
INSERT INTO sandbox VALUES (7, 'alpha', 'foo', 'corge', '2010-12-29 16:53:21.242904+00');
INSERT INTO sandbox VALUES (8, 'alpha', 'bar', 'corge', '2010-12-29 16:54:33.318907+00');
INSERT INTO sandbox VALUES (9, 'alpha', 'baz', 'quxx', '2010-12-29 16:54:38.727095+00');
INSERT INTO sandbox VALUES (10, 'alpha', 'bar', 'qux', '2010-12-29 16:54:46.237294+00');
INSERT INTO sandbox VALUES (11, 'alpha', 'baz', 'qux', '2010-12-29 16:54:53.891606+00');
INSERT INTO sandbox VALUES (12, 'alpha', 'baz', 'corge', '2010-12-29 16:55:39.596076+00');
INSERT INTO sandbox VALUES (13, 'alpha', 'baz', 'corge', '2010-12-29 16:55:44.834019+00');
INSERT INTO sandbox VALUES (14, 'alpha', 'foo', 'qux', '2010-12-29 16:55:52.848792+00');
ALTER TABLE ONLY sandbox
ADD CONSTRAINT sandbox_pkey PRIMARY KEY (id);
Here's the current SQL query I have:
SELECT
*
FROM
(
SELECT
DISTINCT ON (this, that)
id, this, that, timestamp
FROM
sandbox
WHERE
callsign = 'alpha'
AND
CAST(timestamp AS date) = '2010-12-29'
)
playground
ORDER BY
timestamp
DESC
This is the result it gives me:
id this that timestamp
-----------------------------------------------------
14 foo qux 2010-12-29 16:55:52.848792+00
13 baz corge 2010-12-29 16:55:44.834019+00
11 baz qux 2010-12-29 16:54:53.891606+00
10 bar qux 2010-12-29 16:54:46.237294+00
9 baz quxx 2010-12-29 16:54:38.727095+00
8 bar corge 2010-12-29 16:54:33.318907+00
7 foo corge 2010-12-29 16:53:21.242904+00
This is what I want to see:
id this that timestamp count
-------------------------------------------------------------
14 foo qux 2010-12-29 16:55:52.848792+00 3
13 baz corge 2010-12-29 16:55:44.834019+00 2
11 baz qux 2010-12-29 16:54:53.891606+00 1
10 bar qux 2010-12-29 16:54:46.237294+00 1
9 baz quxx 2010-12-29 16:54:38.727095+00 1
8 bar corge 2010-12-29 16:54:33.318907+00 1
7 foo corge 2010-12-29 16:53:21.242904+00 1
EDIT:
I'm using PostgreSQL 9.0.* (if that helps any).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有安装postgre,但我在SQL Server上做了这个,我想你可以使用这个想法:
输出:
I don't have postgre installed, but I did this on SQL Server, I think you can use the idea:
Output:
DCP 解决方案有效。在 postgres 上测试过。
顺便说一句,您应该避免使用时间戳作为列名,因为它是保留字并且需要加引号。
dcp solution works. Tested it on postgres.
BTW, you should avoid timestamp as a column name since it is a reserved word and needs to be quoted.