Limiting to sub-id in a query ?

Поиск
Список
Период
Сортировка
От Tim Smith
Тема Limiting to sub-id in a query ?
Дата
Msg-id CA+HuS5FY_Cgvni-7wgjAZ9YoGE5hCk_+QdGXaKBVyfn8CoW4Rw@mail.gmail.com
обсуждение исходный текст
Ответы Re: Limiting to sub-id in a query ?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general
Hi,

I've been trying various GROUP BY but these all end up erroring out,
so maybe I'm using the wrong tool for the job (or, more likely, the
required query is beyond my level of SQL-fu !).

CREATE TABLE IF NOT EXISTS names (
main_id bigint,
sub_id bigint,
name text
);

create unique index IF NOT EXISTS name_idx on names(main_id,sub_id);
insert into names(main_id,sub_id,name) values(1,2,'Bob');
insert into names(main_id,sub_id,name) values(1,1,'Baby Bob');
insert into names(main_id,sub_id,name) values(100,200,'Tom');
insert into names(main_id,sub_id,name) values(100,100,'Jerry');


The desired output is one row per main_id, with the desired row being
the one with the lowest sub_id, i.e.

select.......

  main_id  |  sub_id |       name
---------+------------+-------------------
 1 |  1 | Baby Bob
 100 |    100 | Jerry


В списке pgsql-general по дате отправления:

Предыдущее
От: Kevin Grittner
Дата:
Сообщение: Re: Locking question
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: Limiting to sub-id in a query ?