postgresql 自定义查询,用于获取表的所有条目,并且仅包含基于特定列的多个重复项中的第一个

nmpmafwu  于 5个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(50)

我有一个位置模型,表看起来像
| ID|名称|VIN| ip地址|创建于|更新于|
| --|--|--|--|--|--|
| 0 |默认| 0 |0.0.0.0/0|2021-11-08 11:54:26.822623| 2021-11-08 11:54:26.822623|
| 1 |admin| 1 |10.108.150.143|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 2 |V122| 122 |10.108.150.122|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 3 |V123| 123 |10.108.150.123|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 4 |V124| 124 |10.108.150.124|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 5 |V122| 122 |10.108.150.122|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 6 |V125| 122 |10.108.150.125|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
位置模型中的方法

def self.find_all_non_duplicate
     return self.find(:all, :conditions => "id <> 1")
   end

字符串
我想获取位置表的所有条目,除了id = 1的条目,它只包含基于列ip_address的许多重复项中的第一个条目。
由于id = 2和id = 5的ip_address重复,我想保留很多重复的第条,即id = 2
该预期的结果是
| ID|名称|VIN| ip地址|创建于|更新于|
| --|--|--|--|--|--|
| 0 |默认| 0 |0.0.0.0/0|2021-11-08 11:54:26.822623| 2021-11-08 11:54:26.822623|
| 2 |V122| 122 |10.108.150.122|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 3 |V123| 123 |10.108.150.123|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 4 |V124| 124 |10.108.150.124|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|
| 6 |V125| 122 |10.108.150.125|2021-11-08 11:54:26.82885| 2021-11-08 11:54:26.82885|

id为1和5的条目将被忽略

bbmckpt7

bbmckpt71#

您需要的是最近向RoR提出的distinct onhere,但not yet merged,正如@engineersmnky所指出的那样。在原始SQL形式中,它看起来像这样:

select distinct on (ip_address) * 
from test 
where id<>1 
order by ip_address,created_at;

字符串
也就是说

self.where("id <> 1").distinct_on(:ip_address)


或者,直到新功能被接受:

self.where("id <> 1").select("distinct on (ip_address) *")


全db侧测试:

drop table if exists test cascade;
create table test (
    id serial primary key,
    name text,
    vin integer,
    ip_address inet,
    created_at timestamp,
    updated_at timestamp);
insert into test 
(id,name,vin,ip_address,created_at,updated_at)
values
(0,'default', 0,'0.0.0.0/0'::inet,'2021-11-08 11:54:26.822623'::timestamp,'2021-11-08 11:54:26.822623'::timestamp),
(1,'admin',   1,'10.108.150.143'::inet,'2021-11-08 11:54:26.82885'::timestamp,'2021-11-08 11:54:26.82885'::timestamp),
(2,'V122',    122,'10.108.150.122'::inet,'2021-11-08 11:54:26.82885'::timestamp,'2021-11-08 11:54:26.82885'::timestamp),
(3,'V123',    123,'10.108.150.123'::inet,'2021-11-08 11:54:26.82885'::timestamp,'2021-11-08 11:54:26.82885'::timestamp),
(4,'V124',    124,'10.108.150.124'::inet,'2021-11-08 11:54:26.82885'::timestamp,'2021-11-08 11:54:26.82885'::timestamp),
(5,'V122',    122,'10.108.150.122'::inet,'2021-11-08 11:54:26.82885'::timestamp,'2021-11-08 11:54:26.82885'::timestamp),
(6,'V125',    122,'10.108.150.125'::inet,'2021-11-08 11:54:26.82885'::timestamp,'2021-11-08 11:54:26.82885'::timestamp);

select distinct on (ip_address) * 
from test where id<>1 
order by ip_address,created_at;
-- id |  name   | vin |   ip_address   |         created_at         |         updated_at
------+---------+-----+----------------+----------------------------+----------------------------
--  0 | default |   0 | 0.0.0.0/0      | 2021-11-08 11:54:26.822623 | 2021-11-08 11:54:26.822623
--  2 | V122    | 122 | 10.108.150.122 | 2021-11-08 11:54:26.82885  | 2021-11-08 11:54:26.82885
--  3 | V123    | 123 | 10.108.150.123 | 2021-11-08 11:54:26.82885  | 2021-11-08 11:54:26.82885
--  4 | V124    | 124 | 10.108.150.124 | 2021-11-08 11:54:26.82885  | 2021-11-08 11:54:26.82885
--  6 | V125    | 122 | 10.108.150.125 | 2021-11-08 11:54:26.82885  | 2021-11-08 11:54:26.82885
--(5 rows)

相关问题