繁体   English   中英

在PostgreSQL中选择带有DISTINCT的列

[英]Selecting columns with DISTINCT in PostgreSQL

我正在从数据库查询公交车站,我希望每个公交线路/方向只返回1站。 这个查询只是这样:

Stop.select("DISTINCT line_id, direction")

除了它不会给我任何其他属性而不是那些2.我尝试了几个其他查询,除了line_iddirection字段之外还要返回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(或其他任何东西)拼命。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM