【发布时间】:2021-03-27 08:14:41
【问题描述】:
我有下表:
CREATE TABLE my_table (
the_visitor_id varchar(5) NOT NULL,
the_visitor_visit timestamp NOT NULL,
the_visitor_returning text
);
INSERT INTO my_table
VALUES ('VIS01', '2019-05-02 09:00:00','YES' ),
('VIS01', '2019-05-04 12:00:00',NULL ),
('VIS01', '2019-05-05 18:00:00',NULL ),
('VIS02', '2019-05-06 18:30:00',NULL),
('VIS02', '2019-05-15 12:00:00',NULL),
('VIS03', '2019-06-30 18:00:00','YES'),
('VIS04', '2019-06-30 18:00:00','NULL');
我想过滤掉所有只有一个观察(或记录)的visitor_id。在这种情况下 VIS03 和 VIS04,所以我必须以 VIS01 和 VIS02 结束。我试过这个:
SELECT DISTINCT ON(the_visitor_id) the_visitor_id,
the_visitor_visit, the_visitor_returning
FROM my_table
预期的结果应该是:
the_visitor_id the_visitor_visit the_visitor_returning
VIS01 2019-05-02 09:00:00 YES
VIS01 2019-05-04 12:00:00
VIS01 2019-05-05 18:00:00
VIS02 2019-05-06 18:30:00
VIS02 2019-05-15 12:00:00
但我想需要像等级这样的东西。任何帮助将不胜感激。
【问题讨论】:
标签: sql postgresql