在PostgreSQL中选择带有DISTINCT的列
我正在从数据库查询公交车站,我希望每个公交线路/方向只返回1站。 这个查询只是这样:
Stop.select("DISTINCT line_id, direction")
除了它不会给我任何其他属性,除了那些2.我尝试了几个其他查询,让它返回除了line_id
和direction
字段之外的id
(理想情况下它将返回所有列),没有运气:
Stop.select("DISTINCT line_id, direction, id")
和
Stop.select("DISTINCT(line_id || '-' || direction), id")
在这两种情况下,查询都会丢失其distinct子句,并返回所有行。
一些很棒的家伙帮助我,并建议使用子查询让它返回所有的ID:
Stop.find_by_sql("SELECT DISTINCT a1.line_id, a1.direction, (SELECT a2.id from stops a2 where a2.line_id = a1.line_id AND a2.direction = a1.direction ORDER BY a2.id ASC LIMIT 1) as id FROM stops a1
然后,我可以提取所有ID并执行第二个查询以获取每个停靠点的完整属性。
有没有办法在1个查询中拥有它并让它返回所有属性?
Stop.select("DISTINCT ON (line_id, direction) *")
不是那么快 – 另一个答案选择stop_id任意
这就是你的问题毫无意义的原因。 我们可以拉出stop_ids并具有明显的line_id和方向。 但我们不知道为什么我们有stop_id。
create temp table test( line_id integer, direction char(1), stop_id integer); insert into test values (1, 'N', 1), (1, 'N', 2), (1, 'S', 1), (1, 'S', 2), (2, 'N', 1), (2, 'N', 2), (2, 'S', 1), (2, 'S', 2) ; select distinct on (line_id, direction) * from test; -- do this again but will reverse the order of stop_ids -- could it possible change our Robust Query?!!! drop table test; create temp table test(line_id integer,direction char(1),stop_id integer); insert into test values (1, 'N', 2), (1, 'N', 1), (1, 'S', 2), (1, 'S', 1), (2, 'N', 2), (2, 'N', 1), (2, 'S', 2), (2, 'S', 1) ; select distinct on (line_id, direction) * from test;
首先选择:
line_id | direction | stop_id ---------+-----------+--------- 1 | N | 1 1 | S | 1 2 | N | 1 2 | S | 1
第二个选择:
line_id | direction | stop_id ---------+-----------+--------- 1 | N | 2 1 | S | 2 2 | N | 2 2 | S | 2
所以我们在没有分组stop_id的情况下离开了,但我们无法保证为什么我们得到了我们所做的那个。 我们所知道的是这是有效的stop_id。 RDMS将保证的任何更新,插入和其他内容都可以围绕行的物理顺序进行更改。
这就是我在最高评论中的含义。 没有已知的理由将一个stop_id拉到另一个上,但不知怎的,你需要这个stop_id(或其他任何东西)拼命地。
- 为什么Rails / ActiveRecord在ORDER中给出Postgres SyntaxError“非整数常量”?
- 仅在成功保存父对象时才将更改保存到has_many关联?
- 如何在Rails中管理3个多对多模型
- 怎么逃避? (问号)运算符在Rails中查询Postgresql JSONB类型
- validation:if
- 在memcached中存储ActiveRecord对象的最佳方法是什么?
- 使用activerecord如何根据字段的最高值选择记录?
- 有没有办法确保ActiveRecord层次结构中每个记录有一个对象引用?
- 为什么此ActiveRecord语句在Server中失败但在Console中失败